Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
The C# Workshop

You're reading from   The C# Workshop Kickstart your career as a software developer with C#

Arrow left icon
Product type Paperback
Published in Sep 2022
Publisher Packt
ISBN-13 9781800566491
Length 780 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (3):
Arrow left icon
Almantas Karpavicius Almantas Karpavicius
Author Profile Icon Almantas Karpavicius
Almantas Karpavicius
Jason Hales Jason Hales
Author Profile Icon Jason Hales
Jason Hales
Mateus Viegas Mateus Viegas
Author Profile Icon Mateus Viegas
Mateus Viegas
Arrow right icon
View More author details
Toc

Refit

Refit is the smartest client abstraction because it generates a client from an interface. All you have to do is provide an abstraction:

  1. To use the Refit library, first install the Refit NuGet:
    dotnet add package Refit
  2. 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 /.

  1. 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...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime