Skip to content

Commit 13f3133

Browse files
author
Cameron James
committed
Language support, API Key is no longer required for FREE api calls
1 parent 4d5b6d8 commit 13f3133

File tree

2 files changed

+64
-18
lines changed

2 files changed

+64
-18
lines changed

Diff for: README.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ For more specific documentation to the APIs available, including endpoints, requ
2626

2727

2828

29+
## Language Support
30+
31+
BigDataCloud APIs now offer Locality information in a number of languages.
32+
Please see the example below for how to set the API language.
33+
[Click here For a list of all supported languages, and their respective codes.](https://www.bigdatacloud.com/supported-languages)
34+
35+
36+
2937
## Authentication / Identification
3038

3139
To use this API client you must have a BigDataCloud API Key.
@@ -65,9 +73,21 @@ See the example below.
6573

6674
//vanilla implementation
6775
var client=new BDCApiClient(apiKey);
76+
77+
/* You can set the default api language as needed */
78+
client.localityLanguage='es';
79+
6880
client.call(
81+
/* api endpoint */
6982
'ip-geolocation-full',
70-
{'ip':'8.8.8.8'},
83+
84+
/* api query parameters */
85+
{
86+
'ip':'8.8.8.8',
87+
/* You can override the default api language on a per-query basis
88+
* This is an optional parameter on all API calls */
89+
'localityLanguage':'zh'
90+
},
7191
function(jsonResult) {
7292
console.log('Vanilla result',jsonResult);
7393
},
@@ -83,7 +103,8 @@ See the example below.
83103
$.BDCApi('ip-geolocation-full',{
84104
data:{
85105
ip:'8.8.8.8',
86-
key:apiKey
106+
key:apiKey,
107+
localityLanguage:'en'
87108
},
88109
success:function(jsonResult) {
89110
console.log('jQuery result',jsonResult);

Diff for: bigdatacloud_api_client.js

+41-16
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,23 @@
11
;(function(_w,$) {
2-
var BDC=function(apiKey,nameSpace,server) {
2+
var BDC=function(apiKey,nameSpace,server,localityLanguage) {
33
this.apiKey=apiKey;
44
this.nameSpace=nameSpace ? nameSpace : 'data';
55
this.server=server ? server : 'api.bigdatacloud.net';
6+
this.localityLanguage=localityLanguage ? localityLanguage : 'en';
67
};
78
BDC.prototype={
89
call:function(endpoint,payload,successCb,errorCb,method) {
9-
var data=[];
10-
var hasKey=false;
11-
1210
if (!method) method='GET';
1311
else method=method.toUpperCase();
14-
1512
var params={
1613
url:'https://'+this.server+'/'+this.nameSpace+'/'+endpoint,
1714
type:method,
1815
success:successCb,
1916
error:errorCb,
2017
dataType:'json'
2118
};
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);
3119

32-
data=data.join('&');
20+
var data=this.prepareData(payload,endpoint);
3321

3422
switch(method) {
3523
case 'GET':
@@ -42,13 +30,34 @@
4230
}
4331
return this.xhr(params);
4432
},
33+
prepareData:function(payload,endpoint) {
34+
var data=[];
35+
var hasKey=false;
36+
var hasLocalityLanguage=false;
37+
if (payload) {
38+
for (var i in payload) {
39+
switch(i) {
40+
case 'key':
41+
hasKey=true;
42+
break;
43+
case 'localityLanguage':
44+
hasLocalityLanguage=true;
45+
break;
46+
}
47+
data.push(encodeURIComponent(i)+'='+encodeURIComponent(payload[i]));
48+
}
49+
}
50+
if (!hasKey && this.apiKey) data.push('key='+this.apiKey);
51+
if (!hasLocalityLanguage) data.push('localityLanguage='+this.localityLanguage);
52+
data=data.join('&');
53+
return data;
54+
},
4555
xhr:function(params) {
4656
if ($) {
4757
var xhr=$.ajax(params);
4858
} else {
4959
var xhr = new XMLHttpRequest()
5060
xhr.open(params.type, params.url, true);
51-
5261
xhr.onreadystatechange = function() {
5362
if (this.readyState === XMLHttpRequest.DONE) {
5463
if (this.status === 200) {
@@ -86,6 +95,19 @@
8695
xhr.send(params.data);
8796
}
8897
return xhr;
98+
},
99+
getClientCoordinates:function(cb) {
100+
if (!cb) return false;
101+
if (!navigator.geolocation || !navigator.geolocation.getCurrentPosition) return cb(false);
102+
return navigator.geolocation.getCurrentPosition(
103+
(function(position) { return this.cb(position);}).bind({cb:cb}),
104+
(function(err) { console.error(err); return this.cb(false);}).bind({cb:cb}),
105+
{
106+
enableHighAccuracy: true,
107+
timeout: 5000,
108+
maximumAge: 0
109+
}
110+
);
89111
}
90112
}
91113
_w.BDCApiClient=BDC;
@@ -96,6 +118,9 @@
96118
if (params.key) {
97119
$.BDCApiClientInstance.apiKey=params.key;
98120
}
121+
if (params.localityLanguage) {
122+
$.BDCApiClientInstance.setLanguage(params.localityLanguage);
123+
}
99124
if (params.nameSpace) {
100125
$.BDCApiClientInstance.nameSpace=params.nameSpace;
101126
}

0 commit comments

Comments
 (0)