-
Notifications
You must be signed in to change notification settings - Fork 19
Request Service
The RequestService module is a shared service which simplifies the HTTP requests for other modules and extensions. It will for example automatically add the authentication information if a user is logged in.
The RequestService is part of the project's Module
s which are imported by default. It should be instanciated once and shared among the services that need it. The constructor of the service doesn't need any configuration.
Here is an example of using the RequestService module to perform a GET request :
const url = 'http://localhost:5000/document';
const result = await requestService.request('GET', url);
Another example that performs a POST with a form data as body :
//retrieve data from an html form
const form = document.getElementById('my-form');
const formData = new FormData(form);
//request
const req = await requestService.request('POST', url, {
body: formData
});
const json = JSON.parse(req.response);
The request
method takes two mandatory arguments. Optional parameters are passed in the options
arguments :
request = function (method, url, options)
Note that the method
send(method, url, body, auth)
still exists in the service for legacy support, but its use is depreciated.
Here is a basic description of the mandatory arguments :
Argument | Type | Description |
---|---|---|
method | string | The HTTP method. Accepted values are 'GET', 'PUT', 'POST', 'UPDATE', 'DELETE'. Depending on the method, the body will be taken into account. |
url | string | The requested URL. |
The options
argument is a dictionary. Supported keys are listed below :
Key | Value type | Description |
---|---|---|
body |
Document or BodyInit
|
This argument can be of any type that can be sent through the XMLHttpRequest 's send method. Common types include strings or FormData . See the MDN reference for more information. |
authenticate | boolean | Specifies if the request should use authentication. It only works if the Authentication module is loaded by the app. By default, its value equals true so all requests will fail if no user is logged in. To bypass this kind of restriction, you can set this parameter to false . Example : requestService.request('POST', url, {body: data, authenticate: false}) will perform the request even if no user is logged in. |
responseType | string | Specifies the type of data contained in the response. See the MDN reference for more information. |
In order for the Request service to use the authentication, the Authentication module must be loaded by the app. In the main file of the app, an AuthenticationService must be instanciated first, then passed as an argument for the RequestService.
//First, we create a RequestService that will be used by the various services later
let requestService = new udvcore.RequestService();
//Then, we create the AuthenticationService. It needs the requestService to perform the login and register operations.
let authenticationService = new udvcore.AuthenticationService(requestService, config);
//Finally, we tell the RequestService to use authentication
requestService.setAuthenticationService(authenticationService);
The use of an authentication service will influence the RequestService's behavior in two ways :
- The requests will be denied by default if no user is logged in (this can be bypassed with the
authenticate
parameter). - The user's JWT will be added into the requests' header as an
authorization
parameter, in accordance to the OAuth2 protocol.
The goal of this service is to be used as the default way to perform requests through the project. As existing modules already perform requests to retrieve data, we'll describe in this section the process of adapting a module to use the RequestService.
In order to use the service, a module must have access to an instante of it. The recommended way of doing so is through dependency injection (see the wikipedia page). This will guaranty that only one instance of the service is used through the session, avoiding odd bugs such as having one service with authentication and another one without authentication. To do that, we first need to take a RequestService as a parameter of the module :
export SomeModule = function(requestService, /* other parameters */) {
this.requestService = requestService;
//Rest of the code
}
And we need to pass the service during the instanciation of the module :
let requestService = /* Instanciation of RequestService (and potentially AuthenticationService) as seen above */
let someModule = new SomeModule(requestService, /* other arguments */);
Now our module is ready to use the service.
All the requests in the service, that are made with JQuery or XMLHttpRequest, can be replaced with calls to requestService.send()
.
This replacement still has to be done in the existing modules and extensions :
- ConsultDoc
- GuidedTour
- Contribute