-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCatalogClient.cs
More file actions
35 lines (30 loc) · 1.41 KB
/
Copy pathCatalogClient.cs
File metadata and controls
35 lines (30 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
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
using System.Net.Http.Json;
using HttpQueryDemo.Contracts;
namespace HttpQueryDemo.Client;
/// <summary>
/// A typed <c>HttpClient</c> wrapper that talks to the catalogue API using the
/// HTTP <c>QUERY</c> method. Registered through <c>IHttpClientFactory</c>, so it
/// gets connection pooling and a resilience pipeline for free.
/// </summary>
public sealed class CatalogClient(HttpClient http)
{
/// <summary>
/// Runs a product search by sending the query in the body of an HTTP QUERY
/// request — the client-side counterpart of the server's endpoint.
/// </summary>
public async Task<ProductQueryResult> QueryAsync(
ProductQuery query, CancellationToken cancellationToken = default)
{
// HttpMethod.Query is the first-class QUERY verb introduced in .NET 10.
// Before .NET 10 you'd write new HttpMethod("QUERY") — same wire result.
using var request = new HttpRequestMessage(HttpMethod.Query, "products/query")
{
Content = JsonContent.Create(query, options: ProductJson.Options),
};
using var response = await http.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();
var result = await response.Content
.ReadFromJsonAsync<ProductQueryResult>(ProductJson.Options, cancellationToken);
return result ?? throw new InvalidOperationException("The API returned an empty body.");
}
}