This page describes briefly how to create a REST operation: a Business Operation that invokes an external REST service.
Example
For example, the following extension of EnsLib.REST.OperationOpens in a new tab calls the weather REST service and provides a city name as a parameter:
Class Test.REST.WeatherOperation Extends EnsLib.REST.Operation
{
Parameter INVOCATION = "Queue";
Method getWeather(
pRequest As Test.REST.WeatherRequest,
Output pResponse As Test.REST.WeatherResponse) As %Status
{
try {
// Prepare and log the call
// Append the city to the URL configured for adapter
Set tURL=..Adapter.URL_"?q="_pRequest.City_"&units=imperial"
// Execute the call
Set tSC=..Adapter.GetURL(tURL,.tHttpResponse)
// Return the response
If $$$ISERR(tSC)&&$IsObject(tHttpResponse)&&$IsObject(tHttpResponse.Data)&&tHttpResponse.Data.Size {
Set tSC=$$$ERROR($$$EnsErrGeneral,$$$StatusDisplayString(tSC)_":"_tHttpResponse.Data.Read())
}
Quit:$$$ISERR(tSC)
If $IsObject(tHttpResponse) {
// Instantiate the response object
set pResponse = ##class(Test.REST.WeatherResponse).%New()
// Convert JSON into a Proxy Object
set tSC = ..JSONStreamToObject(tHttpResponse.Data, .tProxy)
if (tSC){
// Set response properties from the Proxy Object
set pResponse.Temperature = tProxy.main.temp_"F"
set pResponse.Humidity = tProxy.main.humidity_"%"
set pResponse.MaxTemp = tProxy.main."temp_max"_"F"
set pResponse.MinTemp = tProxy.main."temp_min"_"F"
set pResponse.Pressure = tProxy.main.pressure_" mbar"
set pResponse.WindSpeed = tProxy.wind.speed_" MPH"
set pResponse.WindDirection = tProxy.wind.deg_" degrees"
// Convert from POSIX time
set pResponse.Sunrise = $ZT($PIECE($ZDTH(tProxy.sys.sunrise, -2),",",2),3)
set pResponse.Sunset = $ZT($PIECE($ZDTH(tProxy.sys.sunset, -2),",",2),3)
}
}
}catch{
Set tSC=$$$SystemError
}
Quit tSC
}
XData MessageMap
{
<MapItems>
<MapItem MessageType="Test.REST.WeatherRequest">
<Method>getWeather</Method>
</MapItem>
</MapItems>
}
}
The message sent to the operation specifies the city:
Class Test.REST.WeatherRequest Extends (%Persistent, Ens.Util.MessageBodyMethods)
{
Property City As %String;
}
This operation calls the JSONStreamToObject()Opens in a new tab method and returns an InterSystems IRIS object that makes the elements of the JSON accessible. The message returned by this sample returns the following properties taken from the JSON stream:
Class Test.REST.WeatherResponse Extends (%Persistent, Ens.Util.MessageBodyMethods)
{
Property Temperature As %String;
Property MinTemp As %String;
Property MaxTemp As %String;
Property Pressure As %String;
Property Humidity As %String;
Property WindSpeed As %String;
Property WindDirection As %String;
Property Sunrise As %String;
Property Sunset As %String;
}