Constructor
new RestClient(webServiceUrl, certificateopt)
Creates a client to connect to REST web service.
It aims to facilitate implementation of a REST web service client. Requesting an end point, without being authenticated, will automatically execute the authenticateRequest first before calling the end point. If token expired, a new authentication is made automatically.
You MUST:
- Extend this class
- Define a authenticateRequest property with a Request as a value. This will be automatically called by the RestClient to get an access token from the server (response from server should contain the property access_token containing the access token which will be stored in RestClient accessToken property and used for all subsequent requests). Use buildRequest function to create the authenticate request
- Make sure that the web service server returns a property error_description set to "Token not found or expired" if token couln't be retrieved
- Make sure that the web service server returns a property error_description set to "Token already expired" if token has expired
You MAY:
- Override function getAuthenticationHeaders. By default the list of headers returned by getAuthenticationHeaders function will be added to all requests sent to the server. One of this header may be the authentication header for example
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
webServiceUrl |
String | The complete URL of the REST web service (with protocol and port) |
|
certificate |
String |
<optional> |
Absolute path to the web service server full chain certificate file |
- Source:
Throws:
-
Thrown if webServiceUrl is not a valid String
- Type
- TypeError
Methods
(private, static) rejectAll(requests, error)
Rejects all requests with the given error.
If the request is running, it will be aborted.
Parameters:
Name | Type | Description |
---|---|---|
requests |
Set | The list of requests to reject |
error |
Error | The reject's error |
- Source:
(async) delete(endPoint, optionsopt, timeoutopt) → {Promise}
Executes a DELETE request.
If client is not authenticated or access token has expired, a new authentication is automatically performed.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
endPoint |
String | The web service end point to reach with query parameters |
||
options |
Object |
<optional> |
The list of http(s) options as described by NodeJS http.request documentation |
|
timeout |
Number |
<optional> |
10000 | Maximum execution time for the request (in ms), set it to Infinity for a request without limits |
- Source:
Throws:
-
Thrown if endPoint is not valid a String
- Type
- TypeError
Returns:
Promise resolving with results as an Object
- Type
- Promise
(async) get(endPoint, optionsopt, timeoutopt) → {Promise}
Executes a GET request.
If client is not authenticated or access token has expired, a new authentication is automatically performed.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
endPoint |
String | The web service end point to reach with query parameters |
||
options |
Object |
<optional> |
The list of http(s) options as described by NodeJS http.request documentation |
|
timeout |
Number |
<optional> |
10000 | Maximum execution time for the request (in ms), set it to Infinity for a request without limits |
- Source:
Throws:
-
Thrown if endPoint is not valid a String
- Type
- TypeError
Returns:
Promise resolving with result as an Object
- Type
- Promise
getAuthenticationHeaders() → {Object}
Gets the list of headers to send with each request.
- Source:
Returns:
The list of headers to add to all requests sent to the server
- Type
- Object
(async) patch(endPoint, bodyopt, optionsopt, timeoutopt, multipartedopt) → {Promise}
Executes a PATCH request.
If client is not authenticated or access token has expired, a new authentication is automatically performed.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
endPoint |
String | The web service end point to reach with query parameters |
||
body |
Object | String |
<optional> |
The request body |
|
options |
Object |
<optional> |
The list of http(s) options as described by NodeJS http.request documentation |
|
timeout |
Number |
<optional> |
10000 | Maximum execution time for the request (in ms), set it to Infinity for a request without limits |
multiparted |
Boolean |
<optional> |
false | true to send body as multipart/form-data |
- Source:
Throws:
-
Thrown if endPoint is not valid a String
- Type
- TypeError
Returns:
Promise resolving with results as an Object
- Type
- Promise
(async) post(endPoint, bodyopt, optionsopt, timeoutopt, multipartedopt) → {Promise}
Executes a POST request.
If client is not authenticated or access token has expired, a new authentication is automatically performed.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
endPoint |
String | The web service end point to reach with query parameters |
||
body |
Object | String |
<optional> |
The request body |
|
options |
Object |
<optional> |
The list of http(s) options as described by NodeJS http.request documentation |
|
timeout |
Number |
<optional> |
10000 | Maximum execution time for the request (in ms), set it to Infinity for a request without limits |
multiparted |
Boolean |
<optional> |
false | true to send body as multipart/form-data |
- Source:
Throws:
-
Thrown if endPoint is not valid a String
- Type
- TypeError
Returns:
Promise resolving with results as an Object
- Type
- Promise
(async) put(endPoint, bodyopt, optionsopt, timeoutopt, multipartedopt) → {Promise}
Executes a PUT request.
If client is not authenticated or access token has expired, a new authentication is automatically performed.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
endPoint |
String | The web service end point to reach with query parameters |
||
body |
Object | String |
<optional> |
The request body |
|
options |
Object |
<optional> |
The list of http(s) options as described by NodeJS http.request documentation |
|
timeout |
Number |
<optional> |
10000 | Maximum execution time for the request (in ms), set it to Infinity for a request without limits |
multiparted |
Boolean |
<optional> |
false | true to send body as multipart/form-data |
- Source:
Throws:
-
Thrown if endPoint is not valid a String
- Type
- TypeError
Returns:
Promise resolving with results as an Object
- Type
- Promise