-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderClient.cs
More file actions
121 lines (97 loc) · 4.25 KB
/
OrderClient.cs
File metadata and controls
121 lines (97 loc) · 4.25 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
namespace Comanda.Internal.Contracts.Clients;
public sealed class OrderClient(HttpClient httpClient) : IOrderClient
{
private readonly JsonSerializerOptions serializerOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true
};
public async Task<Result<PaginationScheme<OrderScheme>>> GetOrdersAsync(OrdersFetchParameters parameters, CancellationToken cancellation = default)
{
var queryString = QueryParametersParser.ToQueryString(parameters);
var response = await httpClient.GetAsync($"orders?{queryString}", cancellation);
var content = await response.Content.ReadAsStringAsync(cancellation);
var error = response.StatusCode switch
{
HttpStatusCode.Unauthorized => CommonErrors.UnauthorizedAccess,
HttpStatusCode.Forbidden => CommonErrors.UnauthorizedAccess,
HttpStatusCode.InternalServerError => CommonErrors.OperationFailed,
_ => null
};
if (error is not null)
{
return Result<PaginationScheme<OrderScheme>>.Failure(error);
}
var metadataHeader = response.Headers
.GetValues("X-Pagination")
.FirstOrDefault();
if (metadataHeader is null)
{
return Result<PaginationScheme<OrderScheme>>.Failure(CommonErrors.InvalidContent);
}
var metadata = JsonSerializer.Deserialize<PaginationMetadata>(metadataHeader, serializerOptions);
if (metadata is null)
{
return Result<PaginationScheme<OrderScheme>>.Failure(CommonErrors.InvalidContent);
}
var items = JsonSerializer.Deserialize<IEnumerable<OrderScheme>>(content, serializerOptions);
if (items is null)
{
return Result<PaginationScheme<OrderScheme>>.Failure(CommonErrors.InvalidContent);
}
var result = new PaginationScheme<OrderScheme>
{
Items = [.. items],
Total = metadata.Total,
PageNumber = metadata.PageNumber,
PageSize = metadata.PageSize
};
return Result<PaginationScheme<OrderScheme>>.Success(result);
}
public async Task<Result<OrderScheme>> CreateOrderAsync(OrderCreationScheme parameters, CancellationToken cancellation = default)
{
var response = await httpClient.PostAsJsonAsync("orders", parameters, cancellation);
var content = await response.Content.ReadAsStringAsync(cancellation);
var error = response.StatusCode switch
{
HttpStatusCode.Unauthorized => CommonErrors.UnauthorizedAccess,
HttpStatusCode.Forbidden => CommonErrors.UnauthorizedAccess,
HttpStatusCode.InternalServerError => CommonErrors.OperationFailed,
_ => null
};
if (error is not null)
{
return Result<OrderScheme>.Failure(error);
}
var order = JsonSerializer.Deserialize<OrderScheme>(content, serializerOptions);
if (order is null)
{
return Result<OrderScheme>.Failure(CommonErrors.InvalidContent);
}
return Result<OrderScheme>.Success(order);
}
public async Task<Result<OrderScheme>> UpdateOrderAsync(
OrderModificationScheme parameters, CancellationToken cancellation = default)
{
var response = await httpClient.PutAsJsonAsync($"orders/{parameters.Id}", parameters, cancellation);
var content = await response.Content.ReadAsStringAsync(cancellation);
var error = response.StatusCode switch
{
HttpStatusCode.NotFound => OrderErrors.OrderDoesNotExist,
HttpStatusCode.Unauthorized => CommonErrors.UnauthorizedAccess,
HttpStatusCode.Forbidden => CommonErrors.UnauthorizedAccess,
HttpStatusCode.InternalServerError => CommonErrors.OperationFailed,
_ => null
};
if (error is not null)
{
return Result<OrderScheme>.Failure(error);
}
var order = JsonSerializer.Deserialize<OrderScheme>(content, serializerOptions);
if (order is null)
{
return Result<OrderScheme>.Failure(CommonErrors.InvalidContent);
}
return Result<OrderScheme>.Success(order);
}
}