RestBase Common Parameters

All the JavaScript objects extend from the RestBase class. There is some common configuration that can be passed into each of these objects during initialization.

Example—Common Configurations

Code Snippet
Copyvar _user = new finesse.restservices.User(options);
or
var _team = new finesse.restservices.Team(options);
or
var _media = new finesse.restservices.Media(options);

Example—Options

Code Snippet
Copyvar options = {
    id: 'someUniqueId',
    onLoad: function(restObj){},
    onChange: function(restObj){},
    onAdd: function(restObj){},
    onDelete: function(restObj){},
    onError: function(response){},
}
ParameterDescription

Example

id

An ID that uniquely identifies the JavaScript object.

For a User API, this is the id or username which uniquely identifies that User. Every REST API object has a unique identifier. You cannot generate this on the client-side.

Each JavaScript object is a representation of an already existing REST API object on the system. A user with that id is already present in the upstream system, and you are instantiating a JavaScript version of it on the client-side.

onLoad

A callback that is invoked one time in the life of the object, which is when the initialization is successful and the data is loaded into the JavaScript object successfully. This JavaScript object is then passed to the handler as a parameter.

This is equivalent to the success handler of a GET REST API request.

An example of a User JavaScript object loading:

Code Snippet
Copyvar _user = new finesse.restservices.User({
    id: '1001001',
    onLoad: function(user){
        // Do something on the successful fetch of User data.
        // For example get the full name of the agent by calling user.getFullName() and display it
        // or check if this user is a supervisor by calling user.hasSupervisorRole().
        // Check the JS Library for more of these useful APIs to play around with.
    }
});
onChange

A callback that is invoked upon the successful update of the object. The updated object is then passed to the handler as a parameter.

This is equivalent to the success handler of a PUT REST API request.

An example where the User's state changes and shows an alert to the user:

Code Snippet
Copyvar _user = new finesse.restservices.User({
    id: '1001001',
    onLoad: function(user){},
    onChange: function(user){
        // onChange will be triggered every time the User object updates.
        // check if the agent state is, say, 'NOT_READY'.
        if(user.getState() === 'NOT_READY') 
    }
});

In the above example, the Agent is alerted when the state changes to NOT_READY.

onAdd

A callback that is invoked when an object is created in the upstream system by doing a POST request from the client. The client receives a success response for the creation. The newly created object is passed to the handler as a parameter.

This is unlike the other scenarios where the JavaScript object is pre-existing in the system and you are creating a JavaScript version that creates a new object in the system.

Currently, there are no such instances where you are using the JavaScript object to create an object that is not present in the upstream system.

onDelete

A callback that is invoked when an object is deleted in the upstream system by doing a DELETE request from the client. The client receives a success response for the deletion.

Currently, there are no such instances where you are using the JavaScript object to delete an object that is there already in the upstream system.

onError

A callback that is invoked with the response object as the parameter when any of the operations such as, GET, PUT, POST and DELETE fails.

The following are the parameters:

  • status—{Number} Returns the HTTP status code.

  • content—{String} The raw string response.

  • object—{Object} The parsed object response.

  • error—The type of API error returned from the REST API request. It can be accessed using
    Code Snippet
    Copyrsp.object.ApiErrors.
    ApiError.ErrorType
    • errorType—{String} The type of error.

    • errorMessage—{String} The message that is associated with the error.

An example when the operations fail:

Code Snippet
Copyvar _user = new finesse.restservices.User({
    id: '1001001',
    onLoad: function(user){},
    onError: function(rsp){
        // log the error message to clientLogs with rsp.error.errorMessage
        // render a meaningful error message into the UI
    }
});