Skip to content

Commit

Permalink
Language support, API Key is no longer required for FREE api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Cameron James committed Oct 7, 2019
1 parent 4d5b6d8 commit 13f3133
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ For more specific documentation to the APIs available, including endpoints, requ



## Language Support

BigDataCloud APIs now offer Locality information in a number of languages.
Please see the example below for how to set the API language.
[Click here For a list of all supported languages, and their respective codes.](https://www.bigdatacloud.com/supported-languages)



## Authentication / Identification

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

//vanilla implementation
var client=new BDCApiClient(apiKey);

/* You can set the default api language as needed */
client.localityLanguage='es';

client.call(
/* api endpoint */
'ip-geolocation-full',
{'ip':'8.8.8.8'},

/* api query parameters */
{
'ip':'8.8.8.8',
/* You can override the default api language on a per-query basis
* This is an optional parameter on all API calls */
'localityLanguage':'zh'
},
function(jsonResult) {
console.log('Vanilla result',jsonResult);
},
Expand All @@ -83,7 +103,8 @@ See the example below.
$.BDCApi('ip-geolocation-full',{
data:{
ip:'8.8.8.8',
key:apiKey
key:apiKey,
localityLanguage:'en'
},
success:function(jsonResult) {
console.log('jQuery result',jsonResult);
Expand Down
57 changes: 41 additions & 16 deletions bigdatacloud_api_client.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,23 @@
;(function(_w,$) {
var BDC=function(apiKey,nameSpace,server) {
var BDC=function(apiKey,nameSpace,server,localityLanguage) {
this.apiKey=apiKey;
this.nameSpace=nameSpace ? nameSpace : 'data';
this.server=server ? server : 'api.bigdatacloud.net';
this.localityLanguage=localityLanguage ? localityLanguage : 'en';
};
BDC.prototype={
call:function(endpoint,payload,successCb,errorCb,method) {
var data=[];
var hasKey=false;

if (!method) method='GET';
else method=method.toUpperCase();

var params={
url:'https://'+this.server+'/'+this.nameSpace+'/'+endpoint,
type:method,
success:successCb,
error:errorCb,
dataType:'json'
};
if (payload) {
for (var i in payload) {
if (i=='key') {
hasKey=true;
}
data.push(encodeURIComponent(i)+'='+encodeURIComponent(payload[i]));
}
}
if (!hasKey) data.push('key='+this.apiKey);

data=data.join('&');
var data=this.prepareData(payload,endpoint);

switch(method) {
case 'GET':
Expand All @@ -42,13 +30,34 @@
}
return this.xhr(params);
},
prepareData:function(payload,endpoint) {
var data=[];
var hasKey=false;
var hasLocalityLanguage=false;
if (payload) {
for (var i in payload) {
switch(i) {
case 'key':
hasKey=true;
break;
case 'localityLanguage':
hasLocalityLanguage=true;
break;
}
data.push(encodeURIComponent(i)+'='+encodeURIComponent(payload[i]));
}
}
if (!hasKey && this.apiKey) data.push('key='+this.apiKey);
if (!hasLocalityLanguage) data.push('localityLanguage='+this.localityLanguage);
data=data.join('&');
return data;
},
xhr:function(params) {
if ($) {
var xhr=$.ajax(params);
} else {
var xhr = new XMLHttpRequest()
xhr.open(params.type, params.url, true);

xhr.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.DONE) {
if (this.status === 200) {
Expand Down Expand Up @@ -86,6 +95,19 @@
xhr.send(params.data);
}
return xhr;
},
getClientCoordinates:function(cb) {
if (!cb) return false;
if (!navigator.geolocation || !navigator.geolocation.getCurrentPosition) return cb(false);
return navigator.geolocation.getCurrentPosition(
(function(position) { return this.cb(position);}).bind({cb:cb}),
(function(err) { console.error(err); return this.cb(false);}).bind({cb:cb}),
{
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
}
);
}
}
_w.BDCApiClient=BDC;
Expand All @@ -96,6 +118,9 @@
if (params.key) {
$.BDCApiClientInstance.apiKey=params.key;
}
if (params.localityLanguage) {
$.BDCApiClientInstance.setLanguage(params.localityLanguage);
}
if (params.nameSpace) {
$.BDCApiClientInstance.nameSpace=params.nameSpace;
}
Expand Down

0 comments on commit 13f3133

Please sign in to comment.