Using PeopleCode to Manage Consumer REST Service Operations

This section discusses how to:

  • Invoke a consumer REST service operation.

  • Add REST HTTP connector headers.

Invoking a Consumer REST Service Operations

To invoke a consumer REST service operation, the message is instantiated and then the document template is retrieved and populated. The URI index is selected and a SyncRequest method is invoked. The response message contains the HTTP return code. Processing of the data is the same as any other SyncRequest method. In the case of an error, if the User Exception option is selected on the routing, you can attempt to read the fault if defined on the service operation. If the HTTP response code is the same as that defined on the fault message, then the fault message is created and returned. You can read the fault in the document, if the fault message is a Document message type. The message property IsFault is read to determine if a fault message was created.

The code example shows a simple example of populating the document template data and entering the URI resource index to use and invoking the SyncRequest method.

The GetURIDocument method of the Message class is used to retrieve the URI for the REST based on the specified index. The URIResourceIndex property of the Message class is used to set or return the index for the URI as an integer. This index corresponds to the row number in the URI grid of the REST Resource Definition section of the service operation definition.

Declare Function out_BI_results PeopleCode QE_FLIGHTDATA.QE_ACNUMBER 
  FieldFormula;
&MSG = CreateMessage(Operation.MAPS_GET);

/* Get URI Document and populate with data */
&DOC = &MSG.GetURIDocument();

&COM = &DOC.DocumentElement;

&COM.GetPropertyByName("MapType").Value = "topographic";
&COM.GetPropertyByName("Scale").Value = "1:63";
&COM.GetPropertyByName("Planet").Value = "Earth";
&COM.GetPropertyByName("Country").Value = "USA";
&COM.GetPropertyByName("City").Value = "WhiteSalmon";
&COM.GetPropertyByName("Name").Value = "MainSteet";

/* Set URI Resource Index to be used */
&MSG.URIResourceIndex = 1;
&return_message = %IntBroker.SyncRequest(&MSG);

/* Get return data and display */

If &return_message.ResponseStatus = %IB_Status_Success Then

  /* if xml is returned can use XmlDoc */  
  &xmldocReturn = &return_mesage.GetXmlDoc();
  out_BI_results(&xmldocReturn.GenXmlString());

 /* or if the data returned is JSON */
 Local JsonParser &parser = CreateJsonParser();
 Local boolean &status = &parser.Parse(&return_mesage.GetContentString());
 Local JsonObject &jObj = &parser.GetRootObject();
 out_BI_results(&jObj.ToString());

/* Read Response Headers if set */
  For &i = 1 To &MSG.IBInfo.IBConnectorInfo.GetNumberOfConnectorProperties()
    If &MSG.IBInfo.IBConnectorInfo.GetConnectorPropertiesType(&i) =
      %HttpHeader
    Then
      &name = &MSG.IBInfo.IBConnectorInfo.GetConnectorPropertiesName(&i);
      &value = &MSG.IBInfo.IBConnectorInfo.GetConnectorPropertiesValue(&i);
    End-If;
  End-For;
Else
  If &return_message.IsFault = True Then
     &Fault_Doc = &return_message.GetDocument();
     &COM = &Fault_Doc.DocumentElement;
     out_BI_results(&COM.GetPropertyByName("fault_data").Value);
  Else
     out_BI_results(&return_message.IBException.ToString());
  End-If;
End-If;

Adding REST HTTP Connector Headers

Use the LoadRESTHeader method of the Message class to add HTTP header properties not defined on the routing for the service operation.

The code snippet below shows how to modify HTTP headers using PeopleCode

Note:

The connector override flag in PeopleCode does not need to be set in this case.

No HTTP properties are currently applicable for REST and will be removed by the Integration Broker framework.

&request = CreateMessage(Operation.MAPS_GET);
&bRet = &request.LoadRESTHeaders();  

/* add any additional Headers not defined on Routing */     
&bRet = &request.IBInfo.IBConnectorInfo.AddConnectorProperties
  ("Content-Language ", "eng ", %HttpHeader);