Skip to content

Commit

Permalink
Removal of 'request' module requirement, improved documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Cameron James committed Jul 25, 2021
1 parent edb40e7 commit 1c256eb
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
test.js
42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ For documentation specific to this api client, please read below.
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).


## Update Notes

- This repository now utilises `node-fetch` rather than the deprecated `request` module.
- Calls to the API will return a JSON object as a successful response, see below for an example
- Exceptions return a single object: `{error:json_object || error_string,code:http_status_code}`



## Authentication / Identification

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

## Example usage

The below example is found in the included demo.js.

```javascript
const client = require('@bigdatacloudapi/client')('XXX'); // XXX being your api key found at: https://www.bigdatacloud.net/customer/account

Expand All @@ -56,7 +65,7 @@ See the example below.
//Asynchronous example using 'then':
client
.getIpGeolocationFull({ip:'8.8.8.8'})
.then(function(jsonResult) {
.then((jsonResult=> {
console.log('Asynchronous "then" result:',jsonResult);
})
.catch(function(error) {
Expand All @@ -73,7 +82,6 @@ See the example below.
}
})();

</script>
```
Expand Down Expand Up @@ -206,3 +214,33 @@ See the example below.
}
}
```
## Error Handling
Utilize standard error handling practices as shown in the above example.
Wrap any synchronous calls in a try/catch handler, and ensure to include the .catch() method on any async calls.
```javascript

//Asynchronous error handling
client
.getIpGeolocationFull({ip:'8.8.8.8'})
.then((jsonResult=> {
//success
})
.catch(function(error) {
console.error('Asynchronous "then" error:', error);
});

//Synchronous error handling
(async function() {
try {
//success
} catch (error) {
console.error('Asynchronous "await" error:', error);
}
})();

```
27 changes: 27 additions & 0 deletions demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const client = require('./index')('XXX'); // XXX being your api key found at: https://www.bigdatacloud.net/customer/account

/*
* All api endpoints can be accessed via magic methods in the following camelised format:
* method | endpoint
* For example: an asynchronous "GET" call to the "ip-geolocation-full" endpoint would be: client.getIpGeolocationFull();
* All endpoints return a promise
*/

//Asynchronous example using 'then':
client
.getIpGeolocationFull({ip:'8.8.8.8'})
.then(jsonResult => {
console.log('Asynchronous "then" result:',jsonResult);
}).catch(exception => {
console.log(exception);
});

//Asynchronous example using 'await':
(async () => {
try {
var jsonResult = await client.getIpGeolocationFull({ip:'8.8.8.8'});
console.log('Asynchronous "await" result:',jsonResult);
} catch (error) {
console.error('Asynchronous "await" error:', error);
}
})();
35 changes: 21 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const request = require('request');
const fetch = require('node-fetch');

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

return new Proxy(this,{
get:function(t,p) {
get:(t,p) => {
if (typeof t[p]!=='undefined') return t[p];
return function(params) {
return params => {
var key=p;
var method='GET';

key=key.replace(/([A-Z])/g,function(m,c,o,i) {
key=key.replace(/([A-Z])/g,(m,c,o,i) => {
return '-'+c.toLowerCase();
});
key=key.trim('-');
Expand Down Expand Up @@ -42,7 +42,7 @@ class Client {
});
}

async communicate(endpoint,method,payload) {
communicate(endpoint,method,payload) {
var qs=[];
var data=false;
var hasKey=false;
Expand Down Expand Up @@ -71,23 +71,30 @@ class Client {
}

talk(method,url,data) {
return new Promise((resolve, reject) => {
var payload={url:url,json:true,method:method};
return new Promise(async (resolve, reject) => {
var payload={method:method};
if (method=='POST' || method=='PUT' || method=='PATCH') {
payload.headers={'content-type' : 'application/x-www-form-urlencoded'};
}
if (data) payload.body=data;
request(payload, (error, response, body) => {
if (error) reject(error,0);
if (response.statusCode != 200) {
reject(body,code);
try {
const res=await fetch(url,payload);
var json=await res.json();
if (!res.ok) {
return reject({error:json,code:res.status});
}
resolve(body);
});
if (json) {
return resolve(json);
}
return reject({error:res.body,code:res.status});

} catch (e) {
reject({error:e,code:0});
}
});
}
};

module.exports=function(apiKey,nameSpace,server) {
module.exports=(apiKey,nameSpace,server) => {
return new Client(apiKey,nameSpace,server);
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bigdatacloudapi/client",
"version": "1.0.1",
"version": "1.1.1",
"description": "A NodeJS client for BigDataCloud API connectivity (https://www.bigdatacloud.net)",
"main": "index.js",
"repository": {
Expand All @@ -19,6 +19,6 @@
},
"homepage": "https://github.com/bigdatacloudapi/nodejs-api-client#readme",
"dependencies": {
"request": "^1.0.0"
"node-fetch": "^2.6.1"
}
}

0 comments on commit 1c256eb

Please sign in to comment.