Skip to content

Commit edb40e7

Browse files
author
Cameron James
committed
Initial Commit
1 parent 155dbb3 commit edb40e7

File tree

3 files changed

+325
-0
lines changed

3 files changed

+325
-0
lines changed

README.md

+208
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
# BigDataCloud NodeJS API Client
2+
3+
4+
A NodeJS 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+
## Installation
39+
40+
`npm install @bigdatacloudapi/client`
41+
42+
43+
44+
## Example usage
45+
46+
```javascript
47+
const client = require('@bigdatacloudapi/client')('XXX'); // XXX being your api key found at: https://www.bigdatacloud.net/customer/account
48+
49+
/*
50+
* All api endpoints can be accessed via magic methods in the following camelised format:
51+
* method | endpoint
52+
* For example: an asynchronous "GET" call to the "ip-geolocation-full" endpoint would be: client.getIpGeolocationFull();
53+
* All endpoints return a promise
54+
*/
55+
56+
//Asynchronous example using 'then':
57+
client
58+
.getIpGeolocationFull({ip:'8.8.8.8'})
59+
.then(function(jsonResult) {
60+
console.log('Asynchronous "then" result:',jsonResult);
61+
})
62+
.catch(function(error) {
63+
console.error('Asynchronous "then" error:', error);
64+
});
65+
66+
//Asynchronous example using 'await':
67+
(async function() {
68+
try {
69+
var jsonResult = await client.getIpGeolocationFull({ip:'8.8.8.8'});
70+
console.log('Asynchronous "await" result:',jsonResult);
71+
} catch (error) {
72+
console.error('Asynchronous "await" error:', error);
73+
}
74+
})();
75+
76+
</script>
77+
```
78+
79+
80+
## Example output
81+
82+
```javascript
83+
{
84+
"ip": "8.8.8.8",
85+
"country": {
86+
"isoAlpha2": "US",
87+
"isoAlpha3": "USA",
88+
"m49Code": 840,
89+
"isoName": "United States of America (the)",
90+
"isoAdminLanguages": [
91+
{
92+
"isoAlpha3": "eng",
93+
"isoAlpha2": "en",
94+
"isoName": "English"
95+
}
96+
],
97+
"unRegion": "Americas/Northern America",
98+
"currency": {
99+
"numericCode": 840,
100+
"code": "USD",
101+
"name": "US Dollar",
102+
"minorUnits": 2
103+
},
104+
"wbRegion": {
105+
"id": "NAC",
106+
"iso2Code": "XU",
107+
"value": "North America"
108+
},
109+
"wbIncomeLevel": {
110+
"id": "HIC",
111+
"iso2Code": "XD",
112+
"value": "High income"
113+
},
114+
"callingCode": "1",
115+
"countryFlagEmoji": "🇺🇸"
116+
},
117+
"location": {
118+
"isoPrincipalSubdivision": "California",
119+
"isoPrincipalSubdivisionCode": "US-CA",
120+
"city": "Mountain View",
121+
"postcode": "94043",
122+
"latitude": 37.42,
123+
"longitude": -122.09,
124+
"timeZone": {
125+
"ianaTimeId": "America/Los_Angeles",
126+
"displayName": "(UTC-08:00) Pacific Time (US & Canada)",
127+
"effectiveTimeZoneFull": "Pacific Daylight Time",
128+
"effectiveTimeZoneShort": "PDT",
129+
"UtcOffsetSeconds": -25200,
130+
"UtcOffset": "-07",
131+
"isDaylightSavingTime": true,
132+
"localTime": "2019-04-06T04:06:39.1691744"
133+
}
134+
},
135+
"lastUpdated": "2019-04-06T09:09:36.1877959Z",
136+
"network": {
137+
"registry": "ARIN",
138+
"registryStatus": "assigned",
139+
"registeredCountry": "US",
140+
"registeredCountryName": "United States of America (the)",
141+
"organisation": "Google LLC",
142+
"isReachableGlobally": true,
143+
"isBogon": false,
144+
"bgpPrefix": "8.8.8.0/24",
145+
"bgpPrefixNetworkAddress": "8.8.8.0",
146+
"bgpPrefixLastAddress": "8.8.8.255",
147+
"totalAddresses": 256,
148+
"carriers": [
149+
{
150+
"asn": "AS15169",
151+
"asnNumeric": 15169,
152+
"organisation": "Google LLC",
153+
"name": "GOOGLE",
154+
"registry": "ARIN",
155+
"registeredCountry": "US",
156+
"registeredCountryName": "United States of America (the)",
157+
"registrationDate": "2000-03-30",
158+
"registrationLastChange": "2012-02-25",
159+
"totalIpv4Addresses": 8698103,
160+
"totalIpv4Prefixes": 435,
161+
"totalIpv4BogonPrefixes": 0,
162+
"rank": 53,
163+
"rankText": "#53 out of 62,872"
164+
}
165+
],
166+
"viaCarriers": [
167+
{
168+
"asn": "AS7018",
169+
"asnNumeric": 7018,
170+
"organisation": "ATT Services Inc.",
171+
"registeredCountry": "US",
172+
"registeredCountryName": "United States of America (the)",
173+
"rank": 2
174+
},
175+
/*........*/
176+
{
177+
"asn": "AS31019",
178+
"asnNumeric": 31019,
179+
"organisation": "Paulus M. Hoogsteder trading as Meanie",
180+
"registeredCountry": "NL",
181+
"registeredCountryName": "Netherlands (the)",
182+
"rank": 51153
183+
}
184+
]
185+
},
186+
"confidence": "low",
187+
"confidenceArea": [
188+
{
189+
"latitude": 18.0256672,
190+
"longitude": -66.5275345
191+
},
192+
/*........*/
193+
{
194+
"latitude": 18.0256672,
195+
"longitude": -66.5275345
196+
}
197+
],
198+
"securityThreat": "unknown",
199+
"hazardReport": {
200+
"isKnownAsTorServer": false,
201+
"isKnownAsProxy": false,
202+
"isKnownAsMailServer": false,
203+
"isKnownAsPublicRouter": false,
204+
"isBogon": false,
205+
"isUnreachable": false
206+
}
207+
}
208+
```

index.js

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
const request = require('request');
2+
3+
class Client {
4+
constructor(apiKey, nameSpace, server) {
5+
var _this=this;
6+
this.apiKey=apiKey;
7+
this.nameSpace=nameSpace ? nameSpace : 'data';
8+
this.server=server ? server : 'api.bigdatacloud.net';
9+
10+
return new Proxy(this,{
11+
get:function(t,p) {
12+
if (typeof t[p]!=='undefined') return t[p];
13+
return function(params) {
14+
var key=p;
15+
var method='GET';
16+
17+
key=key.replace(/([A-Z])/g,function(m,c,o,i) {
18+
return '-'+c.toLowerCase();
19+
});
20+
key=key.trim('-');
21+
key=key.split('-');
22+
23+
if (key.length>1) {
24+
var methodTest=key[0].toUpperCase();
25+
switch (methodTest) {
26+
case 'GET':
27+
case 'POST':
28+
case 'PUT':
29+
case 'DELETE':
30+
case 'OPTIONS':
31+
case 'PATCH':
32+
case 'HEAD':
33+
method=methodTest;
34+
key.shift();
35+
break;
36+
}
37+
}
38+
var endpoint=key.join('-');
39+
return _this.communicate(endpoint,method,params);
40+
}
41+
}
42+
});
43+
}
44+
45+
async communicate(endpoint,method,payload) {
46+
var qs=[];
47+
var data=false;
48+
var hasKey=false;
49+
if (!method) method='GET';
50+
method=method.toUpperCase();
51+
52+
var url='https://'+this.server+'/'+this.nameSpace+'/'+endpoint;
53+
54+
if (payload) {
55+
for (var i in payload) {
56+
if (i=='key') hasKey=true;
57+
qs.push(encodeURIComponent(i)+'='+encodeURIComponent(payload[i]));
58+
}
59+
}
60+
if (!hasKey) qs.push('key='+this.apiKey);
61+
62+
if (qs.length && method=='GET' || method=='HEAD' || method=='DELETE') {
63+
if (qs.length) {
64+
url+=(url.indexOf('?')==-1 ? '?' : '&')+qs.join('&');
65+
}
66+
} else if (qs.length) {
67+
data=qs.join('&');
68+
}
69+
70+
return this.talk(method,url,data);
71+
}
72+
73+
talk(method,url,data) {
74+
return new Promise((resolve, reject) => {
75+
var payload={url:url,json:true,method:method};
76+
if (method=='POST' || method=='PUT' || method=='PATCH') {
77+
payload.headers={'content-type' : 'application/x-www-form-urlencoded'};
78+
}
79+
if (data) payload.body=data;
80+
request(payload, (error, response, body) => {
81+
if (error) reject(error,0);
82+
if (response.statusCode != 200) {
83+
reject(body,code);
84+
}
85+
resolve(body);
86+
});
87+
});
88+
}
89+
};
90+
91+
module.exports=function(apiKey,nameSpace,server) {
92+
return new Client(apiKey,nameSpace,server);
93+
}

package.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "@bigdatacloudapi/client",
3+
"version": "1.0.1",
4+
"description": "A NodeJS client for BigDataCloud API connectivity (https://www.bigdatacloud.net)",
5+
"main": "index.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/bigdatacloudapi/nodejs-api-client.git"
9+
},
10+
"keywords": [
11+
"ip-geolocation",
12+
"bigdatacloud",
13+
"bigdatacloudapi"
14+
],
15+
"author": "BigDataCloud",
16+
"license": "MIT",
17+
"bugs": {
18+
"url": "https://github.com/bigdatacloudapi/nodejs-api-client/issues"
19+
},
20+
"homepage": "https://github.com/bigdatacloudapi/nodejs-api-client#readme",
21+
"dependencies": {
22+
"request": "^1.0.0"
23+
}
24+
}

0 commit comments

Comments
 (0)