Skip to content

Commit 1c256eb

Browse files
author
Cameron James
committed
Removal of 'request' module requirement, improved documentation
1 parent edb40e7 commit 1c256eb

File tree

5 files changed

+92
-18
lines changed

5 files changed

+92
-18
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
test.js

README.md

+40-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ For documentation specific to this api client, please read below.
2323
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).
2424

2525

26+
## Update Notes
27+
28+
- This repository now utilises `node-fetch` rather than the deprecated `request` module.
29+
- Calls to the API will return a JSON object as a successful response, see below for an example
30+
- Exceptions return a single object: `{error:json_object || error_string,code:http_status_code}`
31+
32+
2633

2734
## Authentication / Identification
2835

@@ -43,6 +50,8 @@ See the example below.
4350

4451
## Example usage
4552

53+
The below example is found in the included demo.js.
54+
4655
```javascript
4756
const client = require('@bigdatacloudapi/client')('XXX'); // XXX being your api key found at: https://www.bigdatacloud.net/customer/account
4857

@@ -56,7 +65,7 @@ See the example below.
5665
//Asynchronous example using 'then':
5766
client
5867
.getIpGeolocationFull({ip:'8.8.8.8'})
59-
.then(function(jsonResult) {
68+
.then((jsonResult=> {
6069
console.log('Asynchronous "then" result:',jsonResult);
6170
})
6271
.catch(function(error) {
@@ -73,7 +82,6 @@ See the example below.
7382
}
7483
})();
7584

76-
</script>
7785
```
7886
7987
@@ -206,3 +214,33 @@ See the example below.
206214
}
207215
}
208216
```
217+
218+
219+
## Error Handling
220+
221+
Utilize standard error handling practices as shown in the above example.
222+
223+
Wrap any synchronous calls in a try/catch handler, and ensure to include the .catch() method on any async calls.
224+
225+
```javascript
226+
227+
//Asynchronous error handling
228+
client
229+
.getIpGeolocationFull({ip:'8.8.8.8'})
230+
.then((jsonResult=> {
231+
//success
232+
})
233+
.catch(function(error) {
234+
console.error('Asynchronous "then" error:', error);
235+
});
236+
237+
//Synchronous error handling
238+
(async function() {
239+
try {
240+
//success
241+
} catch (error) {
242+
console.error('Asynchronous "await" error:', error);
243+
}
244+
})();
245+
246+
```

demo.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const client = require('./index')('XXX'); // XXX being your api key found at: https://www.bigdatacloud.net/customer/account
2+
3+
/*
4+
* All api endpoints can be accessed via magic methods in the following camelised format:
5+
* method | endpoint
6+
* For example: an asynchronous "GET" call to the "ip-geolocation-full" endpoint would be: client.getIpGeolocationFull();
7+
* All endpoints return a promise
8+
*/
9+
10+
//Asynchronous example using 'then':
11+
client
12+
.getIpGeolocationFull({ip:'8.8.8.8'})
13+
.then(jsonResult => {
14+
console.log('Asynchronous "then" result:',jsonResult);
15+
}).catch(exception => {
16+
console.log(exception);
17+
});
18+
19+
//Asynchronous example using 'await':
20+
(async () => {
21+
try {
22+
var jsonResult = await client.getIpGeolocationFull({ip:'8.8.8.8'});
23+
console.log('Asynchronous "await" result:',jsonResult);
24+
} catch (error) {
25+
console.error('Asynchronous "await" error:', error);
26+
}
27+
})();

index.js

+21-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const request = require('request');
1+
const fetch = require('node-fetch');
22

33
class Client {
44
constructor(apiKey, nameSpace, server) {
@@ -8,13 +8,13 @@ class Client {
88
this.server=server ? server : 'api.bigdatacloud.net';
99

1010
return new Proxy(this,{
11-
get:function(t,p) {
11+
get:(t,p) => {
1212
if (typeof t[p]!=='undefined') return t[p];
13-
return function(params) {
13+
return params => {
1414
var key=p;
1515
var method='GET';
1616

17-
key=key.replace(/([A-Z])/g,function(m,c,o,i) {
17+
key=key.replace(/([A-Z])/g,(m,c,o,i) => {
1818
return '-'+c.toLowerCase();
1919
});
2020
key=key.trim('-');
@@ -42,7 +42,7 @@ class Client {
4242
});
4343
}
4444

45-
async communicate(endpoint,method,payload) {
45+
communicate(endpoint,method,payload) {
4646
var qs=[];
4747
var data=false;
4848
var hasKey=false;
@@ -71,23 +71,30 @@ class Client {
7171
}
7272

7373
talk(method,url,data) {
74-
return new Promise((resolve, reject) => {
75-
var payload={url:url,json:true,method:method};
74+
return new Promise(async (resolve, reject) => {
75+
var payload={method:method};
7676
if (method=='POST' || method=='PUT' || method=='PATCH') {
7777
payload.headers={'content-type' : 'application/x-www-form-urlencoded'};
7878
}
7979
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);
80+
try {
81+
const res=await fetch(url,payload);
82+
var json=await res.json();
83+
if (!res.ok) {
84+
return reject({error:json,code:res.status});
8485
}
85-
resolve(body);
86-
});
86+
if (json) {
87+
return resolve(json);
88+
}
89+
return reject({error:res.body,code:res.status});
90+
91+
} catch (e) {
92+
reject({error:e,code:0});
93+
}
8794
});
8895
}
8996
};
9097

91-
module.exports=function(apiKey,nameSpace,server) {
98+
module.exports=(apiKey,nameSpace,server) => {
9299
return new Client(apiKey,nameSpace,server);
93100
}

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bigdatacloudapi/client",
3-
"version": "1.0.1",
3+
"version": "1.1.1",
44
"description": "A NodeJS client for BigDataCloud API connectivity (https://www.bigdatacloud.net)",
55
"main": "index.js",
66
"repository": {
@@ -19,6 +19,6 @@
1919
},
2020
"homepage": "https://github.com/bigdatacloudapi/nodejs-api-client#readme",
2121
"dependencies": {
22-
"request": "^1.0.0"
22+
"node-fetch": "^2.6.1"
2323
}
2424
}

0 commit comments

Comments
 (0)