11using System . Diagnostics . CodeAnalysis ;
2+ using System . Text . Json ;
23using Tizzani . QueryStringSerializer ;
34
45// ReSharper disable once CheckNamespace
56namespace System . Net . Http . Json ;
67
8+ /// <summary>
9+ /// Extension methods for <see cref="HttpClient"/> to support query string serialization.
10+ /// </summary>
711public static class HttpClientExtensions
812{
913 /// <summary>
@@ -14,13 +18,35 @@ public static class HttpClientExtensions
1418 /// <param name="requestQuery">The object that should be serialized to the request query string.</param>
1519 /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
1620 /// <returns>The response message.</returns>
21+ public static Task < HttpResponseMessage > GetWithQueryAsync (
22+ this HttpClient client ,
23+ [ StringSyntax ( "Uri" ) ] string requestUri ,
24+ object ? requestQuery ,
25+ CancellationToken cancellationToken = default ) =>
26+ client . GetWithQueryAsync (
27+ requestUri ,
28+ requestQuery ,
29+ QueryStringSerializer . DefaultSerializerOptions ,
30+ cancellationToken
31+ ) ;
32+
33+ /// <summary>
34+ /// Sends a GET request to the specified URL.
35+ /// </summary>
36+ /// <param name="client">The client used to send the request.</param>
37+ /// <param name="requestUri">The URI the request is sent to.</param>
38+ /// <param name="requestQuery">The object that should be serialized to the request query string.</param>
39+ /// <param name="jsonSerializerOptions">The <see cref="JsonSerializerOptions"/> to use for serialization.</param>
40+ /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
41+ /// <returns>The response message.</returns>
1742 public static async Task < HttpResponseMessage > GetWithQueryAsync (
1843 this HttpClient client ,
1944 [ StringSyntax ( "Uri" ) ] string requestUri ,
2045 object ? requestQuery ,
46+ JsonSerializerOptions jsonSerializerOptions ,
2147 CancellationToken cancellationToken = default )
2248 {
23- var uri = QueryStringSerializer . Serialize ( requestUri , requestQuery ) ;
49+ var uri = QueryStringSerializer . Serialize ( requestUri , requestQuery , jsonSerializerOptions ) ;
2450 return await client . GetAsync ( uri , cancellationToken ) ;
2551 }
2652
@@ -33,13 +59,36 @@ public static async Task<HttpResponseMessage> GetWithQueryAsync(
3359 /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
3460 /// <typeparam name="T">The type to deserialize to.</typeparam>
3561 /// <returns>The deserialized response.</returns>
62+ public static Task < T ? > GetFromJsonWithQueryAsync < T > (
63+ this HttpClient client ,
64+ [ StringSyntax ( "Uri" ) ] string requestUri ,
65+ object ? requestQuery ,
66+ CancellationToken cancellationToken = default ) =>
67+ client . GetFromJsonWithQueryAsync < T > (
68+ requestUri ,
69+ requestQuery ,
70+ QueryStringSerializer . DefaultSerializerOptions ,
71+ cancellationToken
72+ ) ;
73+
74+ /// <summary>
75+ /// Sends a GET request to the specified URL and deserializes the response into an object of type <typeparamref name="T"/>.
76+ /// </summary>
77+ /// <param name="client">The client used to send the request.</param>
78+ /// <param name="requestUri">The URI the request is sent to.</param>
79+ /// <param name="requestQuery">The object that should be serialized to the request query string.</param>
80+ /// <param name="jsonSerializerOptions">The <see cref="JsonSerializerOptions"/> to use for serialization.</param>
81+ /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
82+ /// <typeparam name="T">The type to deserialize to.</typeparam>
83+ /// <returns>The deserialized response.</returns>
3684 public static async Task < T ? > GetFromJsonWithQueryAsync < T > (
3785 this HttpClient client ,
3886 [ StringSyntax ( "Uri" ) ] string requestUri ,
3987 object ? requestQuery ,
88+ JsonSerializerOptions jsonSerializerOptions ,
4089 CancellationToken cancellationToken = default )
4190 {
42- var uri = QueryStringSerializer . Serialize ( requestUri , requestQuery ) ;
43- return await client . GetFromJsonAsync < T > ( uri , cancellationToken ) ;
91+ var uri = QueryStringSerializer . Serialize ( requestUri , requestQuery , jsonSerializerOptions ) ;
92+ return await client . GetFromJsonAsync < T > ( uri , jsonSerializerOptions , cancellationToken ) ;
4493 }
4594}
0 commit comments