RSCG Example–HttpClientGenerator–part 19

 

 

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

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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

All RSCG

NrBlog Post
1RSCG–part 1
2RSCG- AppVersion–part 2
3http://msprogrammer.serviciipeweb.ro/2021/02/17/rsgc-enum-part-3/
4RSGC-JSON to Class- part 4
5RSGC-Constructor – Deconstructor – part 5
6RSGC – DTO Mapper – part 6
7RSGC – Skinny Controllers- part 7
8RSGC-Builder Design Pattern – part 8
9RSGC- MetadataFromObject – part 9
10RSGC- Dynamic Mock – part 10
11RSCG- Method Decorator – part 11
12RSCG – Curry – Partial function – part 12
13RSCG- part 13 – IFormattable
14RSCG- part 14 – DP_Decorator
15RSCG- part 15 – Expression Generator
16RSCG- part 16 – Many Others
17RSCG- the book
18RSCG–Template Rendering- part 17
19CI Version
20HttpClientGenerator
21Query from database
22AutoRegister
23TinyTypes
24Static2Interface
25AppSettings
26Properties
27
Roslyn Source Code Generators