The point of this library is to make things easier and also add structure to our code when we communicate with REST API. In this article we look at: How you could convert those URL calls to Methods, build an interface, and how to control which parameter gets posted in the body and which parameter gets posted in the query.
Introduction
When you work with REST API, the biggest issue is the unstructured URLs and HttpClient
calls.
Rest.API.Translator
make things much easier for you to communicate with the REST API.
Changes
Added FromQueryAttribute
to control how the parameter gets processed.
Background
Back in the old days before REST API, we used WCF. Some still prefer WCF because of the structor and the auto generated classes.
REST API made things much easier and more flexible but with every + there is a -.
With REST API, there are no auto generated classes.
So with the help of the URLs and HttpClient
, you could communicate with the API.
Now Rest.API.Translator
does all that job for you, you only need to build an interface
and configure it, and all the URL buildings, HttpClient
calls and json
handler will be done automatically.
Nuget
Using the Code
Now imagine that you have a mobile application and you want to get some data from a REST API.
Let's say here that those APIs exist in the following URLs:
http:
http:
http:
HttpGet Get the HTML from the site
http:
With the normal way, we would use HttpClient
. Read here to know how it's normally done.
And let's see how Rest.API.Translator
works, and how you could convert those URL calls to Methods
.
If we look at the URL http://go.com/api/db/GetName, we know that the baseaddress is http://go.com followed by a key
or identifier
which is api
there after we have the controller name which is db
and lastly the method name which is GetName
.
Let's put what we explain above into code and let's build an interface
that presents those urls
.
[Route(relativeUrl:"api", fullUrl: false)]
public interface IDbController
{
[Route(httpMethod: MethodType.GET)]
string GetName(string firstName, string lastName);
[Route(relativeUrl: "SaveUser", httpMethod: MethodType.JSONPOST)]
Task Save(User user);
[Route(httpMethod:MethodType.Post,relativeUrl: "../y/GetMusic", parameterIntendFormat:true)]
Music GetMusic(string musicId)
[Route(relativeUrl:"http://google.com", fullUrl: true)]
string GoogleCall();
}
Ok, now that we have built the interface
, we are ready to begin communicating with our API using Rest.API.Translator
using(var client = new APIController<IDbController>("http://go.com"))
{
string name= client.Execute(x=> x.GetName("alen", "toma"));
string html = client.Execute(x=> x.GoogleCall());
Music music = await client.ExecuteAsync(x=> x.GetMusic("1152_test"));
await client.Execute(x=> x.Save(user));
}
Now for those that want to add some header to the HttpClient
or simply want to use your own HttpClient
, you could simply do it like below:
using(var client = new APIController<IDbController>(new HttpClient(), "http://go.com")){
}
Sometimes, we don't like to use Attributes in the class and would like to be able to somehow add those attribute settings dynamically.
Well, you could do that still by using AddInterfaceRoute()
and AddMethodRoute
.
using(var client = new APIController<IDbController>()){
client.AddInterfaceRoute(relativeUrl:"api");
client.AddMethodRoute(nameof(IDbController.GoogleCall),
relativeUrl:"http://google.com", fullUrl: true);
}
FromQuery
Now there is sometime we want to be able to control which parameter gets posted in the body and which parameter get posted in the query.
The HTTPPost and JsonPost by default it post all the parameters to the body. We could control this behavior by using the attribute FromQueryAttribute
. Here is an example:
[Route(httpMethod: MethodType.JSONPOST)]
ApplicationSettings GetSetting([FromQuery]string key, User user);
Attributes
[FromQuery]
[Header]
[Route]
Points of Interest
The point of this library is to make things easier and also add structure to our code when we communicate with REST API.
If you have any ideas or suggestions for improvements which you think are a must, then please let me know.
The code exists here if you are interested in downloading it or maybe helping me to improve the library.
History
- 15th August, 2019: Initial version