REST Service Proxy

#Custom software development #Kentico development #.NET #Kentico Kontent

SHARE

 
​Kentico provides us with REST service which is extremely handy when building Single Page Application or native mobile app as it allows us to retrieve any kind of data we need and goes even further than that - allows to manipulate that data creating new or editing existing objects or pages, applying workflow actions to a page, etc. However there are cases, where REST almost covers our needs. In this post I'm going to introduce an approach that might help you to hit your target.

 
All we are facing situation when customer is asking a bit more than Kentico suggest out of the box. It is really frustrating when existing feature meets 95% of the requirement, but we don't have enough control to adjust it, so it would work the way we need. I was in this situation for many times so, I believe, everyone was. Usually I asked myself: so what's next? Should I invent/implement a wheel in order to add an extra check? And obviously no one wants to.
 
Recently I've got into similar situation: customer request support on date-time where condition in UNIX format for REST API. So where condition is already there, it supports filtering by date, so my issue was just format. Would anybody like to build a new REST service just because of that?! Neither I...
 
You might think about exposing Web API controller that would retrieve needed data from database and pass it back to client and this solution would be right, but... what if there many different object client is going to request? - This would introduce extra coding... and maintenance. Another issue you might face with this solution in case there is a client app with a ton of javascript code that has been written to access data according to the object structure REST service returns - you'll have to return exact same structure and this might be challenging in some cases.
 
Anyway, Web API is a step in right direction. The trick here is the way to retrieve data. No need to query data directly from database, but consume Kentico REST service. So I suggest wrapping REST service with Web API controller. Let's check out the code snippet for it:
 
 


private HttpResponseMessage GetRestData(string where)

    {

        using (var client = new HttpClient())

        {

            var url = URLHelper.GetAbsoluteUrl("/rest/mycustommodule.product");

 

            if (!string.IsNullOrEmpty(where))

            {

                url += string.Format("?where={0}", where);

            }

 

            client.BaseAddress = new Uri(url);

 

            client.DefaultRequestHeaders.Accept.Clear();

 

            client.DefaultRequestHeaders.Authorization = Request.Headers.Authorization;

 

            Task<HttpResponseMessage> t = client.GetAsync(new Uri(url));

 

            return t.Result;

        }

    }

 



As you can see, we are getting request with controller and we can 'translate' it into something we need. In my case I've converted date parameter from UNIX format to one that works for Kentico. 
Also we have control over response. We can deserialize data returned by REST API into strongly typed object and do any processing we need, e.g. join User and UserSettings.  This is great opportunity to extend API where condition to anything we need meanwhile Kentico provides just basic support for it.
  
In my example I'm relying on REST service security by passing authorization header, but you don't have to! You are free to implement extra layer of security here. 

So just with a couple of lines of code we've built proxy for Kentico REST service gaining more control over it as well as more flexibility. Enjoy and stay tuned!
 

Author

Check other articles

Bitsorchestra
5 5

What our clients say

Bits Orchestra team are outstanding developers​. They listen carefully to our business needs and easily turns our business objectives into a well thought out and executed development effort. Roman is very bright and definitely the most capable developer that has worked on our site. He is not only a Kentico expert but has successfully tackled other complicated development assignments demonstrating expertise in both front and backend development. Roman takes initiative to suggest enhancements that make site maintenance easier while improving the customer experience. The team is very responsive to our work requests and has great follow up. They have also worked very business partners and this has reflected positively on our company. Roman is a true partner for us and a tremendous asset to our organization. We will continue to work with them and would highly recommend Roman and his team for your development needs. He and his team will exceed your expectations!
 Alan Lehmann
Alan Lehmann
President at In energy sector

What our clients say

The Bits Orchestra team does excellent work. They are always available and I appreciate our frequent calls and screen-shares together. Their dedication to the projects and knowledge of Kentico is outstanding. They truly care about the quality of their work, and became a part of our team easily!
Shena Lowe
Shena Lowe
Managing Partner at Consensus Interactive

What our clients say

We hired Roman for a Kentico analysis project and have been very satisfied. He is very skilled and professional. We are looking to hire him and his team again on future projects.
Sylvain Audet
Sylvain Audet
CEO at MyDevPartner.com

What our clients say

Roman and team have taken over an existing Kentico EMS site for a large US Oil Company. So far, they have handled every single request that we have thrown at them and these were diverse, challenging, often bespoke, usually urgent and almost daily, over the last 11 months. Their work is of an extremely high quality, they are capable, quick and we have great confidence in the support that we are getting.
Jon Hollis
Jon Hollis
Head of Web Development at confidential

What our clients say

Bits Orchestra team was very helpful, they had a good understanding of the brief and deep knowledge of the system. They were always keen to provide advice and recommendations that benefit the project substantially.
Ramon Lapenta
Ramon Lapenta
Senior Front End Developer at Cyber-Duck Ltd