Creating Endpoint REST Service instance for a REST api which needs authorization bearer token

 

This post is in continuation to my previous post K2 Integration with REST Api Services which explains how to integrate K2 with REST Api Services. In my previous post, I have integrated with an Api Service which doesn’t require any Authorization Token that needs to be passed. But there is a chance that you find some REST api Services that needs an authorization bearer token to be passed to execute its methods. Now in this post we will see how to integrate to one of those REST Api services which needs the Authorization Bearer Tokens.

To get the swagger file follow the steps that were mentioned in my previous post which is using RESTUnited.com. There will not be any changes in steps to generate swagger except in authentication tab you will have to mention authentication token details and add it which will be as shown in below screenshots.

1AddAuthentication

 

2AddedAuthRESTUnited.jpg

 

Now once we get the Swagger file generated proceed to create Service Instance using EndPoints REST option in Smart Objects – Services – Tester Utility.

Now in Service Keys Section, Default HTTP Request Headers field you need to add this Authorization Token key in below format without any line breaks

{“$type”:”SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader[], SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null”,”$values”:[{“$type”:”SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null”,”Name”:”Authorization”,”Value”:”Bearer eyJhbGciOiJub25lIn0.eyJVc2V==”}]}

*Replace the Values with the token that needs to be passed. Also we can pass multiple header Tokens here.

Now give the path of the Swagger file in the descriptor location field and leave rest of the options with default values

3.jpg

That’s It, Now just create a SmartObject using this Service instance and execute to see the result.

If you get any error with message like “Unable to Deserialize…” then its issue with the format of token that’s passed in HTTP Request Headers section…Just ensure that it is in above mentioned format without any line breaks . That’s It.

Also please note that this procedure should be used only for development purpose as per K2. Don’t ask me the reason (B’cuz I too don’t know :))

Note: Recently, when one of my friend tried to create a SmartObject over a REST Service instance that he created for a REST service which needs authorization bearer token, he was constantly getting 401, 404, 0 as response code but the same service he was able to run from Postman and fiddler without any issue. He tried all above mentioned things but still we couldn’t figure out what is the issue with REST when we are trying to connect with K2. so we approached K2 Team for help and when they investigated they found that its the issue with REST Service Api URL which has spaces in it and K2RESTBroker is trying to encode the URL which is already encoded…so as a fix we have removed spaces in REST service endpoint URL and the k2 is still investigating the actually issue behind the k2 encoding the already encoded URL. So just ensure that your REST Service URL doesn’t have any spaces in it.

Below are the community posts which will give you related information regarding this.

http://community.k2.com/t5/K2-blackpearl/How-toHandle-Authorization-Token-while-Creating-Endpoints-REST/m-p/101521#M33759

Advertisements

Handling Complex Objects in K2 SmartObjects

Deserializing Complex Objects in SmartObjects

java-serialization.png

This post is in continuation to my previous post K2 Integration with REST Api Services where I’ve created an SMO for a REST Api. As api’s return you serialized response, we need to further deserialize them using the Deserialize methods of that particular type/object.

Now if you see the result in below screen shot which is the result of WeatherAPI, it is providing a Serialized objects for Clouds, Coord, Main, Sys. Now to get the individual properties of the above mentioned serialized types/objects, we need to further deserialize them. Lets see how we can do.

SMOResults.jpg

Now the SMO which we have created in last post needs to be modified using K2 Designer. Select the method from service instance (Here it is GetWeatherbyCityName) and map the properties as shown in below screen shots.

ServiceInstance.jpg

MainMethodMapping.jpg

Now if you see the mapped properties in above screen shot, Clouds is mapped to a memo property Clouds (Clouds) which is the serialized result from api. Once the method is created click add and select Deserialize method of clouds from service instance as shown below.

CloudsDesrialize.jpg

Now the Deserialize method takes the serialized value as input and returns the individual properties of Clouds type. You need to map the serialized value which we got from above method as input and map the individual return properties to SMO properties (Here the property is ALL).

DeserializeClouds.jpg

Now similar to above add deserialize methods of other class types (Coord, Main..) and map their respective properties.

AllMethods

Now once you finish,  it should like below.

Method.jpg

That’s it. Click Finish and execute the SMO to see the individual properties as below

 

Results

CloudsALL is the deserialized value from Clouds Object, Similarly CoordinatesLatitude, CoordinatesLongitude are the desreialized values from Coord object, similarly Main Object Values you can see there.

That’s it. This is how we can Deserialize complex objects.

Here we have seen only single level of complexity which means for every weather result object, we get only one cloud object, one Coord object and so on. But there could be cases of result with multi-level complexity which means for every single weather result object there could be multiple cloud objects or multiple Coord objects and so on. In that case we may not be able to deserialize as we did above. We need to create separate methods for each object like GetCloudsbyCityName, GetCoordbyCityName, GetMainValuesbyCityName and handle the methods in same way as we did above.

Also in some cases, Api methods takes serialized objects as an input values, in that case we need to use the serialize method of that objects first which returns us the serialized value and then we need to pass this serialized value to the main method (just opposite to the way we did above).

This is how we can handle complex objects in K2 Smartobjects.