90 words
1 minute
Configure Refit
- Install
Refit
Terminal window dotnet add package Refitdotnet add package Refit.HttpClientFactory - Create interface
IGithubApi.cs public interface IGithubApi{[Get("/users/{userName}")]Task<User?> GetUserAsync(string username, CancellationToken cancellationToken = default);}9 collapsed linespublic class User{[JsonPropertyName("id")]public long Id { get; set; }[JsonPropertyName("login")]public string Login { get; set; } = string.Empty;[JsonPropertyName("avatar_url")]public string AvatarUrl { get; set; } = string.Empty;} - Configure refit client
Program.cs builder.Services.AddRefitClient<IGithubApi>().ConfigureHttpClient(c =>{c.BaseAddress = new Uri("https://api.github.com");c.DefaultRequestHeaders.UserAgent.TryParseAdd("request");}); - Map endpoint
Program.cs app.MapGet("github/{userName}", async (IGithubApi githubApi,[FromRoute] string userName,CancellationToken cancellationToken) =>{var response = await githubApi.GetUserAsync(userName, cancellationToken);return Results.Ok(response);});
References
Configure Refit
https://slimaeus.github.io/posts/refit-configuration/