RestClient
Summary
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
Constructor
Item Index
Methods
Methods
authenticate
Syntax
Summary
Authenticates the client to the web service.
Returns:
Promise resolving when the client is authenticated, promise is rejected if authentication failed
buildRequest
Syntax
buildRequest
-
[options]
-
[body]
-
[timeout=10000]
-
[multiparted=false]
-
resolve
-
reject
Summary
Builds a request.
Parameters:
-
[options]
Object optionalThe list of http(s) options as described by NodeJS http.request documentation
-
[body]
Object | String optionalThe request body
-
[timeout=10000]
Number optionalMaximum execution time for the request (in ms), set it to Infinity for a request without limits
-
[multiparted=false]
Boolean optionaltrue to send body as multipart/form-data
-
resolve
FunctionThe function to call with request's result
-
reject
FunctionThe function to call if request fails
Returns:
The request, ready to be executed
delete
Syntax
Summary
Executes a DELETE request.
If client is not authenticated or access token has expired, a new authentication is automatically performed.
Parameters:
-
endPoint
StringThe web service end point to reach with query parameters
-
[options]
Object optionalThe list of http(s) options as described by NodeJS http.request documentation
-
[timeout=10000]
Number optionalMaximum execution time for the request (in ms), set it to Infinity for a request without limits
Returns:
Promise resolving with results as an Object
executeRequest
Syntax
executeRequest
-
method
-
endPoint
-
[options]
-
[body]
-
[timeout=10000]
-
[multiparted=false]
Summary
Executes a REST request after making sure the client is authenticated.
If client is not authenticated or access token has expired, a new authentication is automatically performed and request is retried.
Parameters:
-
method
StringThe HTTP method to use (either get, post, delete or put)
-
endPoint
StringThe web service end point to reach with query parameters
-
[options]
Object optionalThe list of http(s) options as described by NodeJS http.request documentation
-
[body]
Object | String optionalThe request body
-
[timeout=10000]
Number optionalMaximum execution time for the request (in ms), set it to Infinity for a request without limits
-
[multiparted=false]
Boolean optionaltrue to send body as multipart/form-data
Returns:
Promise resolving with request's response
get
Syntax
Summary
Executes a GET request.
If client is not authenticated or access token has expired, a new authentication is automatically performed.
Parameters:
-
endPoint
StringThe web service end point to reach with query parameters
-
[options]
Object optionalThe list of http(s) options as described by NodeJS http.request documentation
-
[timeout=10000]
Number optionalMaximum execution time for the request (in ms), set it to Infinity for a request without limits
Returns:
Promise resolving with result as an Object
getAuthenticationHeaders
Syntax
Summary
Gets the list of headers to send with each request.
Returns:
The list of headers to add to all requests sent to the server
getRequestHeaders
Syntax
Summary
Gets the list of headers to send with each request.
Returns:
The list of headers to add to all requests sent to the server
isAuthenticated
Syntax
Summary
Indicates if the client is authenticated to the web service or not.
Returns:
true if the client is authenticated, false otherwise
patch
Syntax
Summary
Executes a PATCH request.
If client is not authenticated or access token has expired, a new authentication is automatically performed.
Parameters:
-
endPoint
StringThe web service end point to reach with query parameters
-
[body]
Object | String optionalThe request body
-
[options]
Object optionalThe list of http(s) options as described by NodeJS http.request documentation
-
[timeout=10000]
Number optionalMaximum execution time for the request (in ms), set it to Infinity for a request without limits
-
[multiparted=false]
Boolean optionaltrue to send body as multipart/form-data
Returns:
Promise resolving with results as an Object
post
Syntax
Summary
Executes a POST request.
If client is not authenticated or access token has expired, a new authentication is automatically performed.
Parameters:
-
endPoint
StringThe web service end point to reach with query parameters
-
[body]
Object | String optionalThe request body
-
[options]
Object optionalThe list of http(s) options as described by NodeJS http.request documentation
-
[timeout=10000]
Number optionalMaximum execution time for the request (in ms), set it to Infinity for a request without limits
-
[multiparted=false]
Boolean optionaltrue to send body as multipart/form-data
Returns:
Promise resolving with results as an Object
put
Syntax
Summary
Executes a PUT request.
If client is not authenticated or access token has expired, a new authentication is automatically performed.
Parameters:
-
endPoint
StringThe web service end point to reach with query parameters
-
[body]
Object | String optionalThe request body
-
[options]
Object optionalThe list of http(s) options as described by NodeJS http.request documentation
-
[timeout=10000]
Number optionalMaximum execution time for the request (in ms), set it to Infinity for a request without limits
-
[multiparted=false]
Boolean optionaltrue to send body as multipart/form-data
Returns:
Promise resolving with results as an Object
Properties
certificate
Syntax
Summary
Path to the web service server certificate file.
maxAuthenticationAttempts
Syntax
maxAuthenticationAttempts
Number
Summary
Maximum number of authentication attempts to perform on a request in case of an invalid or expired token.
Default: 1
queuedRequests
Syntax
queuedRequests
Set
Summary
The collection of queued requests waiting to be executed.