Skip to content

Commit 13fd355

Browse files
committed
v0.1.0
0 parents  commit 13fd355

File tree

7 files changed

+254
-0
lines changed

7 files changed

+254
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
build
2+
dist
3+
**/__pycache__
4+
abstract_python_ip_geolocation.egg-info
5+
dev_test.py
6+
.vscode
7+
*.history

README.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# AbstractAPI python-ip-geolocation library
2+
3+
Integrate the powerful [IP Geolocation API from Abstract](https://www.abstractapi.com/ip-geolocation-api) in your Python project in a few lines of code.
4+
5+
Abstract's IP Geolocation API is a fast, lightweight, modern, and RESTful JSON API allowing you to look up the location, timezone, country details, and more of an IPv4 or IPv6 address.
6+
7+
It's very simple to use: you only need to submit your API key and an IP address, and the API will respond with an assessment of its geographical location, as well as additional details like the timezone, if it's a VPN address, and more.
8+
9+
Validating and verifying IP addresses is a critical step to reducing the chances of low-quality data and fraudulent or risky users in your website or application.
10+
11+
# Documentation
12+
13+
## Supported Python Versions
14+
15+
This library supports the **Python version 3.6** and higher.
16+
17+
## Installation
18+
19+
You can install **python-ip-geolocation** via PyPi or by downloading the source.
20+
21+
### Via Composer:
22+
23+
**python-ip-geolocation** is available on PyPi as the
24+
[`abstract-python-ip-geolocation`](https://pypi.org/project/abstract-python-ip-geolocation/) package:
25+
26+
```bash
27+
pip install abstract-python-ip-geolocation
28+
```
29+
30+
## API key
31+
32+
Get your API key for free and without hassle from the [Abstact website](https://app.abstractapi.com/users/signup?target=/api/ip-geolocation/pricing/select).
33+
34+
## Quickstart
35+
36+
### Geolocation from an IP Address
37+
38+
```python
39+
# Get a Geolocation from an IP Address Abstract's IP Geolocation API and Python
40+
from python_ip_geolocation import AbstractIpGeolocation
41+
42+
IP_GEOLOCATION_API_KEY = "YYYYYY"; # Get your API Key from https://app.abstractapi.com/api/ip-geolocation/documentation
43+
44+
AbstractIpGeolocation.configure(IP_GEOLOCATION_API_KEY)
45+
AbstractIpGeolocation.look_up("108.177.16.0")
46+
```
47+
48+
## API response
49+
50+
The API response is returned in a `IpGeolocationData` object.
51+
52+
| PARAMETER | TYPE | DETAILS |
53+
| - | - | - |
54+
| Parameter | Type | Details |
55+
| ip_address | String | The requested IP address |
56+
| city | String | City's name. |
57+
| city_geoname_id | String | City's geoname ID. |
58+
| region | String | State or province in which the the city is located. |
59+
| region_iso_code | Char[2] | State or province's ISO 3166-2 code. |
60+
| region_geoname_id | String | State or province's geoname ID. |
61+
| postal_code | String | ZIP or postal code. |
62+
| country | String | Country's name. |
63+
| country_code | Char[2] | Country's ISO 3166-1 alpha-2 code. |
64+
| country_geoname_id | String | Country's geoname ID. |
65+
| country_is_eu | Boolean | True if the country is in the EU, false if it is not. |
66+
| continent | String | Continent's name. |
67+
| continent_code | Char[2] | 2 letter continent code: AF, AS, EU, NA, OC, SA, AN |
68+
| continent_geoname_id | String | Continent's geoname ID. |
69+
| longitude | Float | Decimal of the longitude. |
70+
| latitude | Float | Decimal of the latitude. |
71+
| security > is_vpn | Boolean | Whether the IP address is using from a VPN or using a proxy |
72+
| timezone > name | String | Timezone's name from the IANA Time Zone Database. |
73+
| timezone > abbreviation | String | Timezone's abbreviation, also from the IANA Time Zone Database. |
74+
| timezone > gmt_offset | String | Timezone's offset from Greenwich Mean Time (GMT). |
75+
| timezone > current_time | String | Current time in the local time zone. |
76+
| timezone > is_dst | Boolean | True if the location is currently in Daylight Savings Time (DST). |
77+
| flag > svg | String | Link to a hosted version of the country's flag in SVG format. |
78+
| flag > png | String | Link to a hosted version of the country's flag in PNG format. |
79+
| flag > emoji | String | Country's flag as an emoji. |
80+
| flag > unicode | String | Country's flag in unicode. |
81+
| currency > currency_name | String | The currency's name. |
82+
| currency > currency_code | String | The currency's code in ISO 4217 format. |
83+
| connection > connection_type | String | Type of network connection: Dialup, Cable/DSL, Cellular, Corporate |
84+
| connection > autonomous_system_number | Uint32 | Autonomous System number |
85+
| connection > autonomous_system_organization | String | Autonomous System Organization name. |
86+
| connection > isp_name | String | Internet Service Provider (ISP) name. |
87+
| connection > organization_name | String | Organization name. |
88+
89+
## Detailed documentation
90+
91+
You will find additional information and request examples in the [Abstract help page](https://app.abstractapi.com/api/ip-geolocation/documentation).
92+
93+
## Getting help
94+
95+
If you need help installing or using the library, please contact [Abstract's Support](https://app.abstractapi.com/api/ip-geolocation/support).
96+
97+
For bug report and feature suggestion, please use [this repository issues page](https://github.com/abstractapi/python-ip-geolocation/issues).
98+
99+
# Contribution
100+
101+
Contributions are always welcome, as they improve the quality of the libraries we provide to the community.
102+
103+
Please provide your changes covered by the appropriate unit tests, and post them in the [pull requests page](https://github.com/abstractapi/python-ip-geolocation/pulls).
104+
105+
## Setup
106+
107+
To install the requirements, run:
108+
109+
```bash
110+
python3 setup.py install --user
111+
```
112+
113+
Once you implementer all your changes and the unit tests, run the following command to run the tests:
114+
115+
```bash
116+
EMAIL_VAL_API_KEY=YYYYYY python3 tests/test_python_ip_geolocation.py
117+
```

python_ip_geolocation/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .python_ip_geolocation import AbstractIpGeolocation
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from python_core import HttpEndpoint
2+
from collections import namedtuple
3+
4+
class AbstractIpGeolocation:
5+
6+
api_key = None
7+
http_endpoint = HttpEndpoint(
8+
endpoint_subdomain='ipgeolocation',
9+
global_req_params={
10+
'lang' : 'python'
11+
}
12+
)
13+
14+
15+
@staticmethod
16+
def configure(api_key):
17+
AbstractIpGeolocation.api_key = api_key
18+
19+
20+
@staticmethod
21+
def look_up(ip):
22+
23+
if AbstractIpGeolocation.api_key is None:
24+
exception_type = "APINotConfiguredException"
25+
exception_msg = "Can't use the endpoint unless it's configured, use configure(api_key)"
26+
raise Exception(
27+
"[{}] : {}".format(exception_type, exception_msg)
28+
)
29+
30+
try:
31+
req_params = {
32+
"api_key" : AbstractIpGeolocation.api_key,
33+
"ip_address" : ip
34+
}
35+
response = AbstractIpGeolocation.http_endpoint.get(
36+
req_params=req_params
37+
)
38+
# Convert the dict to an object
39+
for key in response:
40+
if (type(response[key]) == dict):
41+
# dict holding only {value, text}
42+
if "value" in response[key]:
43+
response[key] = response[key]["value"]
44+
# embedded dict, convert to object
45+
else:
46+
EmbeddedObject = namedtuple('EmbeddedObject', response[key])
47+
embedded_obj = EmbeddedObject(**response[key])
48+
response[key] = embedded_obj
49+
50+
ResponseObject = namedtuple('ResponseObject', response.keys())
51+
response_obj = ResponseObject(**response)
52+
return response_obj
53+
54+
except Exception as e:
55+
# HttpEndpoint has expressive exceptions
56+
raise SystemExit(e)

requirements.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
certifi==2020.12.5
2+
chardet==4.0.0
3+
idna==2.10
4+
plotly==4.14.3
5+
requests==2.25.1
6+
urllib3==1.26.4
7+
wincertstore==0.2
8+
python-dotenv==0.17.0
9+
abstract_python_core==0.1.0

setup.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from setuptools import setup, find_packages
2+
3+
try: # for pip >= 10
4+
from pip._internal.req import parse_requirements
5+
except ImportError: # for pip <= 9.0.3
6+
from pip.req import parse_requirements
7+
8+
# Set the library's long description to the repo's README.md
9+
with open('README.md', 'r') as readme_file:
10+
readme = readme_file.read()
11+
12+
with open('requirements.txt') as f:
13+
required = f.read().splitlines()
14+
15+
requirements = required
16+
17+
setup(
18+
name='abstract_python_ip_geolocation',
19+
version='0.1.0',
20+
author='Benjamin Bouchet',
21+
author_email='libraries@abstractapi.com',
22+
description="AbstractIpGeolocation - Wrapper to quickly start using the powerful AbstractAPI's IP geolocation service in your projects.",
23+
long_description=readme,
24+
long_description_content_type='text/markdown',
25+
url='https://github.com/abstractapi/python-ip-geolocation',
26+
packages=find_packages(),
27+
install_requires=requirements,
28+
classifiers=[
29+
'Programming Language :: Python :: 3.6',
30+
'Intended Audience :: Developers',
31+
'License :: OSI Approved :: MIT License',
32+
],
33+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import unittest
2+
import os, sys
3+
4+
from python_ip_geolocation import AbstractIpGeolocation
5+
from dotenv import load_dotenv
6+
7+
load_dotenv()
8+
9+
IP_GEOLOCATION_API_KEY = os.getenv('IP_GEOLOCATION_API_KEY')
10+
11+
class TestAbstractIpGeolocation(unittest.TestCase):
12+
def __init__(self, *args, **kwargs):
13+
14+
super(TestAbstractIpGeolocation, self).__init__(*args, **kwargs)
15+
16+
def test_no_config(self):
17+
18+
with self.assertRaises(Exception):
19+
# tests aren't always run in order, so make sure to
20+
# clear api_key
21+
AbstractIpGeolocation.api_key = None
22+
AbstractIpGeolocation.look_up("108.177.16.0")
23+
24+
def test_config(self):
25+
26+
AbstractIpGeolocation.configure(IP_GEOLOCATION_API_KEY)
27+
AbstractIpGeolocation.look_up("108.177.16.0")
28+
29+
30+
if __name__ == '__main__':
31+
unittest.main()

0 commit comments

Comments
 (0)