From 1c256eb098f817538d456f62cec566143e742b52 Mon Sep 17 00:00:00 2001 From: Cameron James Date: Sun, 25 Jul 2021 05:32:06 +0000 Subject: [PATCH] Removal of 'request' module requirement, improved documentation --- .gitignore | 2 ++ README.md | 42 ++++++++++++++++++++++++++++++++++++++++-- demo.js | 27 +++++++++++++++++++++++++++ index.js | 35 +++++++++++++++++++++-------------- package.json | 4 ++-- 5 files changed, 92 insertions(+), 18 deletions(-) create mode 100644 .gitignore create mode 100644 demo.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..86a0cd8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +test.js \ No newline at end of file diff --git a/README.md b/README.md index 13dbc56..b37edc9 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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) { @@ -73,7 +82,6 @@ See the example below. } })(); - ``` @@ -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); + } + })(); + +``` \ No newline at end of file diff --git a/demo.js b/demo.js new file mode 100644 index 0000000..278b503 --- /dev/null +++ b/demo.js @@ -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); + } +})(); \ No newline at end of file diff --git a/index.js b/index.js index d0cf4cc..4d40d0c 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -const request = require('request'); +const fetch = require('node-fetch'); class Client { constructor(apiKey, nameSpace, server) { @@ -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('-'); @@ -42,7 +42,7 @@ class Client { }); } - async communicate(endpoint,method,payload) { + communicate(endpoint,method,payload) { var qs=[]; var data=false; var hasKey=false; @@ -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); } \ No newline at end of file diff --git a/package.json b/package.json index c71e579..833f398 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -19,6 +19,6 @@ }, "homepage": "https://github.com/bigdatacloudapi/nodejs-api-client#readme", "dependencies": { - "request": "^1.0.0" + "node-fetch": "^2.6.1" } }