The Best .NET REST API Client You Didn't Know About

Sdílet
Vložit
  • čas přidán 4. 06. 2024
  • Get the source code for this video for FREE → the-dotnet-weekly.ck.page/refit
    ☄️ Master the Modular Monolith Architecture: bit.ly/3SXlzSt
    📌 Accelerate your Clean Architecture skills: bit.ly/3PupkOJ
    🚀 Support me on Patreon to access the source code: / milanjovanovic
    The easy way to make HTTP requests in .NET is to use the HttpClient to send those requests. And it's a great abstraction to work with, especially with the methods supporting JSON payloads and responses. Unfortunately, it's easy to misuse the HttpClient. Port exhaustion and DNS behavior are some of the most common problems. Enter Refit: the automatic type-safe REST library for .NET Core. In this video, I'll show you how to use Refit to easily integrate with an HTTP API.
    The Right Way To Use HttpClient In .NET
    www.milanjovanovic.tech/blog/...
    Check out my courses: bit.ly/3PupkOJ
    Join my weekly .NET newsletter:
    www.milanjovanovic.tech
    Read my Blog here:
    www.milanjovanovic.tech/blog
    Chapters
    0:00 GitHub API integration with HttpClient
    4:54 Replacing HttpClient with Refit
    10:03 Creating a PATCH endpoint with Refit
  • Věda a technologie

Komentáře • 75

  • @MilanJovanovicTech
    @MilanJovanovicTech  Před 2 měsíci +1

    Get the source code for this video for FREE → the-dotnet-weekly.ck.page/refit

    • @user-dm3gu3if5s
      @user-dm3gu3if5s Před měsícem

      May be i lost newsletter message, but i can`t find post for this video with sources....

  • @ahmedrizk106
    @ahmedrizk106 Před 2 měsíci +17

    that bright screen warning 0.1s before the bright screen is a scam 😂 ... Great video as always Milan ❤👏

    • @MilanJovanovicTech
      @MilanJovanovicTech  Před 2 měsíci +3

      I'll talk to my editor about that 😂

    • @jeffersantosss
      @jeffersantosss Před 2 měsíci +2

      @@MilanJovanovicTech, you can turn swagger theme to the dark mode for the video, customizing the swagger UI css.. haha

  • @GeorgiosNtomoras
    @GeorgiosNtomoras Před 2 měsíci +3

    Loved the way you compare the traditional with the modern way of doing things. Thank you for sharing this

  • @xavier.xiques
    @xavier.xiques Před 2 měsíci +1

    The Refit implementation is the same as used in FeignClients, in Spring Boot applications for example.
    Love it.
    Thanks for the video Milan.

    • @MilanJovanovicTech
      @MilanJovanovicTech  Před 2 měsíci +1

      A lot of .NET libraries were inspired by existing Java libraries

  • @satyayuga0
    @satyayuga0 Před 2 měsíci +5

    I said, ooh, I'm blinded by the lights
    No, I can't sleep until I refactor the http
    I said, ooh, I'm drowning in the lines
    Oh, when I'm like this, refit is the one I trust
    (Hey, hey, hey)

  • @VoroninPavel
    @VoroninPavel Před 2 měsíci +2

    Couple of downsides: Refit currently does not support streaming, and it's not AOT friendly. Both issues are tracked on github.

  • @10Totti
    @10Totti Před 2 měsíci +3

    Best Tutorial!

  • @adamchubbuck4385
    @adamchubbuck4385 Před 2 měsíci

    @MilanJovanovicTech, a bit off topic, but are there any reasonably decent alternatives to Swagger’s UI?

  • @Rob_III
    @Rob_III Před 2 měsíci +1

    What do you all use for a (thread-safe) token delegating handler which refreshes the token when expires?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Před 2 měsíci

      Would all requests share the same token, or do we need to make sure we use the _correct_ token for each request?

    • @Rob_III
      @Rob_III Před 2 měsíci

      @@MilanJovanovicTech All request would share the same token. I have implemented my own, but I'm curious if there are (more battle-tested) solutions.

  • @enricoroselino7557
    @enricoroselino7557 Před 2 měsíci +2

    a bit OOT but github use int for user id, then what do you think about userid better use int or uuid (or some sort) ? or its depend on the usage ?

    • @md.redwanhossain6288
      @md.redwanhossain6288 Před 2 měsíci

      Most of the tech giants use int, it has significantly better performance than uuid.

    • @janjoska2549
      @janjoska2549 Před 2 měsíci +1

      Just don't use it as clustered index in db

    • @MilanJovanovicTech
      @MilanJovanovicTech  Před 2 měsíci

      At GitHub's scale using a UUID probably has a performance impact, so that's another reason. Most of the time I'm not concerned with that. An interesting alternative if you want sortable (at creation) IDs is ULID

    • @satyayuga0
      @satyayuga0 Před 2 měsíci

      I would suggest ULID by Cysharp

    • @euler.chavez
      @euler.chavez Před 2 měsíci +2

      @@MilanJovanovicTech It would be nice to have a video discussing the following topics: UUID, GUID, ULID, Snowflake ID (probably more).

  • @lyrmusica
    @lyrmusica Před měsícem

    Nice video! I'm from Brazil

  • @JuanOG
    @JuanOG Před 2 měsíci

    How would you implement caching for certain api endpoints in refit. I haven't seen any options for this. Best case scenario is that there is some attribute which will allow the refit client to be source generated with caching. But unfortunately that doesn't seem to be the case.

    • @MilanJovanovicTech
      @MilanJovanovicTech  Před 2 měsíci +1

      A delegating handler that caches based on the URL params could get the job done

    • @JuanOG
      @JuanOG Před 2 měsíci

      @@MilanJovanovicTech yes that would work. But if it was an attribute of some sort, it would have been more clean and scalable. Great alternative though 🤘.

  • @baranacikgoz
    @baranacikgoz Před 2 měsíci +1

    Great video. But I wouldn't use an extra dependency just to shorten my code 2 lines. Thanks for sharing alternatives

    • @MilanJovanovicTech
      @MilanJovanovicTech  Před 2 měsíci +2

      Fair enough, that's your decision to make. But do consider how this scales as the number of endpoints increases.

  • @jimgilmartin1360
    @jimgilmartin1360 Před 2 měsíci +1

    What about error handling using refit? If the api returns either a DTO on success and a different, error DTO on failure, along with the appropriate status code, what does that look like? In general I like returning error objects along with a 4xx status code, but handling them feels like a lot of work.

    • @MilanJovanovicTech
      @MilanJovanovicTech  Před 2 měsíci

      HttpResponseMessage and parse the response...

    • @VoroninPavel
      @VoroninPavel Před 2 měsíci

      @@MilanJovanovicTech or just catch an exception which also contains the content and can be deserialized as required type.

    • @ppasieka
      @ppasieka Před měsícem

      Or return ApiResponse. It will have the parsed content alongside with all the headers. Just don't get crazy and do not make this type lurking around through all the layers in your application.

    • @DiegoModoloRibeiro
      @DiegoModoloRibeiro Před měsícem

      The perfect scenario would be Refit allow something like Task GetAsync(), and deserialize accordingly. I don't feel comfortable returning 200 for errors so Refit will be able to serialize the response.

  • @davearkley7014
    @davearkley7014 Před 2 měsíci +3

    Are you aware of a package which can generate a Refit client form an OpenApi spec (yaml - v3.0.1)?

  • @rengstrom
    @rengstrom Před měsícem

    Disclaimer, watched the video without sound so maybe you covered this. :P But in the context of clean architecture, in which layer would you put the interface? Seems to me like I would need to have it in the infrastructure layer because I don't want to have models in my domain that is tightly coupled to an external dependency. So I'd need some adapter that uses this interface, fetches models and converts it into som dto in my domain.

    • @MilanJovanovicTech
      @MilanJovanovicTech  Před měsícem +1

      You can either sacrifice purity, and have the interface in Application. Or create another abstraction that will be implemented using Refit in Infrastructure.

    • @rengstrom
      @rengstrom Před měsícem

      @@MilanJovanovicTech Alright, thanks for sharing your thoughts.

  • @milutinke
    @milutinke Před 2 měsíci

    Ћао, одлична библиотека и видео.
    Имам предлог, а то је да направиш додатни видео да покажеш како да се са Refit-ом и на пример са Polly-ем постигне поузданост, на пример ако неки захтев пукне, па да га пошаље поново.

    • @Rob_III
      @Rob_III Před 2 měsíci

      is it really that much trouble to write in English?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Před 2 měsíci +2

      Polly има готову библиотеку коју можемо да углавимо овде, можда је више тема за блог него за видео.

    • @MilanJovanovicTech
      @MilanJovanovicTech  Před 2 měsíci +1

      @Rob_III What's wrong with some Serbian? 😁 He was suggesting to make a video about Polly and adding resiliency to HTTP client requests.

    • @Rob_III
      @Rob_III Před 2 měsíci +3

      @@MilanJovanovicTech There's nothing wrong with Serbian. My native language is Dutch. I just don't post in Dutch because most people don't understand it and it creates a little "just for us -speakers"-circle-vibe. That's why I wondered why it was too much trouble to write in English.

    • @vgwizardx
      @vgwizardx Před 2 měsíci

      @@Rob_III If you don't speak english or understand it. You might not trust translators. Which I can completely understand.

  • @vgwizardx
    @vgwizardx Před 2 měsíci +1

    Maybe for personal projects. But I couldn't see myself using this for production work projects.

    • @MilanJovanovicTech
      @MilanJovanovicTech  Před 2 měsíci

      Why?

    • @vgwizardx
      @vgwizardx Před 2 měsíci

      @@MilanJovanovicTech I have been burned by thrid-party libraries in the past (looking at you restsharp). Maybe refit is better but some things I just prefer built-in libraries. I also hate when a feature I'm used to isn't available and now I have to mix and match libraries to get what I want. I'm not trying to say that's the case with refit but my experience in the past.

    • @vgwizardx
      @vgwizardx Před 2 měsíci +1

      I have been burned by third party libraries in the past. You also have to get everyone on board. So I try to use built-in libraries as often as possible.

  • @amierulbasyaryusoff2459
    @amierulbasyaryusoff2459 Před 2 měsíci

    Really nice library. Not yet try it. But, can it also handle http status also?

  • @cuachristine
    @cuachristine Před 2 měsíci

    How much slower is the compilation because of source generation?

  •  Před měsícem

    1) the Async suffix is moreorless obsolete now ... there were reasons to name the methods like that, but you don't have to do that anymore.
    2) you forgot cancellationtokens ...

    • @MilanJovanovicTech
      @MilanJovanovicTech  Před měsícem +1

      1) Don't care, I still like using it as a convention. Nothing wrong with that.
      2) Fair point. 👍

  • @benjsoft
    @benjsoft Před 2 měsíci

    Similar to FeignClient in Java/Springboot

    • @MilanJovanovicTech
      @MilanJovanovicTech  Před 2 měsíci

      That's cool, I like learning about Java libraries that do the same thing

    • @benjsoft
      @benjsoft Před 2 měsíci

      @@MilanJovanovicTech yes that's why Refit is familiar :D Thanks for this interesting library :D

  • @oladejiakomolafe5591
    @oladejiakomolafe5591 Před 2 měsíci

    Thanks for this Milan , Please how can I use Refit with a controller that returns an Either result type ( this is similar to the Result pattern you explained here => czcams.com/video/WCCkEe_Hy2Y/video.html but am using language ext by Paul Louth

    • @MilanJovanovicTech
      @MilanJovanovicTech  Před 2 měsíci

      Wrap it into an Either before returning?

    • @oladejiakomolafe5591
      @oladejiakomolafe5591 Před 2 měsíci

      @@MilanJovanovicTech Exactly what I did but Refit is throwing deserialisation error

    • @oladejiakomolafe5591
      @oladejiakomolafe5591 Před 2 měsíci

      @@MilanJovanovicTech I want to use refit to generate an sdk but am getting the error => : The JSON value could not be converted to LanguageExt.Either`2
      this is my controller
      [HttpGet(template: DocumentVersionManagerAPIEndPoints.Model.Get, Name =
      DocumentVersionManagerAPIEndPoints.Model.Get)]
      public Task Get(CancellationToken cToken) => _sender.Send(new GetAllModelQuery(), cToken).ToActionResult();
      This is my refit interface
      [Get("/api/v1/Models")]
      Task Get(CancellationToken cToken);
      I also tried using [Get("/api/v1/Models")]
      Task Get(CancellationToken cToken);
      in both cases I got
      An error occured deserializing the response.
      Please I need help is there a way I can correctly have Either Result in my controller return type and still Use Refit for my SDK development ?