You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Constructor for our base client class. This class is used to act as a base layer for all HTTP calls. It uses the request promise module to achieve the same. This class is primaraly meant to act as a base class which can be further extended to build clients for third party APIs.
16
+
*
17
+
* @param {Object} options - Following parameters are supported and can be passed as options :
18
+
* @param {String} options.ENDPOINT - Hostname / Endpoint for all our client requests
* @param {String} [options.CLIENT_NAME = 'BASE-CLIENT'] - Name of our client
21
+
* @param {String} [options.LOGTAG = '[clients/base]'] - Tag for logging purposes
22
+
*
23
+
* @throws {Error} Please note that some input parameters for this class are mandatory and hence
24
+
* is being validated within the constructor. Hence it is advised to wrap the object creation for
25
+
* this class in a try catch block to catch any synchronous errors being thrown from while creating
26
+
* this object.
27
+
*
28
+
*/
29
+
constructor({
30
+
ENDPOINT,
31
+
TIMEOUT=20*1000,
32
+
CLIENT_NAME='BASE-CLIENT',
33
+
LOGTAG='[clients/base]',
34
+
}={}){
35
+
this.LOGTAG=LOGTAG;
36
+
if(!LibUtils.isValidString(ENDPOINT)){
37
+
consterr=newTypeError(`Provided endpoint : ${ENDPOINT} is of invalid type`);
38
+
debug(`${this.LOGTAG} : Error :`,err);
39
+
throwerr;
40
+
}
41
+
42
+
this.ENDPOINT=ENDPOINT;
43
+
this.TIMEOUT=TIMEOUT;
44
+
this.CLIENT_NAME=CLIENT_NAME;
45
+
}
46
+
47
+
/**
48
+
*
49
+
* Function which taken in request options and hits HTTP request via requestPromise module. It expects following parameters
50
+
*
51
+
* @param {Object} requestOpts - the HTTP request opts which is needed to hit the request.
52
+
*
53
+
* @returns {Promise.<Object|Error>} - Returns a promise which either resolves to the required
54
+
* response body or rejects with the corresponding error.
55
+
*
56
+
* @private
57
+
*/
58
+
async_hitHttpRequest(requestOpts){
59
+
debug(`${this.LOGTAG} : Going to hit HTTP request with options : ${JSON.stringify(requestOpts)}`);
60
+
61
+
try{
62
+
constresponse=awaitrequestPromise(requestOpts);
63
+
debug(`${this.LOGTAG} : Success Response Body :`,requestOpts&&requestOpts.resolveWithFullResponse ? JSON.stringify(response.body) : JSON.stringify(response));
64
+
returnresponse;
65
+
}catch(err){
66
+
debug(`${this.LOGTAG} : Error occurred while hitting HTTP request with options : ${JSON.stringify(requestOpts)}`,err);
67
+
throw(err&&err.error)||err;
68
+
}
69
+
}
70
+
71
+
/**
72
+
*
73
+
* Function which taken in apiPath and other parameters to generate the request options for hitting the API request. It expects following input parameters :
74
+
*
75
+
* @param {String} apiPath - The API path for our client. Ex : /v1/some/sample/route
76
+
* @param {Object} requestOpts - Following are supported as the parameters which are needed to hit the request.
77
+
* @param {String} requestOpts.method - HTTP method, ex : GET, POST, PUT, DELETE, etc.
0 commit comments