Http request as builder pattern for easier to use. You can config it to try request again a few more times, if the request was failed. You can map the response in an object of class.
In .Net - NuGet:
Install-Package Http.Request.Builder
// Prepare request object.
var request = RequestBuilder.Create("YOUR_URL")
.Build();
// Send request and get response.
var response = await request.SendAsync();
//Console.WriteLine($"{response.StatusCode} {response.Content}");
var request = RequestBuilder.Create("YOUR_URL")
.WithBearerToken("JWT_TOKEN") // <--- Authentication as bearer token here!
.WithHeader("Accept", "application/json") // <--- Your custom headers!
.WithHeader("CUSTOM_KEY", "CUSTOM_VALUE") // <--- Your custom headers!
.WithRetryAttemptsForFailed(options =>
{
options.MaxRetries = 5; // <--- Number of retries after failure!
options.Delay = TimeSpan.FromSeconds(1); // <--- Number of retries after failure!
})
.Build();
☝ Create your custom client (for example we create WeatherClient.cs
) and register in startup.cs
or program.cs
.
// ...
builder.Services.AddHttpClient<WeatherClient>();
// ...
public class WeatherClient
{
private readonly HttpClient _httpClient;
public WeatherClient(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task FetchAsync()
{
var request = RequestBuilder.Create("https://Weather_URL/Get", HttpMethod.Get, _httpClient) // <--- Pass your client here!
.Build();
var response = await request.SendAsync();
}
}