1
1
using System . Net ;
2
2
using System . Net . Http . Json ;
3
3
using Cnblogs . Architecture . Ddd . Cqrs . Abstractions ;
4
+ using Cnblogs . Architecture . Ddd . Domain . Abstractions ;
4
5
using Cnblogs . Architecture . Ddd . Infrastructure . Abstractions ;
5
6
6
7
namespace Cnblogs . Architecture . Ddd . Cqrs . ServiceAgent ;
7
8
9
+ /// <summary>
10
+ /// Base Class for CQRS Service Agent.
11
+ /// </summary>
12
+ public abstract class CqrsServiceAgent : CqrsServiceAgent < ServiceAgentError >
13
+ {
14
+ /// <summary>
15
+ /// Create a Cqrs service agent.
16
+ /// </summary>
17
+ /// <param name="httpClient">The underlying http client.</param>
18
+ protected CqrsServiceAgent ( HttpClient httpClient )
19
+ : base ( httpClient )
20
+ {
21
+ }
22
+ }
23
+
8
24
/// <summary>
9
25
/// Service Agent for CQRS
10
26
/// </summary>
11
- public abstract class CqrsServiceAgent
27
+ /// <typeparam name="TError">The type of error for this service.</typeparam>
28
+ public abstract class CqrsServiceAgent < TError >
29
+ where TError : Enumeration
12
30
{
13
31
/// <summary>
14
32
/// The underlying <see cref="HttpClient"/>.
@@ -30,7 +48,7 @@ protected CqrsServiceAgent(HttpClient httpClient)
30
48
/// <param name="url">The url.</param>
31
49
/// <typeparam name="TResponse">Response type.</typeparam>
32
50
/// <returns>The response.</returns>
33
- public async Task < CommandResponse < TResponse , ServiceAgentError > > DeleteCommandAsync < TResponse > ( string url )
51
+ public async Task < CommandResponse < TResponse , TError > > DeleteCommandAsync < TResponse > ( string url )
34
52
{
35
53
var response = await HttpClient . DeleteAsync ( url ) ;
36
54
return await HandleCommandResponseAsync < TResponse > ( response ) ;
@@ -40,7 +58,7 @@ public async Task<CommandResponse<TResponse, ServiceAgentError>> DeleteCommandAs
40
58
/// Execute a command with DELETE method.
41
59
/// </summary>
42
60
/// <param name="url">The route of the API.</param>
43
- public async Task < CommandResponse < ServiceAgentError > > DeleteCommandAsync ( string url )
61
+ public async Task < CommandResponse < TError > > DeleteCommandAsync ( string url )
44
62
{
45
63
var response = await HttpClient . DeleteAsync ( url ) ;
46
64
return await HandleCommandResponseAsync ( response ) ;
@@ -50,7 +68,7 @@ public async Task<CommandResponse<ServiceAgentError>> DeleteCommandAsync(string
50
68
/// Execute a command with POST method.
51
69
/// </summary>
52
70
/// <param name="url">The route of the API.</param>
53
- public async Task < CommandResponse < ServiceAgentError > > PostCommandAsync ( string url )
71
+ public async Task < CommandResponse < TError > > PostCommandAsync ( string url )
54
72
{
55
73
var response = await HttpClient . PostAsync ( url , new StringContent ( string . Empty ) ) ;
56
74
return await HandleCommandResponseAsync ( response ) ;
@@ -62,7 +80,7 @@ public async Task<CommandResponse<ServiceAgentError>> PostCommandAsync(string ur
62
80
/// <param name="url">The route of the API.</param>
63
81
/// <param name="payload">The request body.</param>
64
82
/// <typeparam name="TPayload">The type of request body.</typeparam>
65
- public async Task < CommandResponse < ServiceAgentError > > PostCommandAsync < TPayload > ( string url , TPayload payload )
83
+ public async Task < CommandResponse < TError > > PostCommandAsync < TPayload > ( string url , TPayload payload )
66
84
{
67
85
var response = await HttpClient . PostAsJsonAsync ( url , payload ) ;
68
86
return await HandleCommandResponseAsync ( response ) ;
@@ -76,7 +94,7 @@ public async Task<CommandResponse<ServiceAgentError>> PostCommandAsync<TPayload>
76
94
/// <typeparam name="TResponse">The type of response body.</typeparam>
77
95
/// <typeparam name="TPayload">The type of request body.</typeparam>
78
96
/// <returns>The response body.</returns>
79
- public async Task < CommandResponse < TResponse , ServiceAgentError > > PostCommandAsync < TResponse , TPayload > (
97
+ public async Task < CommandResponse < TResponse , TError > > PostCommandAsync < TResponse , TPayload > (
80
98
string url ,
81
99
TPayload payload )
82
100
{
@@ -88,7 +106,7 @@ public async Task<CommandResponse<TResponse, ServiceAgentError>> PostCommandAsyn
88
106
/// Execute a command with PUT method and payload.
89
107
/// </summary>
90
108
/// <param name="url">The route of API.</param>
91
- public async Task < CommandResponse < ServiceAgentError > > PutCommandAsync ( string url )
109
+ public async Task < CommandResponse < TError > > PutCommandAsync ( string url )
92
110
{
93
111
var response = await HttpClient . PutAsync ( url , new StringContent ( string . Empty ) ) ;
94
112
return await HandleCommandResponseAsync ( response ) ;
@@ -101,7 +119,7 @@ public async Task<CommandResponse<ServiceAgentError>> PutCommandAsync(string url
101
119
/// <param name="payload">The request body.</param>
102
120
/// <typeparam name="TPayload">The type of request body.</typeparam>
103
121
/// <returns>The command response.</returns>
104
- public async Task < CommandResponse < ServiceAgentError > > PutCommandAsync < TPayload > ( string url , TPayload payload )
122
+ public async Task < CommandResponse < TError > > PutCommandAsync < TPayload > ( string url , TPayload payload )
105
123
{
106
124
var response = await HttpClient . PutAsJsonAsync ( url , payload ) ;
107
125
return await HandleCommandResponseAsync ( response ) ;
@@ -115,7 +133,7 @@ public async Task<CommandResponse<ServiceAgentError>> PutCommandAsync<TPayload>(
115
133
/// <typeparam name="TResponse">The type of response body.</typeparam>
116
134
/// <typeparam name="TPayload">The type of request body.</typeparam>
117
135
/// <returns>The response body.</returns>
118
- public async Task < CommandResponse < TResponse , ServiceAgentError > > PutCommandAsync < TResponse , TPayload > (
136
+ public async Task < CommandResponse < TResponse , TError > > PutCommandAsync < TResponse , TPayload > (
119
137
string url ,
120
138
TPayload payload )
121
139
{
@@ -237,54 +255,44 @@ public async Task<TList> ListItemsAsync<TList>(string url)
237
255
return await HttpClient . GetFromJsonAsync < TList > ( url ) ?? new TList ( ) ;
238
256
}
239
257
240
- private static async Task < CommandResponse < TResponse , ServiceAgentError > > HandleCommandResponseAsync < TResponse > (
258
+ private static async Task < CommandResponse < TResponse , TError > > HandleCommandResponseAsync < TResponse > (
241
259
HttpResponseMessage httpResponseMessage )
242
260
{
243
- if ( httpResponseMessage . IsSuccessStatusCode )
261
+ if ( httpResponseMessage . StatusCode == HttpStatusCode . NoContent )
262
+ {
263
+ return CommandResponse < TResponse , TError > . Success ( ) ;
264
+ }
265
+
266
+ if ( httpResponseMessage . StatusCode == HttpStatusCode . OK )
244
267
{
245
268
var result = await httpResponseMessage . Content . ReadFromJsonAsync < TResponse > ( ) ;
246
- return CommandResponse < TResponse , ServiceAgentError > . Success ( result ) ;
269
+ return CommandResponse < TResponse , TError > . Success ( result ) ;
247
270
}
248
271
249
- var response = await httpResponseMessage . Content . ReadFromJsonAsync < CommandResponse > ( ) ;
272
+ var response = await httpResponseMessage . Content . ReadFromJsonAsync < CommandResponse < TResponse , TError > > ( ) ;
250
273
if ( response is null )
251
274
{
252
- return CommandResponse < TResponse , ServiceAgentError > . Fail ( ServiceAgentError . UnknownError ) ;
275
+ throw new InvalidOperationException (
276
+ $ "Could not deserialize error from response, response: { await httpResponseMessage . Content . ReadAsStringAsync ( ) } ") ;
253
277
}
254
278
255
- return new CommandResponse < TResponse , ServiceAgentError >
256
- {
257
- IsConcurrentError = response . IsConcurrentError ,
258
- IsValidationError = response . IsValidationError ,
259
- ErrorMessage = response . ErrorMessage ,
260
- LockAcquired = response . LockAcquired ,
261
- ValidationErrors = response . ValidationErrors ,
262
- ErrorCode = new ServiceAgentError ( 1 , response . ErrorMessage )
263
- } ;
279
+ return response ;
264
280
}
265
281
266
- private static async Task < CommandResponse < ServiceAgentError > > HandleCommandResponseAsync (
267
- HttpResponseMessage message )
282
+ private static async Task < CommandResponse < TError > > HandleCommandResponseAsync ( HttpResponseMessage message )
268
283
{
269
284
if ( message . IsSuccessStatusCode )
270
285
{
271
- return CommandResponse < ServiceAgentError > . Success ( ) ;
286
+ return CommandResponse < TError > . Success ( ) ;
272
287
}
273
288
274
- var response = await message . Content . ReadFromJsonAsync < CommandResponse > ( ) ;
289
+ var response = await message . Content . ReadFromJsonAsync < CommandResponse < TError > > ( ) ;
275
290
if ( response is null )
276
291
{
277
- return CommandResponse < ServiceAgentError > . Fail ( ServiceAgentError . UnknownError ) ;
292
+ throw new InvalidOperationException (
293
+ $ "Could not deserialize error from response, response: { await message . Content . ReadAsStringAsync ( ) } ") ;
278
294
}
279
295
280
- return new CommandResponse < ServiceAgentError >
281
- {
282
- IsConcurrentError = response . IsConcurrentError ,
283
- IsValidationError = response . IsValidationError ,
284
- ErrorMessage = response . ErrorMessage ,
285
- LockAcquired = response . LockAcquired ,
286
- ValidationErrors = response . ValidationErrors ,
287
- ErrorCode = new ServiceAgentError ( 1 , response . ErrorMessage )
288
- } ;
296
+ return response ;
289
297
}
290
298
}
0 commit comments