1+ using System . Diagnostics . CodeAnalysis ;
2+ using Tizzani . QueryStringSerializer ;
3+
4+ // ReSharper disable once CheckNamespace
5+ namespace System . Net . Http . Json ;
6+
7+ public static class HttpClientExtensions
8+ {
9+ /// <summary>
10+ /// Sends a GET request to the specified URL.
11+ /// </summary>
12+ /// <param name="client">The client used to send the request.</param>
13+ /// <param name="requestUri">The URI the request is sent to.</param>
14+ /// <param name="requestQuery">The object that should be serialized to the request query string.</param>
15+ /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
16+ /// <returns>The response message.</returns>
17+ public static async Task < HttpResponseMessage > GetWithQueryAsync (
18+ this HttpClient client ,
19+ [ StringSyntax ( "Uri" ) ] string requestUri ,
20+ object ? requestQuery ,
21+ CancellationToken cancellationToken = default )
22+ {
23+ var uri = QueryStringSerializer . Serialize ( requestUri , requestQuery ) ;
24+ return await client . GetAsync ( uri , cancellationToken ) ;
25+ }
26+
27+ /// <summary>
28+ /// Sends a GET request to the specified URL and deserializes the response into an object of type <typeparamref name="T"/>.
29+ /// </summary>
30+ /// <param name="client">The client used to send the request.</param>
31+ /// <param name="requestUri">The URI the request is sent to.</param>
32+ /// <param name="requestQuery">The object that should be serialized to the request query string.</param>
33+ /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
34+ /// <typeparam name="T">The type to deserialize to.</typeparam>
35+ /// <returns>The deserialized response.</returns>
36+ public static async Task < T ? > GetFromJsonWithQueryAsync < T > (
37+ this HttpClient client ,
38+ [ StringSyntax ( "Uri" ) ] string requestUri ,
39+ object ? requestQuery ,
40+ CancellationToken cancellationToken = default )
41+ {
42+ var uri = QueryStringSerializer . Serialize ( requestUri , requestQuery ) ;
43+ return await client . GetFromJsonAsync < T > ( uri , cancellationToken ) ;
44+ }
45+ }
0 commit comments