Skip to main content

This is documentation for Caché & Ensemble. See the InterSystems IRIS version of this content.Opens in a new tab

For information on migrating to InterSystems IRISOpens in a new tab, see Why Migrate to InterSystems IRIS?

REST オペレーションの開発

REST オペレーションを開発するには、EnsLib.REST.OperationOpens in a new tab クラスを拡張します。REST オペレーションは Ensemble の出力 HTTP アダプタを使用します。詳細は、"HTTP 送信アダプタの使用法" を参照してください。

HTTP 送信アダプタを使用する場合、HTTP アダプタ構成の外部 Web サービスのサーバ、ポート、およびアドレスを指定します。例えば、天気のサービスを呼び出す REST オペレーションを定義するには、以下のようにオペレーションを構成します。

generated description: operation weather

EnsLib.REST.OperationOpens in a new tab の拡張で、アダプタの URL プロパティに値を付加して、URL の残りの部分を指定します。次に、使用する HTTP オペレーションに応じて、以下のいずれかのアダプタ・メソッドを呼び出します。

  • GetURL()— HTTP GET オペレーションを使用します。

  • PostURL()— HTTP POST オペレーションを使用します。

  • PutURL()— HTTP PUT オペレーションを使用します。

  • DeleteURL()— HTTP DELETE オペレーションを使用します。

  • SendFormDataArray()— HTTP オペレーションをパラメータとして指定できます。

たとえば、以下の EnsLib.REST.OperationOpens in a new tab の拡張は、天気の REST サービスを呼び出して、都市名をパラメータとして提供します。

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 Cache 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>
}

}

オペレーションに送信されたメッセージは、都市を指定します。

Class Test.REST.WeatherRequest Extends (%Persistent, Ens.Util.MessageBodyMethods)
{
   Property City As %String;
}

このオペレーションは、JSONStreamToObject() メソッドを呼び出し、JSON の要素をアクセス可能にする Caché オブジェクトを返します。このサンプルによって返されたメッセージは、JSON ストリームから取得された以下のプロパティを返します。

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;
 }

実行中のビジネス・プロセスがない場合は、Ensemble, プロダクション構成 で、このオペレーションおよび他のオペレーションを実行およびテストできます。これを行うには、オペレーションを選択して、[プロダクション設定] メニューの [アクション] に移動し、[テスト] をクリックします。

FeedbackOpens in a new tab