No video

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

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

Пікірлер: 80

  • @MilanJovanovicTech
    @MilanJovanovicTech4 ай бұрын

    Get the source code for this video for FREE → the-dotnet-weekly.ck.page/refit Want to master Clean Architecture? Go here: bit.ly/3PupkOJ Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt

  • @user-dm3gu3if5s

    @user-dm3gu3if5s

    3 ай бұрын

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

  • @ahmedrizk106
    @ahmedrizk1064 ай бұрын

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

  • @MilanJovanovicTech

    @MilanJovanovicTech

    4 ай бұрын

    I'll talk to my editor about that 😂

  • @jeffersantosss

    @jeffersantosss

    4 ай бұрын

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

  • @GeorgiosNtomoras
    @GeorgiosNtomoras4 ай бұрын

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

  • @MilanJovanovicTech

    @MilanJovanovicTech

    4 ай бұрын

    Glad you liked it!

  • @xavier.xiques
    @xavier.xiques3 ай бұрын

    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

    3 ай бұрын

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

  • @10Totti
    @10Totti3 ай бұрын

    Best Tutorial!

  • @MilanJovanovicTech

    @MilanJovanovicTech

    3 ай бұрын

    Glad you think so!

  • @lyrmusica
    @lyrmusica3 ай бұрын

    Nice video! I'm from Brazil

  • @MilanJovanovicTech

    @MilanJovanovicTech

    3 ай бұрын

    Thanks from Serbia :)

  • @oscarmiguelgonzalezbaute3155
    @oscarmiguelgonzalezbaute31554 күн бұрын

    This video is pure Gold.

  • @MilanJovanovicTech

    @MilanJovanovicTech

    4 күн бұрын

    Thanks a lot! :)

  • @VoroninPavel
    @VoroninPavel4 ай бұрын

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

  • @MilanJovanovicTech

    @MilanJovanovicTech

    3 ай бұрын

    Good points!

  • @joga_bonito_aro
    @joga_bonito_aro4 ай бұрын

    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)

  • @MilanJovanovicTech

    @MilanJovanovicTech

    4 ай бұрын

    Nice one 🏆

  • @adamchubbuck4385
    @adamchubbuck43854 ай бұрын

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

  • @MilanJovanovicTech

    @MilanJovanovicTech

    3 ай бұрын

    Not that I know, but I haven't really looked for any

  • @JuanOG
    @JuanOG4 ай бұрын

    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

    4 ай бұрын

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

  • @JuanOG

    @JuanOG

    3 ай бұрын

    @@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
    @baranacikgoz3 ай бұрын

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

  • @MilanJovanovicTech

    @MilanJovanovicTech

    3 ай бұрын

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

  • @Rob_III
    @Rob_III4 ай бұрын

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

  • @MilanJovanovicTech

    @MilanJovanovicTech

    4 ай бұрын

    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

    4 ай бұрын

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

  • @MikkalYT
    @MikkalYTАй бұрын

    Any ideas how to setup this with Autofac DI container? Searched web but I cannot see any sdk from refit to deal with, so probably some custom setup required

  • @MilanJovanovicTech

    @MilanJovanovicTech

    Ай бұрын

    Shouldn't it just work? Not sure which part is missing with Autofac?

  • @enricoroselino7557
    @enricoroselino75574 ай бұрын

    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

    4 ай бұрын

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

  • @janjoska2549

    @janjoska2549

    4 ай бұрын

    Just don't use it as clustered index in db

  • @MilanJovanovicTech

    @MilanJovanovicTech

    4 ай бұрын

    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

  • @joga_bonito_aro

    @joga_bonito_aro

    4 ай бұрын

    I would suggest ULID by Cysharp

  • @euler.chavez

    @euler.chavez

    4 ай бұрын

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

  • @jimgilmartin1360
    @jimgilmartin13604 ай бұрын

    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

    4 ай бұрын

    HttpResponseMessage and parse the response...

  • @VoroninPavel

    @VoroninPavel

    4 ай бұрын

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

  • @ppasieka

    @ppasieka

    3 ай бұрын

    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

    3 ай бұрын

    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
    @davearkley70144 ай бұрын

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

  • @MilanJovanovicTech

    @MilanJovanovicTech

    4 ай бұрын

    I'm not aware

  • @Rob_III

    @Rob_III

    4 ай бұрын

    Refitter

  • @davearkley7014

    @davearkley7014

    4 ай бұрын

    @@Rob_III Thanks Rob, looks interesting

  • @milutinke
    @milutinke4 ай бұрын

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

  • @Rob_III

    @Rob_III

    4 ай бұрын

    is it really that much trouble to write in English?

  • @MilanJovanovicTech

    @MilanJovanovicTech

    4 ай бұрын

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

  • @MilanJovanovicTech

    @MilanJovanovicTech

    4 ай бұрын

    @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

    4 ай бұрын

    @@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

    3 ай бұрын

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

  • @vgwizardx
    @vgwizardx3 ай бұрын

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

  • @MilanJovanovicTech

    @MilanJovanovicTech

    3 ай бұрын

    Why?

  • @vgwizardx

    @vgwizardx

    3 ай бұрын

    @@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

    3 ай бұрын

    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.

  • @rengstrom
    @rengstrom3 ай бұрын

    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

    3 ай бұрын

    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

    3 ай бұрын

    @@MilanJovanovicTech Alright, thanks for sharing your thoughts.

  • @amierulbasyaryusoff2459
    @amierulbasyaryusoff24593 ай бұрын

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

  • @amierulbasyaryusoff2459

    @amierulbasyaryusoff2459

    3 ай бұрын

    Found it!! In the documentation. We can use `ApiResponse` 😇

  • @MilanJovanovicTech

    @MilanJovanovicTech

    3 ай бұрын

    You can get back an HttpResponseMessage

  • @cuachristine
    @cuachristine3 ай бұрын

    How much slower is the compilation because of source generation?

  • @MilanJovanovicTech

    @MilanJovanovicTech

    3 ай бұрын

    At least 2 eternities on my microwave...

  • 3 ай бұрын

    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

    3 ай бұрын

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

  • @benjsoft
    @benjsoft3 ай бұрын

    Similar to FeignClient in Java/Springboot

  • @MilanJovanovicTech

    @MilanJovanovicTech

    3 ай бұрын

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

  • @benjsoft

    @benjsoft

    3 ай бұрын

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

  • @oladejiakomolafe5591
    @oladejiakomolafe55913 ай бұрын

    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 => kzread.info/dash/bejne/iXd3zaeej6vfYrw.html but am using language ext by Paul Louth

  • @MilanJovanovicTech

    @MilanJovanovicTech

    3 ай бұрын

    Wrap it into an Either before returning?

  • @oladejiakomolafe5591

    @oladejiakomolafe5591

    3 ай бұрын

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

  • @oladejiakomolafe5591

    @oladejiakomolafe5591

    3 ай бұрын

    @@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 ?