Skip to content

Commit cbb7474

Browse files
authored
Merge pull request #10 from marcinx/master
* replaces promise chains with async await for inactive HTTP request …
2 parents 5eaf1e2 + 51c2263 commit cbb7474

File tree

4 files changed

+107
-165
lines changed

4 files changed

+107
-165
lines changed

README.md

+36-62
Original file line numberDiff line numberDiff line change
@@ -33,46 +33,34 @@ Get your API key and secret [here](https://www.hlr-lookups.com/en/api-settings).
3333
Performs an authenticated request against the `GET /auth-test` endpoint. A status code of 200 indicates that you were able to authenticate using you [API credentials](https://www.hlr-lookups.com/en/api-settings#authSettings).
3434

3535
```javascript
36-
client.get('/auth-test',
37-
null,
38-
function (response) {
36+
let response = await client.get('/auth-test');
3937

40-
console.log(response.status);
41-
console.log(response.data);
38+
console.log(response.status);
39+
console.log(response.data);
4240

43-
if (response.status === 200) {
44-
// authentication was successful
45-
}
46-
47-
}
48-
);
41+
if (response.status === 200) {
42+
// authentication was successful
43+
}
4944
```
5045

5146
Get your API key and secret [here](https://www.hlr-lookups.com/en/api-settings).
5247

5348
**Submitting an HLR Lookup** ([HLR API Docs](https://www.hlr-lookups.com/en/api-docs#post-hlr-lookup))
5449

5550
```javascript
56-
client.post('/hlr-lookup',
57-
{
58-
msisdn: '+905536939460'
59-
},
60-
function(response) {
51+
let response = await client.post('/hlr-lookup', {msisdn: '+905536939460'});
6152

62-
// The API returns an HTTP status code of 200 if the request was successfully processed. Let's have a look.
63-
console.log('HLR Lookup Status Code', response.status);
64-
console.log('HLR Lookup Response Body', response.data);
53+
// The API returns an HTTP status code of 200 if the request was successfully processed. Let's have a look.
54+
console.log('HLR Lookup Status Code', response.status);
55+
console.log('HLR Lookup Response Body', response.data);
6556

66-
if (response.status !== 200) {
67-
console.log('Received non-OK status code from server.');
68-
return;
69-
}
70-
71-
let data = response.data;
72-
// do something with the data
57+
if (response.status !== 200) {
58+
console.log('Received non-OK status code from server.');
59+
return;
60+
}
7361

74-
}
75-
);
62+
let data = response.data;
63+
// do something with the data
7664
```
7765

7866
The HLR API Response Object:
@@ -117,26 +105,19 @@ A detailed documentation of the attributes and connectivity statuses in the HLR
117105
**Submitting a Number Type (NT) Lookup** ([NT API Docs](https://www.hlr-lookups.com/en/api-docs#post-nt-lookup))
118106

119107
```javascript
120-
client.post('/nt-lookup',
121-
{
122-
number: '+4989702626'
123-
},
124-
function(response) {
108+
let response = await client.post('/nt-lookup', {number: '+4989702626'});
125109

126-
// The API returns an HTTP status code of 200 if the request was successfully processed. Let's have a look.
127-
console.log('NT Lookup Status Code', response.status);
128-
console.log('NT Lookup Response Body', response.data);
110+
// The API returns an HTTP status code of 200 if the request was successfully processed. Let's have a look.
111+
console.log('NT Lookup Status Code', response.status);
112+
console.log('NT Lookup Response Body', response.data);
129113

130-
if (response.status !== 200) {
131-
console.log('Received non-OK status code from server.');
132-
return;
133-
}
134-
135-
let data = response.data;
136-
// do something with the data
114+
if (response.status !== 200) {
115+
console.log('Received non-OK status code from server.');
116+
return;
117+
}
137118

138-
}
139-
);
119+
let data = response.data;
120+
// do something with the data
140121
```
141122

142123
The NT API Response Object:
@@ -177,26 +158,19 @@ A detailed documentation of the attributes and connectivity statuses in the NT A
177158
**Submitting an MNP Lookup (Mobile Number Portability)** ([MNP API Docs](https://www.hlr-lookups.com/en/api-docs#post-mnp-lookup))
178159

179160
```javascript
180-
client.post('/mnp-lookup',
181-
{
182-
msisdn: '+14156226819'
183-
},
184-
function(response) {
161+
let response = await client.post('/mnp-lookup', {msisdn: '+14156226819'});
185162

186-
// The API returns an HTTP status code of 200 if the request was successfully processed. Let's have a look.
187-
console.log('MNP Lookup Status Code', response.status);
188-
console.log('MNP Lookup Response Body', response.data);
163+
// The API returns an HTTP status code of 200 if the request was successfully processed. Let's have a look.
164+
console.log('MNP Lookup Status Code', response.status);
165+
console.log('MNP Lookup Response Body', response.data);
189166

190-
if (response.status !== 200) {
191-
console.log('Received non-OK status code from server.');
192-
return;
193-
}
194-
195-
let data = response.data;
196-
// do something with the data
167+
if (response.status !== 200) {
168+
console.log('Received non-OK status code from server.');
169+
return;
170+
}
197171

198-
}
199-
);
172+
let data = response.data;
173+
// do something with the data
200174
```
201175

202176
The MNP API Response Object:

src/examples/examples.js

+49-67
Original file line numberDiff line numberDiff line change
@@ -17,89 +17,71 @@ const client = new HlrLookupClient(
1717
'YOUR-API-SECRET'
1818
);
1919

20-
/**
21-
Invoke a request to GET /auth-test (https://www.hlr-lookups.com/en/api-docs#get-auth-test) to see if everything worked
22-
*/
23-
client.get('/auth-test',
24-
null,
25-
function(response) {
26-
// The API returns an HTTP status code of 200 if the request was successfully processed. Let's have a look.
27-
console.log(response.status);
28-
console.log(response.data);
29-
}
30-
);
20+
async function main() {
3121

32-
/**
33-
* Submit an HLR Lookup via POST /hlr-lookup (https://www.hlr-lookups.com/en/api-docs#post-hlr-lookup)
34-
*/
35-
client.post('/hlr-lookup',
36-
{
37-
msisdn: '+905536939460'
38-
},
39-
function(response) {
22+
/**
23+
Invoke a request to GET /auth-test (https://www.hlr-lookups.com/en/api-docs#get-auth-test) to see if everything worked
24+
*/
25+
let response = await client.get('/auth-test');
4026

41-
// The API returns an HTTP status code of 200 if the request was successfully processed. Let's have a look.
42-
console.log('HLR Lookup Status Code', response.status);
43-
console.log('HLR Lookup Response Body', response.data);
27+
// The API returns an HTTP status code of 200 if the request was successfully processed. Let's have a look.
28+
console.log(response.status);
29+
console.log(response.data);
4430

45-
if (response.status !== 200) {
46-
console.log('Received non-OK status code from server.');
47-
return;
48-
}
31+
/**
32+
* Submit an HLR Lookup via POST /hlr-lookup (https://www.hlr-lookups.com/en/api-docs#post-hlr-lookup)
33+
*/
34+
response = await client.post('/hlr-lookup', {msisdn: '+905536939460'});
4935

50-
let data = response.data;
51-
// do something with the data
36+
// The API returns an HTTP status code of 200 if the request was successfully processed. Let's have a look.
37+
console.log('HLR Lookup Status Code', response.status);
38+
console.log('HLR Lookup Response Body', response.data);
5239

40+
if (response.status !== 200) {
41+
console.log('Received non-OK status code from server.');
42+
return;
5343
}
54-
);
55-
56-
/**
57-
* Submit an NT Lookup via POST /nt-lookup (https://www.hlr-lookups.com/en/api-docs#post-nt-lookup)
58-
*/
59-
client.post('/nt-lookup',
60-
{
61-
number: '+4989702626'
62-
},
63-
function(response) {
6444

65-
// The API returns an HTTP status code of 200 if the request was successfully processed. Let's have a look.
66-
console.log('NT Lookup Status Code', response.status);
67-
console.log('NT Lookup Response Body', response.data);
45+
let data = response.data;
46+
// do something with the data
6847

69-
if (response.status !== 200) {
70-
console.log('Received non-OK status code from server.');
71-
return;
72-
}
48+
/**
49+
* Submit an NT Lookup via POST /nt-lookup (https://www.hlr-lookups.com/en/api-docs#post-nt-lookup)
50+
*/
51+
response = await client.post('/nt-lookup', {number: '+4989702626'});
7352

74-
let data = response.data;
75-
// do something with the data
53+
// The API returns an HTTP status code of 200 if the request was successfully processed. Let's have a look.
54+
console.log('NT Lookup Status Code', response.status);
55+
console.log('NT Lookup Response Body', response.data);
7656

57+
if (response.status !== 200) {
58+
console.log('Received non-OK status code from server.');
59+
return;
7760
}
78-
);
79-
80-
/**
81-
* Submit an MNP Lookup via POST /mnp-lookup (https://www.hlr-lookups.com/en/api-docs#post-mnp-lookup)
82-
*/
83-
client.post('/mnp-lookup',
84-
{
85-
msisdn: '+14156226819'
86-
},
87-
function(response) {
8861

89-
// The API returns an HTTP status code of 200 if the request was successfully processed. Let's have a look.
90-
console.log('MNP Lookup Status Code', response.status);
91-
console.log('MNP Lookup Response Body', response.data);
62+
data = response.data;
63+
// do something with the data
9264

93-
if (response.status !== 200) {
94-
console.log('Received non-OK status code from server.');
95-
return;
96-
}
65+
/**
66+
* Submit an MNP Lookup via POST /mnp-lookup (https://www.hlr-lookups.com/en/api-docs#post-mnp-lookup)
67+
*/
68+
response = await client.post('/mnp-lookup', {msisdn: '+14156226819'});
9769

98-
let data = response.data;
99-
// do something with the data
70+
// The API returns an HTTP status code of 200 if the request was successfully processed. Let's have a look.
71+
console.log('MNP Lookup Status Code', response.status);
72+
console.log('MNP Lookup Response Body', response.data);
10073

74+
if (response.status !== 200) {
75+
console.log('Received non-OK status code from server.');
76+
return;
10177
}
102-
);
78+
79+
data = response.data;
80+
// do something with the data
81+
82+
}
83+
84+
main();
10385

10486

10587

src/lib/index.js

+21-35
Original file line numberDiff line numberDiff line change
@@ -33,47 +33,43 @@ function Client(key, secret) {
3333
*
3434
* @param endpoint (string), e.g. /auth-test
3535
* @param params (object), a list of GET parameters to be included in the request (or null if none)
36-
* @param callback (function), processes the response
3736
*/
38-
this.get = function(endpoint, params, callback) {
39-
sendRequest('get', endpoint, params, callback);
37+
this.get = async function(endpoint, params) {
38+
return await sendRequest('get', endpoint, params);
4039
};
4140

4241
/**
4342
* Use this method to communicate with POST endpoints
4443
*
4544
* @param endpoint (string), e.g. /hlr-lookup
4645
* @param params (object), a list of POST parameters to be included in the request
47-
* @param callback (function), processes the response
4846
*/
49-
this.post = function(endpoint, params, callback) {
50-
sendRequest('post', endpoint, params, callback);
47+
this.post = async function(endpoint, params) {
48+
return await sendRequest('post', endpoint, params);
5149
};
5250

5351
/**
5452
* Use this method to communicate with PUT endpoints
5553
*
5654
* @param endpoint (string)
5755
* @param params (object), a list of PUT parameters to be included in the request
58-
* @param callback (function), processes the response
5956
*/
60-
this.put = function(endpoint, params, callback) {
61-
sendRequest('put', endpoint, params, callback);
57+
this.put = async function(endpoint, params) {
58+
return await sendRequest('put', endpoint, params);
6259
};
6360

6461
/**
6562
* Use this method to communicate with DELETE endpoints
6663
*
6764
* @param endpoint (string)
6865
* @param params (object), a list of DELETE parameters to be included in the request
69-
* @param callback (function), processes the response
7066
*/
71-
this.delete = function(endpoint, params, callback) {
72-
sendRequest('delete', endpoint, params, callback);
67+
this.delete = async function(endpoint, params) {
68+
return await sendRequest('delete', endpoint, params);
7369
};
7470

7571
// private validator function
76-
const validateArgs = function(method, endpoint, params, callback) {
72+
const validateArgs = function(method, endpoint, params) {
7773

7874
if (!validateEndpoint(endpoint)) {
7975
this.log("Invalid endpoint given to " + method + " method.");
@@ -85,11 +81,6 @@ function Client(key, secret) {
8581
return false;
8682
}
8783

88-
if (!validateCallback(callback)) {
89-
this.log("Invalid callback given to " + method + " method.");
90-
return false;
91-
}
92-
9384
return true;
9485

9586
}.bind(this);
@@ -110,11 +101,6 @@ function Client(key, secret) {
110101

111102
};
112103

113-
// private validator function
114-
const validateCallback = function(callback) {
115-
return typeof callback === 'function';
116-
};
117-
118104
// private function to build request configuration
119105
const buildConfig = function(extras) {
120106

@@ -139,23 +125,23 @@ function Client(key, secret) {
139125
}.bind(this);
140126

141127
// private function to send the request
142-
const sendRequest = function(method, endpoint, params, callback) {
128+
const sendRequest = async function(method, endpoint, params) {
143129

144-
if (!validateArgs(method, endpoint, params, callback)) {
130+
if (!validateArgs(method, endpoint, params)) {
145131
return;
146132
}
147133

148-
axios(buildConfig({
149-
method: method,
150-
url: endpoint,
151-
params: method === 'get' ? params : null,
152-
data: method === 'get' ? null : params
153-
}))
154-
.then(callback)
155-
.catch(function(e) {
134+
try {
135+
return await axios(buildConfig({
136+
method: method,
137+
url: endpoint,
138+
params: method === 'get' ? params : null,
139+
data: method === 'get' ? null : params
140+
}));
141+
} catch (e) {
156142
this.log(JSON.stringify(e));
157-
callback(e.response);
158-
}.bind(this));
143+
return e.response;
144+
}
159145

160146
}.bind(this);
161147

0 commit comments

Comments
 (0)