Most developers (if not all) have/will have to connect to API endpoints to retrieve some data to use in some shape or form. The same goes for us, recently, whilst developing some Azure functions for a client we had to get back data from my own API that had been created for them. (Just as a side note, we used PFA – Portable Format for Analytics to create our scoring engine in Python exposing two endpoints in our model which became the API endpoints).
The obvious route which we initially took was to use the HttpClient Class to GET and POST our requests, however this could get messy very quickly especially when it would come to managing multi-threading. It was at this point that we discovered the ‘Refit’ REST Library.
‘Refit’ is a REST library for .NET that is inspired by Square’s Retrofit library. It basically turns your API into a live interface allowing you to call the methods that you have defined and in return, the library will provide an implementation of the interface (that you have declared) through dependency injection. This overcomes the management of loading data from the endpoi nts and handling asynchronous tasks (which we would have had to do had we used the HttpClient Class).
Your interface would look something like the following:
where the endpoint is “/score”. As you can see we wanted to POST so the word ‘Post’ is appended before the endpoint. Similarly, if we wanted to GET then we could append ‘Get’ in the same way.
Also note here that we are using our own custom class ‘Search’ as our ‘Body’ argument i.e. the metadata that we will be passing into our method which the library will use to communicate to the API Endpoint. The argument would have to match the API model (and what it accepts in its request), but because we created our own API we had full control on it.
We defined our Metadata in it’s own class as such:
The additional class would be needed if you were using a custom object, otherwise hard coding the argument will suffice.
All you then need to do is make a RestService.For<YourInterface> call and that’s it. This would look something like this:
And that is all there is to it. Seriously! Just two lines of code? Yes! The beauty of the library is that it handles all the asynchronous tasks, threading, data loading etc and a bonus is that because it is a .NET Standard library you can use it everywhere. Usually the API call will return some JSON Data and this would then need to be deserialised using (would highly recommend the best and most awesome Library – Newtonsoft.json for all your JSON needs), and vice versa. However, in our case as it is a custom class, our API is returning a sentiment score of type double.
The ‘Refit’ library can be added from Nuget Package Manager by searching for ‘Refit’ and then ‘using Refit’ at the top of your file (Nuget will handle installing any dependencies the library needs so no need to worry about that).
NOTE: It is advised that you test the API you wish to use with a toolchain app like ‘Postman’ to make sure the endpoint is working correctly and so that you can also validate what is being returned is correct. Then make use of refit and query in the same way from your code.
‘Refit’ makes it very easy to develop quick strongly-typed REST Clients in C# for almost anything with minimal effort. Its super cool and quite fun to use too so give this one a go for sure!