Loading

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.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.  See one of my previous posts "Consume REST Service On The Server" for more information on this. 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!
 


Comments
EmilySophia
Thank you.Rest service mobile application very nice.Single Page Application websites good looking.
The<a href="http://www.naisedu.com/diploma-in-banking-and-finance-dubai/">finance courses in dubai</a>finance courses in dubai place visiters.
12/8/2017 9:41:57 AM

ERP in Saudi Arabia
Totally awesome posting! Loads of valuable data and motivation, both of which we all need!Relay welcome your work.
11/18/2017 7:41:36 AM

cisco 300-206 dumps
What a amazing post shared here. I really love this website, I would like to say thanks for sharing such great post. I would like to say bundle of thanks for sharing.
11/17/2017 9:48:17 AM

hair extensions stores near me
long Hair will be the daily beauty of long hair played more excellent. Only with uk hair extensions the distinctive hair effect, the show is the collision of personality and temperament beauty.
11/10/2017 1:24:55 AM

waqar mazhar
good content
10/31/2017 4:25:44 PM

تور تایلند
Nice post! keep up the good work The Blogging Arena
10/25/2017 12:27:49 PM

free picture viewer tool online
Here to see unlimited photo using online...
10/16/2017 5:59:59 AM

and
Sex enhancer for men: Herbal A natural sex enhancer for men.
10/13/2017 3:10:40 PM

colour switch
My friend recommended this blog and he was totally right keep up the fantastic work! Thanks for your page! Your share information it helped me alot!
10/3/2017 9:59:14 AM

writing paper
Thank you so much for the post.You are so fascinating! I don't think I've really perused something like this sometime recently. So great to find someone else with a couple of unique musings on this theme. Truly.. much obliged to you for beginning this up.This site is one thing that is required on the web, somebody with a little creativity and workign criteria is Excellent
8/29/2017 7:43:08 AM

my hr cvs
Nice post helps me alot thanks.
8/8/2017 11:34:00 AM

assignment writing service uk
Thank you for sharing this publish. The content is honestly very helpful. This blog is always sharing useful facts. keep sharing greater facts and posts. I truly loved the way you wrote this post. It has some right facts that are written in an easy to recognize manner.
7/26/2017 1:15:11 PM

Essay Help
A very informative post on REST service proxy
7/10/2017 6:06:38 AM

http://ghostwritingerfahrungen.de/
That's the very nice post you have shared with us. I really liked it. Please keep sharing more and more information.
7/6/2017 7:14:24 PM

uan login
Welcome, All to EPFO LOGIN Portal. EPFO login is abbreviated as Employee’s Provident Fund Organisation
6/9/2017 6:45:26 AM

marketing assignment help
Thank you so much for the post.You are so fascinating! I don't think I've really perused something like this sometime recently. So great to find someone else with a couple of unique musings on this theme. Truly.. much obliged to you for beginning this up.This site is one thing that is required on the web, somebody with a little creativity and workign criteria is Excellent
5/13/2017 8:25:21 AM

management assignment help
Thank you so much for the post.You are so fascinating! I don't think I've really perused something like this sometime recently. So great to find someone else with a couple of unique musings on this theme. Truly.. much obliged to you for beginning this up.This site is one thing that is required on the web, somebody with a little creativity and workign criteria is Excellent
5/13/2017 8:24:38 AM

followers - like
Great article.
4/28/2017 7:53:43 AM

write cousersework online
Thanks it is nice to add a further check to make the pathway stronger. I also got stuck in the same situation like this but the solution you provided here will help the customers as well. And if you tell how to do this it will be more convenient for the customers. They can do it anytime.
4/22/2017 7:29:32 PM

 Security code