Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4d5b6d8

Browse files
author
Cameron James
committed
Initial commit
1 parent 381ac07 commit 4d5b6d8

File tree

2 files changed

+344
-2
lines changed

2 files changed

+344
-2
lines changed

‎README.md

Lines changed: 230 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,230 @@
1-
# js-api-client
2-
A frontend Javascript client for BigDataCloud API services.
1+
# BigDataCloud Javascript API Client
2+
3+
4+
A frontend Javascript client for connecting to the API services provided by [BigDataCloud](https://www.bigdatacloud.net)
5+
This client works in plain Vanilla Javascript and also adds easy access if jQuery is available.
6+
7+
8+
9+
## What API services does [BigDataCloud](https://www.bigdatacloud.net) offer?
10+
11+
BigDataCloud offers a range of extremely useful and fast APIs that can be utilised in both backend and frontend scenarios.
12+
From validating customer input live to the next generation of IP Geolocation technology, BigDataCloud has an API suitable to your needs.
13+
14+
For a full list of APIs, visit our [documentation area](https://www.bigdatacloud.net/ip-geolocation-apis).
15+
16+
You can access any and all BigDataCloud APIs with a free API Key.
17+
To get your API Key, just access your account and retrieve it from your [Account Dashboard](https://www.bigdatacloud.net/customer/account).
18+
If you are not yet a customer, it is completely free to join.
19+
20+
21+
22+
## Documentation
23+
24+
For documentation specific to this api client, please read below.
25+
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).
26+
27+
28+
29+
## Authentication / Identification
30+
31+
To use this API client you must have a BigDataCloud API Key.
32+
To get your personal key, just access your account and retrieve it from your [Account Dashboard](https://www.bigdatacloud.net/customer/account).
33+
If you are not yet a customer, it is completely free to join.
34+
35+
Simply provide this key when initiating the api client, and it will be included in all requests to the BigDataCloud API Server.
36+
If using the jQuery quick caller, you can provide the key in each request by appending the key parameter to the request payload.
37+
See the example below.
38+
39+
40+
41+
## Manual Installation
42+
43+
1. Download the included javascript file and place it in a publically accessible location
44+
2. Include the script tag `<script src="bigdatacloud_api_client.js" type="text/javascript"></script>` before your code execution
45+
3. If using jQuery, include the script after the jQuery declaration
46+
4. Initiate the API Client and make the required calls
47+
48+
49+
50+
## CDN Installation
51+
52+
1. Include the CDN script tag `<script src="https://cdn.jsdelivr.net/gh/bigdatacloudapi/js-api-client@latest/bigdatacloud_api_client.min.js" type="text/javascript"></script>` before your code execution
53+
2. If using jQuery, include the script after the jQuery declaration
54+
3. Initiate the API Client and make the required calls
55+
56+
57+
58+
## Example usage
59+
60+
```javascript
61+
<script src="https://cdn.jsdelivr.net/gh/bigdatacloudapi/js-api-client@latest/bigdatacloud_api_client.min.js" type="text/javascript"></script>
62+
<script type="text/javascript">
63+
64+
var apiKey='XXX'; // Your api key found at: https://www.bigdatacloud.net/customer/account
65+
66+
//vanilla implementation
67+
var client=new BDCApiClient(apiKey);
68+
client.call(
69+
'ip-geolocation-full',
70+
{'ip':'8.8.8.8'},
71+
function(jsonResult) {
72+
console.log('Vanilla result',jsonResult);
73+
},
74+
function(err,code) {
75+
console.log('Vanilla error',err,code);
76+
}
77+
);
78+
79+
80+
//jQuery implementation
81+
if (typeof jQuery != 'undefined') {
82+
(function($) {
83+
$.BDCApi('ip-geolocation-full',{
84+
data:{
85+
ip:'8.8.8.8',
86+
key:apiKey
87+
},
88+
success:function(jsonResult) {
89+
console.log('jQuery result',jsonResult);
90+
},
91+
error:function(error,code) {
92+
console.error('jQuery eror',error,code);
93+
}
94+
});
95+
})(jQuery);
96+
}
97+
98+
</script>
99+
```
100+
101+
102+
## Example output
103+
104+
```javascript
105+
{
106+
"ip": "8.8.8.8",
107+
"country": {
108+
"isoAlpha2": "US",
109+
"isoAlpha3": "USA",
110+
"m49Code": 840,
111+
"isoName": "United States of America (the)",
112+
"isoAdminLanguages": [
113+
{
114+
"isoAlpha3": "eng",
115+
"isoAlpha2": "en",
116+
"isoName": "English"
117+
}
118+
],
119+
"unRegion": "Americas/Northern America",
120+
"currency": {
121+
"numericCode": 840,
122+
"code": "USD",
123+
"name": "US Dollar",
124+
"minorUnits": 2
125+
},
126+
"wbRegion": {
127+
"id": "NAC",
128+
"iso2Code": "XU",
129+
"value": "North America"
130+
},
131+
"wbIncomeLevel": {
132+
"id": "HIC",
133+
"iso2Code": "XD",
134+
"value": "High income"
135+
},
136+
"callingCode": "1",
137+
"countryFlagEmoji": "🇺🇸"
138+
},
139+
"location": {
140+
"isoPrincipalSubdivision": "California",
141+
"isoPrincipalSubdivisionCode": "US-CA",
142+
"city": "Mountain View",
143+
"postcode": "94043",
144+
"latitude": 37.42,
145+
"longitude": -122.09,
146+
"timeZone": {
147+
"ianaTimeId": "America/Los_Angeles",
148+
"displayName": "(UTC-08:00) Pacific Time (US & Canada)",
149+
"effectiveTimeZoneFull": "Pacific Daylight Time",
150+
"effectiveTimeZoneShort": "PDT",
151+
"UtcOffsetSeconds": -25200,
152+
"UtcOffset": "-07",
153+
"isDaylightSavingTime": true,
154+
"localTime": "2019年04月06日T04:06:39.1691744"
155+
}
156+
},
157+
"lastUpdated": "2019年04月06日T09:09:36.1877959Z",
158+
"network": {
159+
"registry": "ARIN",
160+
"registryStatus": "assigned",
161+
"registeredCountry": "US",
162+
"registeredCountryName": "United States of America (the)",
163+
"organisation": "Google LLC",
164+
"isReachableGlobally": true,
165+
"isBogon": false,
166+
"bgpPrefix": "8.8.8.0/24",
167+
"bgpPrefixNetworkAddress": "8.8.8.0",
168+
"bgpPrefixLastAddress": "8.8.8.255",
169+
"totalAddresses": 256,
170+
"carriers": [
171+
{
172+
"asn": "AS15169",
173+
"asnNumeric": 15169,
174+
"organisation": "Google LLC",
175+
"name": "GOOGLE",
176+
"registry": "ARIN",
177+
"registeredCountry": "US",
178+
"registeredCountryName": "United States of America (the)",
179+
"registrationDate": "2000年03月30日",
180+
"registrationLastChange": "2012年02月25日",
181+
"totalIpv4Addresses": 8698103,
182+
"totalIpv4Prefixes": 435,
183+
"totalIpv4BogonPrefixes": 0,
184+
"rank": 53,
185+
"rankText": "#53 out of 62,872"
186+
}
187+
],
188+
"viaCarriers": [
189+
{
190+
"asn": "AS7018",
191+
"asnNumeric": 7018,
192+
"organisation": "ATT Services Inc.",
193+
"registeredCountry": "US",
194+
"registeredCountryName": "United States of America (the)",
195+
"rank": 2
196+
},
197+
/*........*/
198+
{
199+
"asn": "AS31019",
200+
"asnNumeric": 31019,
201+
"organisation": "Paulus M. Hoogsteder trading as Meanie",
202+
"registeredCountry": "NL",
203+
"registeredCountryName": "Netherlands (the)",
204+
"rank": 51153
205+
}
206+
]
207+
},
208+
"confidence": "low",
209+
"confidenceArea": [
210+
{
211+
"latitude": 18.0256672,
212+
"longitude": -66.5275345
213+
},
214+
/*........*/
215+
{
216+
"latitude": 18.0256672,
217+
"longitude": -66.5275345
218+
}
219+
],
220+
"securityThreat": "unknown",
221+
"hazardReport": {
222+
"isKnownAsTorServer": false,
223+
"isKnownAsProxy": false,
224+
"isKnownAsMailServer": false,
225+
"isKnownAsPublicRouter": false,
226+
"isBogon": false,
227+
"isUnreachable": false
228+
}
229+
}
230+
```

‎bigdatacloud_api_client.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
;(function(_w,$) {
2+
var BDC=function(apiKey,nameSpace,server) {
3+
this.apiKey=apiKey;
4+
this.nameSpace=nameSpace ? nameSpace : 'data';
5+
this.server=server ? server : 'api.bigdatacloud.net';
6+
};
7+
BDC.prototype={
8+
call:function(endpoint,payload,successCb,errorCb,method) {
9+
var data=[];
10+
var hasKey=false;
11+
12+
if (!method) method='GET';
13+
else method=method.toUpperCase();
14+
15+
var params={
16+
url:'https://'+this.server+'/'+this.nameSpace+'/'+endpoint,
17+
type:method,
18+
success:successCb,
19+
error:errorCb,
20+
dataType:'json'
21+
};
22+
if (payload) {
23+
for (var i in payload) {
24+
if (i=='key') {
25+
hasKey=true;
26+
}
27+
data.push(encodeURIComponent(i)+'='+encodeURIComponent(payload[i]));
28+
}
29+
}
30+
if (!hasKey) data.push('key='+this.apiKey);
31+
32+
data=data.join('&');
33+
34+
switch(method) {
35+
case 'GET':
36+
case 'HEAD':
37+
params.url+=(params.url.indexOf('?')==-1 ? '?' : '&')+data;
38+
break;
39+
default:
40+
params.data=data;
41+
break;
42+
}
43+
return this.xhr(params);
44+
},
45+
xhr:function(params) {
46+
if ($) {
47+
var xhr=$.ajax(params);
48+
} else {
49+
var xhr = new XMLHttpRequest()
50+
xhr.open(params.type, params.url, true);
51+
52+
xhr.onreadystatechange = function() {
53+
if (this.readyState === XMLHttpRequest.DONE) {
54+
if (this.status === 200) {
55+
try {
56+
if (params.success) {
57+
params.success(JSON.parse(this.responseText))
58+
}
59+
} catch (e) {
60+
if (params.error) {
61+
params.error(e)
62+
}
63+
}
64+
} else {
65+
if (params.error) {
66+
try {
67+
var result=JSON.parse(this.responseText);
68+
params.error(result,this.status);
69+
} catch (e) {
70+
params.error(this.responseText,this.status);
71+
}
72+
}
73+
}
74+
}
75+
}
76+
switch(params.type) {
77+
case 'PATCH':
78+
case 'POST':
79+
case 'PUT':
80+
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
81+
break;
82+
default:
83+
84+
break;
85+
}
86+
xhr.send(params.data);
87+
}
88+
return xhr;
89+
}
90+
}
91+
_w.BDCApiClient=BDC;
92+
if ($) {
93+
$.BDCApiClient=BDC;
94+
$.BDCApiClientInstance= new BDC();
95+
$.BDCApi=function(endpoint,params) {
96+
if (params.key) {
97+
$.BDCApiClientInstance.apiKey=params.key;
98+
}
99+
if (params.nameSpace) {
100+
$.BDCApiClientInstance.nameSpace=params.nameSpace;
101+
}
102+
if (params.server) {
103+
$.BDCApiClientInstance.server=params.server;
104+
}
105+
return $.BDCApiClientInstance.call(
106+
endpoint,
107+
params.data ? params.data : false,
108+
params.success ? params.success : false,
109+
params.error ? params.error : false,
110+
params.method ? params.method : false
111+
);
112+
}
113+
}
114+
})(window,typeof jQuery=='undefined' ? null : jQuery);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /