Blog Back to all posts View all authors

Communication with the external API in .NET app made simple

The repetitive code responsible for service of the external API. If you are a programmer who has a brush with a project written in Xamarin or in form of a simple .NET app, you probably know this issue too well. Such classes usually have even several hundred lines and every method presents a common pattern: Proper API request -> Response deserialization . At this point, you probably start wondering how to prevent this unnecessary code repetition. Let me prevent you from spending sleepless nights, keep on reading!


Refit comes to the rescue


Those of you who are mobile developers and deal with Android, surely know plugin Retrofit (http://square.github.io/retrofit/) because it has become the mandatory element of every emerging app. Programmers from the world of .NET got inspired by this plugin and created the exact equivalent of Retrofit called Refit.


How does it work?


Let's retrace the example below. It's a typical service of external API where I used default class HttpClient from the official records.



static async Task<Product> GetProductAsync(string id)
{
Product product = null;
string path = $"/api/products/{id}"
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
product = await response.Content.ReadAsAsync<Product>();
}
return product;
}


The code above loads data about a product pointed by the value of the field id and sends it back as the deserialized object called Product.


However, if you decide to use the library Refit, the whole method is going to be replaced with the code below:

 

public interface IProductsApi
{
[Get("/api/products/{id}")]
Task<Products> GetProduct(string id);
}

 

It seems to be simple and intuitive, doesn't it? Well, it IS simple and intuitive! Refit uses the library Newtonsoft.Json so that it can deserialize responses automatically. You get everything you need immediately, at once.


In the case when you need to attach proper HTTP headline to the request, all you need to do is to modify the code:


public interface IProductsApi
{
[Get("/api/products/{id}")]
Task<Products> GetProduct(string id, [Header("Authorization")] string authorization);
}


When you want to use POST method, just add the attribute [POST()], for example, for DELETE it is going to be [DELETE()] etc.


Configuration in our project


First, we need to add a proper NuGet package with the library. The command below is going to help us


Install-Package refit

 

The library is compatible with projects that target:

- UWP

- Xamarin.Android

- Xamarin.Mac

- Xamarin.iOS

- Desktop .NET 4.5

- .NET Core


Finally, we can call our request by creating an object with implementation, using RestService.

 

var productsApi = RestService.For<IProductsApi>("https://products.oursite.com");

var product = await productsApi.GetProduct("XYZ");


Summary


The Refit library can make your project more readable and less complicated. It can be used in many creative ways that fit your needs. Size of the code doesn't matter. It needs to be optimal!


Wait! It's not the only purpose of this library! To find out more, visit the records: https://github.com/reactiveui/refit/blob/master/README.md.

 

SEE ALSO

Configuration of Continuous Integration Using Gitlab

contact us

Have an idea ? Let’s talk

Office in Rzeszow
Office in Warsaw
CONTACT US
CONTACT INFORMATION

GET IN TOUCH

Fill up the form and we will contact you shortly

Name
E-Mail
Message

Company information

Fill up the form and we will contact you shortly.

ServoCode Sp. z o.o.

Jasionka 954E, 36-002 Jasionka, Poland

NIP: 8133719852

REGON: 364182909

KRS: 0000611643

We are using cookies to provide statistics that help us give you the best experience of our site. You can find out more or switch them off if you prefer. However, by continuing to use the site without changing settings, you are agreeing to our use of cookies. Read more

close