Skip to content

Commit 10979a1

Browse files
author
Cameron James
committed
Initial commit
1 parent 0eb419f commit 10979a1

File tree

4 files changed

+263
-0
lines changed

4 files changed

+263
-0
lines changed

README.md

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
# BigDataCloud Python API Client
2+
3+
4+
A Python client for connecting to the API services provided by [BigDataCloud](https://www.bigdatacloud.net)
5+
6+
7+
## What API services does [BigDataCloud](https://www.bigdatacloud.net) offer?
8+
9+
BigDataCloud offers a range of extremely useful and fast APIs that can be utilised in both backend and frontend scenarios.
10+
From validating customer input live to the next generation of IP Geolocation technology, BigDataCloud has an API suitable to your needs.
11+
12+
For a full list of APIs, visit our [documentation area](https://www.bigdatacloud.net/ip-geolocation-apis).
13+
14+
You can access any and all BigDataCloud APIs with a free API Key.
15+
To get your API Key, just access your account and retrieve it from your [Account Dashboard](https://www.bigdatacloud.net/customer/account).
16+
If you are not yet a customer, it is completely free to join.
17+
18+
19+
20+
## Documentation
21+
22+
For documentation specific to this api client, please read below.
23+
For more specific documentation to the APIs available, including endpoints, request and response data, please visit our [documentation area](https://www.bigdatacloud.net/ip-geolocation-apis).
24+
25+
26+
27+
## Authentication / Identification
28+
29+
To use this API client you must have a BigDataCloud API Key.
30+
To get your personal key, just access your account and retrieve it from your [Account Dashboard](https://www.bigdatacloud.net/customer/account).
31+
If you are not yet a customer, it is completely free to join.
32+
33+
Simply provide this key when initiating the api client, and it will be included in all requests to the BigDataCloud API Server.
34+
See the example below.
35+
36+
37+
38+
## Python 2.x Installation
39+
40+
`pip install bigdatacloudapi-client`
41+
42+
43+
## Python 3.x Installation
44+
45+
`pip3 install bigdatacloudapi-client`
46+
47+
48+
49+
## Example usage
50+
51+
```python
52+
import bigdatacloudapi
53+
54+
apiKey = 'XXX' # XXX being your api key found at: https://www.bigdatacloud.net/customer/account
55+
56+
client = bigdatacloudapi.Client(apiKey)
57+
58+
resultObject,httpResponseCode = client.getIpGeolocationFull({"ip":"8.8.8.8"})
59+
60+
print('HTTP Response Code: ',httpResponseCode)
61+
print('Result: ',resultObject['ip'])
62+
print('Result: ',resultObject['ip'])
63+
```
64+
65+
66+
## Example output
67+
68+
```python
69+
{
70+
"ip": "8.8.8.8",
71+
"country": {
72+
"isoAlpha2": "US",
73+
"isoAlpha3": "USA",
74+
"m49Code": 840,
75+
"isoName": "United States of America (the)",
76+
"isoAdminLanguages": [
77+
{
78+
"isoAlpha3": "eng",
79+
"isoAlpha2": "en",
80+
"isoName": "English"
81+
}
82+
],
83+
"unRegion": "Americas/Northern America",
84+
"currency": {
85+
"numericCode": 840,
86+
"code": "USD",
87+
"name": "US Dollar",
88+
"minorUnits": 2
89+
},
90+
"wbRegion": {
91+
"id": "NAC",
92+
"iso2Code": "XU",
93+
"value": "North America"
94+
},
95+
"wbIncomeLevel": {
96+
"id": "HIC",
97+
"iso2Code": "XD",
98+
"value": "High income"
99+
},
100+
"callingCode": "1",
101+
"countryFlagEmoji": "🇺🇸"
102+
},
103+
"location": {
104+
"isoPrincipalSubdivision": "California",
105+
"isoPrincipalSubdivisionCode": "US-CA",
106+
"city": "Mountain View",
107+
"postcode": "94043",
108+
"latitude": 37.42,
109+
"longitude": -122.09,
110+
"timeZone": {
111+
"ianaTimeId": "America/Los_Angeles",
112+
"displayName": "(UTC-08:00) Pacific Time (US & Canada)",
113+
"effectiveTimeZoneFull": "Pacific Daylight Time",
114+
"effectiveTimeZoneShort": "PDT",
115+
"UtcOffsetSeconds": -25200,
116+
"UtcOffset": "-07",
117+
"isDaylightSavingTime": true,
118+
"localTime": "2019-04-06T04:06:39.1691744"
119+
}
120+
},
121+
"lastUpdated": "2019-04-06T09:09:36.1877959Z",
122+
"network": {
123+
"registry": "ARIN",
124+
"registryStatus": "assigned",
125+
"registeredCountry": "US",
126+
"registeredCountryName": "United States of America (the)",
127+
"organisation": "Google LLC",
128+
"isReachableGlobally": true,
129+
"isBogon": false,
130+
"bgpPrefix": "8.8.8.0/24",
131+
"bgpPrefixNetworkAddress": "8.8.8.0",
132+
"bgpPrefixLastAddress": "8.8.8.255",
133+
"totalAddresses": 256,
134+
"carriers": [
135+
{
136+
"asn": "AS15169",
137+
"asnNumeric": 15169,
138+
"organisation": "Google LLC",
139+
"name": "GOOGLE",
140+
"registry": "ARIN",
141+
"registeredCountry": "US",
142+
"registeredCountryName": "United States of America (the)",
143+
"registrationDate": "2000-03-30",
144+
"registrationLastChange": "2012-02-25",
145+
"totalIpv4Addresses": 8698103,
146+
"totalIpv4Prefixes": 435,
147+
"totalIpv4BogonPrefixes": 0,
148+
"rank": 53,
149+
"rankText": "#53 out of 62,872"
150+
}
151+
],
152+
"viaCarriers": [
153+
{
154+
"asn": "AS7018",
155+
"asnNumeric": 7018,
156+
"organisation": "ATT Services Inc.",
157+
"registeredCountry": "US",
158+
"registeredCountryName": "United States of America (the)",
159+
"rank": 2
160+
},
161+
/*........*/
162+
{
163+
"asn": "AS31019",
164+
"asnNumeric": 31019,
165+
"organisation": "Paulus M. Hoogsteder trading as Meanie",
166+
"registeredCountry": "NL",
167+
"registeredCountryName": "Netherlands (the)",
168+
"rank": 51153
169+
}
170+
]
171+
},
172+
"confidence": "low",
173+
"confidenceArea": [
174+
{
175+
"latitude": 18.0256672,
176+
"longitude": -66.5275345
177+
},
178+
/*........*/
179+
{
180+
"latitude": 18.0256672,
181+
"longitude": -66.5275345
182+
}
183+
],
184+
"securityThreat": "unknown",
185+
"hazardReport": {
186+
"isKnownAsTorServer": false,
187+
"isKnownAsProxy": false,
188+
"isKnownAsMailServer": false,
189+
"isKnownAsPublicRouter": false,
190+
"isBogon": false,
191+
"isUnreachable": false
192+
}
193+
}
194+
```

bigdatacloudapi/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from bigdatacloudapi.client import Client

bigdatacloudapi/client.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import requests
2+
import re
3+
import json
4+
5+
class Client:
6+
def __init__(
7+
self,
8+
apiKey,
9+
nameSpace='data',
10+
server='api.bigdatacloud.net'
11+
):
12+
self.apiKey=apiKey
13+
self.nameSpace=nameSpace
14+
self.server=server
15+
16+
def __getattr__(self,p):
17+
def mm(params):
18+
def conversion(m):
19+
return "-"+m.group(1).lower()
20+
21+
key = re.sub('([A-Z])',conversion,p)
22+
segs = key.split('-')
23+
method = segs.pop(0).upper()
24+
25+
return self.communicate('-'.join(segs),method,params)
26+
return mm
27+
28+
def communicate(self,endpoint,method,payload):
29+
url='https://'+self.server+'/'+self.nameSpace+'/'+endpoint
30+
31+
headers={'X-BDC-Key':self.apiKey}
32+
33+
if (method=='POST' or method=='PUT' or method=='PATCH'):
34+
headers['content-type']='application/x-www-form-urlencoded'
35+
36+
r=getattr(requests,method.lower())(url,headers=headers,params=payload,data=payload)
37+
38+
return [r.json(),r.status_code]

setup.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os.path
2+
from setuptools import setup
3+
4+
# The directory containing this file
5+
HERE = os.path.abspath(os.path.dirname(__file__))
6+
7+
# The text of the README file
8+
with open(os.path.join(HERE, "README.md")) as fid:
9+
README = fid.read()
10+
11+
# This call to setup() does all the work
12+
setup(
13+
name="bigdatacloudapi-client",
14+
version="1.0.0",
15+
description="A Python client for BigDataCloud API connectivity (https://www.bigdatacloud.net)",
16+
long_description=README,
17+
long_description_content_type="text/markdown",
18+
url="https://github.com/bigdatacloudapi/python-api-client",
19+
author="BigDataCloud",
20+
author_email="support@bigdatacloud.net",
21+
license="MIT",
22+
classifiers=[
23+
"License :: OSI Approved :: MIT License",
24+
"Programming Language :: Python",
25+
"Programming Language :: Python :: 2",
26+
"Programming Language :: Python :: 3",
27+
],
28+
packages=["bigdatacloudapi"],
29+
install_requires=["requests","re","json"]
30+
)

0 commit comments

Comments
 (0)