| name | HttpClientGenerator |
| nuget |
https://www.nuget.org/packages/HttpClientGenerator/ |
| link | https://github.com/Jalalx/HttpClientCodeGenerator |
| author | Jalal Amini Robati |
HttpClientGenerator is a tool that uses Roslyn code generator feature to write boilerplate HttpClient code for you.
The code that you start with is
public partial class WeatherService
{
[HttpGet(
WeatherForecast/{id}
)]
public partial Task<WeatherForecast> GetWeather(int id);
[HttpGet(
WeatherForecast
)]
public partial Task<WeatherForecast[]> GetAllWeather();
}
The code that you will use is
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(
http://localhost:5000
);
var userService = new WeatherService(client);
var w = await userService.GetWeather(1);
Console.WriteLine($
{w.Summary}
);
var q = await userService.GetAllWeather();
Console.WriteLine($
{q[0].Summary}
);
}
The code that is generated is
public partial async System.Threading.Tasks.Task<BL.WeatherForecast> GetWeather(int id)
{
const string @___httpMethod =
GET
;
var @___path =
WeatherForecast/{id}
;
var @___routes = new Dictionary<string,object>();
@___routes[
id
] = id;
var @___queryParams = new Dictionary<string,object>();
// Query String dictionary goes here...
var @___headers = new Dictionary<string,string>();
// Header dictionary goes here...
return await HttpClientGenerator.Shared.HttpClientHelper.SendAsync<BL.WeatherForecast>(_httpClient,@___httpMethod,@___path,@___headers,@___routes,@___queryParams);
}
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/HttpClientCodeGenerator
Leave a Reply