@@ -5,75 +5,35 @@ namespace CleanArchitecture.FunctionalTests.Common;
55
66public static class HttpClientExtensions
77{
8- static readonly JsonSerializerOptions DefaultJsonOptions = new ( )
8+ private static readonly JsonSerializerOptions DefaultJsonOptions = new ( )
99 {
1010 PropertyNamingPolicy = JsonNamingPolicy . CamelCase
1111 } ;
1212
1313 public static async Task < T > GetAndDeserializeAsync < T > ( this HttpClient client , string requestUri , string ? token = null )
14- {
15- var request = new HttpRequestMessage ( HttpMethod . Get , requestUri ) ;
16-
17- if ( ! string . IsNullOrEmpty ( token ) )
18- {
19- request . Headers . Authorization = new System . Net . Http . Headers . AuthenticationHeaderValue ( "Bearer" , token ) ;
20- }
14+ => await client . BuildAndSendRequest < T > ( HttpMethod . Get , requestUri , token ) ;
2115
22- HttpResponseMessage response = await client . SendAsync ( request ) ;
23- response . EnsureSuccessStatusCode ( ) ;
24- string text = await response . Content . ReadAsStringAsync ( ) ;
25-
26- return JsonSerializer . Deserialize < T > ( text , DefaultJsonOptions ) ! ;
27- }
2816 public static async Task < T > PostAndDeserializeAsync < T > ( this HttpClient client , string requestUri , object ? model = null , string ? token = null )
29- {
30- string jsonContent = JsonSerializer . Serialize ( model , DefaultJsonOptions ) ;
31- var content = new StringContent ( jsonContent , Encoding . UTF8 , "application/json" ) ;
32-
33- var request = new HttpRequestMessage ( HttpMethod . Post , requestUri )
34- {
35- Content = content
36- } ;
17+ => await client . BuildAndSendRequest < T > ( HttpMethod . Post , requestUri , token , model ) ;
3718
38- if ( ! string . IsNullOrEmpty ( token ) )
39- {
40- request . Headers . Authorization = new System . Net . Http . Headers . AuthenticationHeaderValue ( "Bearer" , token ) ;
41- }
19+ public static async Task < T > PutAndDeserializeAsync < T > ( this HttpClient client , string requestUri , object ? model = null , string ? token = null )
20+ => await client . BuildAndSendRequest < T > ( HttpMethod . Put , requestUri , token , model ) ;
4221
43- HttpResponseMessage response = await client . SendAsync ( request ) ;
22+ public static async Task < T > DeleteAndDeserializeAsync < T > ( this HttpClient client , string requestUri , string ? token = null )
23+ => await client . BuildAndSendRequest < T > ( HttpMethod . Delete , requestUri , token ) ;
4424
45- string text = await response . Content . ReadAsStringAsync ( ) ;
46- return JsonSerializer . Deserialize < T > ( text , DefaultJsonOptions ) ! ;
47- }
48- public static async Task < T > PutAndDeserializeAsync < T > ( this HttpClient client , string requestUri , object ? model = null , string ? token = null )
25+ private static async Task < T > BuildAndSendRequest < T > ( this HttpClient client , HttpMethod method , string requestUri , string ? token , object ? model = null )
4926 {
50- string jsonContent = JsonSerializer . Serialize ( model , DefaultJsonOptions ) ;
51- var content = new StringContent ( jsonContent , Encoding . UTF8 , "application/json" ) ;
27+ var request = new HttpRequestMessage ( method , requestUri ) ;
5228
53- var request = new HttpRequestMessage ( HttpMethod . Put , requestUri )
29+ if ( model is not null )
5430 {
55- Content = content
56- } ;
57-
58- if ( ! string . IsNullOrEmpty ( token ) )
59- {
60- request . Headers . Authorization = new System . Net . Http . Headers . AuthenticationHeaderValue ( "Bearer" , token ) ;
31+ var json = JsonSerializer . Serialize ( model , DefaultJsonOptions ) ;
32+ request . Content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
6133 }
6234
63- HttpResponseMessage response = await client . SendAsync ( request ) ;
64-
65- string text = await response . Content . ReadAsStringAsync ( ) ;
66-
67- return JsonSerializer . Deserialize < T > ( text , DefaultJsonOptions ) ! ;
68- }
69- public static async Task < T > DeleteAndDeserializeAsync < T > ( this HttpClient client , string requestUri , string ? token = null )
70- {
71- var request = new HttpRequestMessage ( HttpMethod . Delete , requestUri ) ;
72-
7335 if ( ! string . IsNullOrEmpty ( token ) )
74- {
75- request . Headers . Authorization = new System . Net . Http . Headers . AuthenticationHeaderValue ( "Bearer" , token ) ;
76- }
36+ request . Headers . Authorization = new ( "Bearer" , token ) ;
7737
7838 HttpResponseMessage response = await client . SendAsync ( request ) ;
7939
0 commit comments