We know that K2 has ability to integrate with multiple LOB’s, let us have a look at how we can integrate K2 with REST Api’s.
This post will explain you
- How we create a REST Service Instance for a REST Api using K2 REST Service broker.
- Create a Smart Object to get the data from REST Api.
- Execute that Smart Object Method.
Creating REST Service Instance
K2 uses Swagger Definition of REST Service for creating a Service Instance. Swagger definition can be obtained by using an online Tool RESTUnited.com
RESTUnited.com is an online tool to generate the swagger definition of a service.
Now for this sample I have used the openWeatherMap.org which provides free REST API’s of weather information with multiple querying options for free along with some paid featured services too.
To Use their free services you need to register with your mail ID and some other basic details. Once registered, you would get an API Key which you need to use for all the API calls.
They have provided enough documentation of their wide range of API’s with details at below link
Here is a sample call to one of the REST Api which takes city name as input and gives the weather response. Please note that API key needs to be passed as URL Parameter APPID
Once you have Api ready, we need to get the swagger definition of that service. Now to get that use RESTUnited.com an online tool. You need to register here for a free trail to use the tool. Now follow the instructions as given at below link.
Once you have completed all the steps you will see below screen in which you would be able to download the Swagger definition by selecting Swagger Tab and clicking Export Button.
Once you have the Swagger Definition/Descriptor file,
- Open SmartObjects – Services – Tester utility tool(find this utility at C:/Program Files/K2 Blackpearl/bin),
- Expand ServiceObject Explorer right click on REST,
- Click Register ServiceInstance and you will be seeing below screen where in highlighted area you need to give the path of that swagger file that we just downloaded.
- Now Click Next button and you should see below screen and Click Add.
Now you should be able to see the Service Instance Added under the REST Category as below, you can explore the methods and types under it from here.
Creating Smart Object
Now it’s time to create a smart object using this Service Instance. We can use K2 Designer or Visual Studio or K2 Studio for creating but here I would prefer to use the same SmartObjects – Services – Tester utility tool to create as it’s the fastest way to create and publish an SMO.
Just give a right click on the object type or method that you want to use from Service Instance and Select Create SmartObject, now you would be seeing below screen. In that give you’re SmartObject a Name and select the folder under Category dropdown and Click Publish SmartObject
That’s it. Now navigate to your SMO and execute the method to see the results.
Below are the serialized data responses from the Api, now to get the individual Values, we need to deserialize them. For knowing how to deserialize the serialized responses check here Handling Complex Objects in K2 SmartObjects
This is how we can integrate K2 with REST Api Services .