Class: RestOperation(eventChannel)

Parameters:
Name Required Type Default Value Description
eventChannel Y EventEmitter   common node event channel for all operations

RestOperation Methods

(instance) -> setUri(uri)

Sets the uri for this RestOperation object

Parameters:
Name Required Type Default Value Description
uri Y url   uri to use requests sent using this RestOperation object will be sent to the provided uri

Returns:
Type Description
restOperation this value for chaining

(instance) -> getReferer()

gets the’referer’ header field for this RestOperation object

Returns:
Type Description
string referer in incoming message

Examples:

MyWorker.prototype.onGet = function(restOperation) {
  var value = restOperation.getReferer();
  this.logger.info("HTTP Referrer: " + value);
}

(instance) -> setReferer(referer)

sets the referer for this RestOperation object

Parameters:
Name Required Type Default Value Description
referer Y string   to use in outgoing messages requests sent using this RestOperation object will be sent to with the referer set to the provided value

Returns:
Type Description
restOperation this value for chaining

(instance) -> getUri()

gets the uri for this RestOperation object

Returns:
Type Description
uri uri used in incoming message

Examples:

MyWorker.prototype.onGet = function(restOperation) {
  var value = restOperation.getUri();
  this.logger.info("HTTP URI: " + value);
}

(instance) -> getBody()

gets the request body for this RestOperation object

Returns:
Type Description
object body the body of the message

Examples:

MyWorker.prototype.onPut = function(restOperation) {
  // this code will extract the content of "SomeData" field in an incoming REST PUT request, e.g. if the incoming JSON is
  //{
  //  SomeData: "Hello",
  //  MoreData: "Bye"
  //}
  // the code below will output
  // SomeData is: Hello
  var newData = restOperation.getBody().SomeData;
  this.logger.info("SomeData is:"+newData);
}

(instance) -> setBody(requestbody)

sets the request body for this RestOperation object

Parameters:
Name Required Type Default Value Description
requestbody Y object   object to be serialized in to the JSON of outgoing message

Returns:
Type Description
restOperation this value for chaining

Examples:

// sets the body of the RestOperation to the object O
// Object O has the fields a and b
// so the outgoing message (received on GET) will look like
// {
// a: "hello",
// b: 99
// }
MyWorker.prototype.onGet = function(restOperation) {
   var o = {a: "hello", b: 99};
   restOperation.setBody(o); // set the body of the outgoing operation to the object o
   this.completeRestOperation(restOperation);
   return;
};

(instance) -> setMethod(method)

sets the request method (GET, PUT, POST, PATCH, DELETE) for this RestOperation object should use one of the predefined RestOperation.Method.* constants

Parameters:
Name Required Type Default Value Description
method Y string   method to use in the request

Returns:
Type Description
restOperation this value for chaining

Examples:

// set the verb to be used in this outgoing restOperation to GET
restOperation.setMethod(RestOperation.Methods.GET);

(instance) -> getMethod()

gets the request method (GET, PUT, POST, PATCH, DELETE) for this RestOperation object

Returns:
Type Description
string method for this restOperation

Examples:

MyWorker.prototype.onGet = function(restOperation) {
  var value = restOperation.getMethod();
  this.logger.info("HTTP Method: " + value);
}

(instance) -> setStatusCode(statusCode)

sets the status code provided with this restOperation. Status codes are as defined in the HTTP stanard and can also be found as constants on WellKnownPorts

Parameters:
Name Required Type Default Value Description
statusCode Y number   status code to be returned as the result of the restOperation call

Returns:
Type Description
restOperation this value for chaining

Examples:

// check a boolean variable named authorization and return code 401 (unauthorized) if not authorized
if (unauthorized) {
  restOperation.setStatusCode(WellKnownPorts.STATUS_UNAUTHORIZED);
  // complete operation or fail otherwise
}

(instance) -> getStatusCode()

gets the status code provided with this restOperation

Returns:
Type Description
number status code

Examples:

MyWorker.prototype.onGet = function(restOperation) {
  var value = restOperation.getStatusCode();
  this.logger.info("HTTP Status Code: " + value);
}

(instance) -> setContentType()

sets the content type for the response provided with this restOperation

Returns:
Type Description
string contentType

Examples:

MyWorker.prototype.onGet = function(restOperation) {
  restOperation.setContentType("application/json");
  var o = {a:"hello", b:99};
  restOperation.setBody(o);
  this.completeRestOperation(restOperation);
}

(instance) -> getContentType()

gets the content type of the request associated with this restOperation

Returns:
Type Description
string contentType

Examples:

MyWorker.prototype.onGet = function(restOperation) {
  var value = restOperation.getContentType();
  this.logger.info("HTTP Content Type Header: " + value);
}

(instance) -> getBasicAuthorization()

gets the basic auth string for the response provided with this restOperation. This is an advanced usecase - follow HTTP specification for formatting of this field

Returns:
Type Description
String the string used in the basic auth header

Examples:

MyWorker.prototype.onGet = function(restOperation) {
  var auth = restOperation.getBasicAuthorization();
  this.logger.info("Basic Authorization String: " + auth);
}

(instance) -> setBasicAuthorization()

sets the basic auth string for the response provided with this restOperation. This is an advanced usecase - follow HTTP specification for formatting of this field

Returns:
Type Description
restOperation this value for chaining

Examples:

// Set the local auth instead of device token
// manually constructing the basic auth header
var auth = "Basic " + new Buffer(oThis.wellKnownPorts.DEFAULT_ADMIN_NAME + ":").toString('base64');
restOperation.setBasicAuthorization(auth);
restOperation.setIsSetBasicAuthHeader(true);

(instance) -> getODataQuery()

Returns OData query If no query is set the last element of the uri path will be used If no uri path is set an empty string is returned

Returns:
Type Description
String odata query