REST Service Proxy

‚Äč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.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!

Similar topics