Refit
Refit is the smartest client abstraction because it generates a client from an interface. All you have to do is provide an abstraction:
- To use the
Refit
library, first install theRefit
NuGet:dotnet add package Refit
- To create a client in Refit, first create an interface with HTTP methods:
public interface IStarWarsClient { Â Â Â Â [Get("/films")] Â Â Â Â public Task<ApiResult<IEnumerable<Film>>> GetFilms(); }
Please note that the endpoint here is /films
rather than films
. If you run the code with films
, you will get an exception suggesting that you change the endpoint with a preceding /
.
- To resolve the client, simply run the following code:
var client = RestService.For<IStarWarsClient>("https://swapi.dev/api/");
On running the demo, the following output gets displayed:
1977-05-25 A New Hope 1980-05-17 The Empire Strikes Back 1983-05-25 Return of the Jedi 1999-05-19...