Interface part for accessing a third-party REST service

In EGL, you can use the following Interface part to access a third-party REST service.

interface IRest
   function invokeGet(reqURL string in) returns(string)  
      {@getRest {uriTemplate="{reqURL}"}};
   function invokePost(reqURL string in, representation string in) returns(string)
      {@postRest {uriTemplate="{reqURL}"}};
   function invokePut(reqURL string in, representation string in) returns(string)
      {@putRest {uriTemplate="{reqURL}"}};
   function invokeDelete(reqURL string in, representation string in) returns(string) 
      {@deleteRest {uriTemplate="{reqURL}"}};
end

By using this Interface part as is, you do not have to write one. However, the work required to code the service-invocation statement is different.

Here is an example for asynchronous access, as occurs in Rich UI applications:
myVar IRest{@RestBinding};
myResource String = ServiceLib.convertToJson(myRec);
call myVar.invokePost("http://www.example.com", myResource) 
    returning to myCallbackfunction;
Here is an example for synchronous access, as occurs elsewhere:
myString STRING:
myVar IRest{@RestBinding};
myResource String = ServiceLib.convertToJson(myRec);
myString = myVar.invokePost("http://www.example.com", myResource);
The differences between synchronous and asynchronous access are as follows:
  • You do not pass multiple arguments that are used as substitution values. Here are different ways to construct the URI:
    • You can pass the whole URI, as shown in the examples
    • You can also specify the whole URI in the variable declaration, as shown here:
      myVar IRest {@RestBinding {baseURI="http://www.example.com"}};
      myResource String = ServiceLib.convertToJSON(myRec);
      call myVar.invokePost("", myResource) 
          returning to myCallbackfunction; 
    • You can rely on the variable declaration to provide the base URI and then pass a relative URI
  • When you invoke a service by using the POST, PUT, or DELETE methods, make sure that the representation (a string) is in the format that is required by the REST service. The following functions show the required formats:
    • For JSON conversion: serviceLib.convertToJSON
    • For XML conversion: XMLLib.convertToXML
You might need a function prototype that has a different characteristic, such as a DELETE operation that does not require you to pass a representation. In that case, create a similar Interface part, but change the InvokeDelete function prototype or add a prototype with a different name. Here is a changed prototype:
function invokeDelete(reqURL string in) returns(string) 
   {@deleteRest {uriTemplate="{reqURL}"}};