eLabSDK.API.Call Class
Builder class that builds an XMLHttpRequest call from various API parameters and executes it.
Constructor
eLabSDK.API.Call
(
-
baseURL
-
version
-
method
-
responseType
-
token
-
path
-
pathParams
-
queryParams
-
page
-
records
-
expand
-
sort
-
onSuccess
-
onError
-
onProgress
Parameters:
-
baseURL
String- The base URL of the eLAB server. Defaults to '/api'. You can do a CORS request to another eLAB by setting it to e.g. 'https://my-elab-server.com/api'.
-
version
String- The major version string of the API call. Default 'v1'.
-
method
String- The HTTP Method of the call: GET, POST, PUT, PATCH or DELETE. Default 'GET'.
-
responseType
String- The response type that you receive in the onsuccess event: json, text, blob or arraybuffer. Default: 'json'. For file downloads set it to 'blob' or 'arraybuffer'. If you set it to 'text' then the response won't be parsed as JSON; you'll receive a plain string.
-
token
String- The API token to use for authorization. Note that this is not required if the user is currently logged in on eLABJournal.
-
path
String- The relative path (a.k.a. route) of the API call without 'api/v1/' in front of it. It may contain parameter templates which will be filled in using pathParams. Example: 'samples/{sampleID}', with the sampleID defined in pathParams.
-
pathParams
Object- If the path has parameter templates then you must define them here. Example: { sampleID: 123 }.
-
queryParams
Object- Any key/value pair defined here will be added as query parameters. Example: { search: 'MFP' } will add ?search='MFP' to the call. The keys and values will be URI encoded by the builder.
-
page
Int- Adds $page=... to the call for a paged request.
-
records
Int- Adds $records=... to the call for a paged request.
-
expand
String/array- Adds $expand=... to the call. This can either be a single string or a string array which will be built as a comma-separated list. The values will be URI encoded by the builder.
-
sort
String/object/array- Adds $sort=... to the call. This can be a single string, an array of strings or an object/array of objects with 'field' and 'direction' keys. Example: { field: 'name', direction: 'desc' }. The values will be URI encoded by the builder.
-
onSuccess
Function- Parameters: xhr, status, response. Called when the call resulted in a status 200, 201 or 204. In case of a 200 the response parameter contains the response body in the format that was specified by responseType. Otherwise it will be undefined.
-
onError
Function- Parameters: xhr, status, response. Called when an error occurs, either a network error (status=0) or the call returned a status that indicates an error. In the latter case, response may contain a JSON object with an error message.
-
onProgress
Function- Parameters: progressEvent. Called only when you upload a binary (blob or arraybuffer) in the execute function. progressEvent is the event object from XMLHttpRequest.
Example:
new eLabSDK.API.Call({
method: 'GET',
path: 'storageLayers/{storageLayerID}/reservations',
pathParams: {
storageLayerID: 123
},
queryParams: {
from: '2018-02-08T09:00:00Z'
},
page: 0,
records: 20,
sort: {
field: 'name',
direction: 'desc'
},
onSuccess: function (xhr, status, response) {
handleResponse(response);
},
onError: function (xhr, status, response) {
if (response) {
showMessage(response.message);
}
}
}).execute();
new eLabSDK.API.Call({
path: 'samples/{sampleID}',
pathParams: {
sampleID: 123
},
expand: ['location', 'meta', 'quantity'],
onSuccess: function (xhr, status, response) {
handleResponse(response);
}
}).execute();
new eLabSDK.API.Call({
method: 'POST',
path: 'samples',
onSuccess: function (xhr, status, response) {
// response is an integer indicating the new sample's ID.
var newSampleID = response;
}
}).execute({
sampleTypeID: 123,
name: 'My sample'
});
new eLabSDK.API.Call({
method: 'GET',
path: 'noteImages/{noteImageID}',
pathParams: {
noteImageID: 123
},
responseType: 'blob',
onSuccess: function (xhr, status, response) {
// response is a blob containing the image.
var img = document.createElement('img');
img.onload = function (e) {
window.URL.revokeObjectURL(img.src); // Clean up after yourself.
};
img.src = window.URL.createObjectURL(response);
document.body.appendChild(img);
}
}).execute();
new eLabSDK.API.Call({
method: 'POST',
path: 'noteImages',
queryParams: {
fileName: 'My image.jpg'
},
onSuccess: function (xhr, status, response) {
var newNoteImageID = response;
},
onProgress: function (e) {
if (e.lengthComputable) {
var progress = (e.loaded / e.total) * 100;
}
},
}).execute(file); // where file is a Blob or File with the image data.
Item Index
Methods
Methods
execute
(
XMLHttpRequest
-
body
Builds and executes the API call.
Parameters:
-
body
Object/Blob/ArrayBuffer- The body of the request. It can either be an object which is serialized as JSON, or a Blob or ArrayBuffer which is uploaded as a file. The Content-Type is automatically determined.
Returns:
XMLHttpRequest:
The XHR object which was used to execute the call.