-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbigdatacloud_api_client.js
More file actions
139 lines (136 loc) · 3.7 KB
/
bigdatacloud_api_client.js
File metadata and controls
139 lines (136 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
;(function(_w,$) {
var BDC=function(apiKey,nameSpace,server,localityLanguage) {
this.apiKey=apiKey;
this.nameSpace=nameSpace ? nameSpace : 'data';
this.server=server ? server : 'api-bdc.net';
this.localityLanguage=localityLanguage ? localityLanguage : 'en';
};
BDC.prototype={
call:function(endpoint,payload,successCb,errorCb,method) {
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'
};
var data=this.prepareData(payload,endpoint);
switch(method) {
case 'GET':
case 'HEAD':
params.url+=(params.url.indexOf('?')==-1 ? '?' : '&')+data;
break;
default:
params.data=data;
break;
}
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) {
try {
if (params.success) {
params.success(JSON.parse(this.responseText))
}
} catch (e) {
if (params.error) {
params.error(e)
}
}
} else {
if (params.error) {
try {
var result=JSON.parse(this.responseText);
params.error(result,this.status);
} catch (e) {
params.error(this.responseText,this.status);
}
}
}
}
}
switch(params.type) {
case 'PATCH':
case 'POST':
case 'PUT':
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
break;
default:
break;
}
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;
if ($) {
$.BDCApiClient=BDC;
$.BDCApiClientInstance= new BDC();
$.BDCApi=function(endpoint,params) {
if (params.key) {
$.BDCApiClientInstance.apiKey=params.key;
}
if (params.localityLanguage) {
$.BDCApiClientInstance.setLanguage(params.localityLanguage);
}
if (params.nameSpace) {
$.BDCApiClientInstance.nameSpace=params.nameSpace;
}
if (params.server) {
$.BDCApiClientInstance.server=params.server;
}
return $.BDCApiClientInstance.call(
endpoint,
params.data ? params.data : false,
params.success ? params.success : false,
params.error ? params.error : false,
params.method ? params.method : false
);
}
}
})(window,typeof jQuery=='undefined' ? null : jQuery);