Wednesday, September 13, 2017

OSB - Consuming REST services [Part 1]

In this post I'll talk about using the REST adapter in OSB 12c to consume a sample webservice:
http://jsonplaceholder.typicode.com. This REST API uses JSON format so we will also use the native format wizard to create the XSD.

GET Operation - Template Parameter
We'll start with a GET operation for the following resource:

/posts/1/comments

This involves using a template to specify the post ID. 

First, drag the REST adapter into the External Services lane to open the Create REST binding window. The Base URI is the one mentioned at the beginning of the post.

Under the Resources section, click the + icon to add the following Relative Path/posts/{id}/comments


We add the brackets and a keyword (in this case, id) to specify the parameter in the style of a template according to how the API expects the resource to be formatted.

Next, in the Operation Bindings section, click the green + and choose Add operation binding to open the REST Operation Binding window. Here, specify the name of the Operation (getPosts), and the resource is /posts/{id}/comments. Because this is a GET operation, the HTTP Verb is GET.


In the Response section, check the JSON box and then click on the Define Schema for Native Format icon on the right side. This is where we will define the WSDL for the operation as well as the schema for the JSON format that is returned by the REST API.

In the Native Format Builder window, provide a proper File Name to the schema and click Next. The File Type is JSON Interchange Format, and in the next step we will choose the filename that has a sample on how the API returns the response:


It will generate a Native Format Schema File like so:



In the Request tab, leave as is. In the URI Parameters, it should show the id Parameter from the template that we defined earlier. Once you click OK, the Request and Response tabs should look as follow:

Request

Response

Now that we've defined the REST service to consume, we will expose a SOAP Webservice that takes a string as an input and another string as an output. I'm not really concerned about the output of the proxy webservice since we just want to see that OSB is consuming the REST service properly given a string as a parameter (which will represent the id that it needs to send).



So create a Proxy service with its pipeline using this schema. The OSB project should look like this:


In the Pipeline, add a Route activity and within, a Routing activity. In the Request Action, add a Replace activity. Here we will send the input parameter to the id element that we defined earlier as a template parameter:



Make sure to select Replace node contents instead of Replace entire node. Once done, we can deploy and test in the Service Bus Console!

Sending a 1 as the input parameter should return a list of comments for the post with Id 1:




GET Operation - Query Parameter
In the first part we did an example of sending a GET request using a template. Now we will see how to do a GET request using a query parameter for the following resource:

/comments?postId=1

Right-click the REST adapter and select Edit REST. Under the Resources section, click the + icon to add the following Relative Path/comments

Next, under Operation Bindings, click Add operation binding. Name the operation getPostsQuery and HTTP Verb as GET. In the Request tab, click the Define schema for Native format to specify that the postId will be one of our query parameters. In the Native Format Builder wizard, choose URI Sample and click Next.

Here, provide the following URL as the sample:
http://jsonplaceholder.typicode.com/comments?postId=1


Click Next twice and then Finish. You'll see that the URI Parameters section has now been updated (the Expression column will update once we click OK):


In the Response tab, use the same NXSD that we built for the previous example since the response will be the same, it's just the request format that is different.

In the pipeline, update the Replace activity accordingly with the updated XML parameters:


Don't forget to also update the operation to getPostsQuery (or whatever you named it) in the Routing activity.

Deploy and test. You should get the same results as in the previous exercise.

And that's it. Super simple to consume a REST JSON service using the REST adapter.

In the next part I'll cover the POST operation.