1
1
using System . Diagnostics . CodeAnalysis ;
2
2
using System . Net ;
3
3
using System . Net . Http . Json ;
4
-
5
4
using Cnblogs . Architecture . Ddd . Infrastructure . Abstractions ;
6
5
7
6
namespace Cnblogs . Architecture . Ddd . Cqrs . ServiceAgent ;
8
7
9
8
/// <summary>
10
- /// ServiceAgent 的基础类。
9
+ /// Base class for service agent.
11
10
/// </summary>
12
- /// <typeparam name="TException">异常类型。 </typeparam>
11
+ /// <typeparam name="TException">The type of exception that this service agent throws. </typeparam>
13
12
public abstract class ServiceAgentBase < TException >
14
13
where TException : Exception , IApiException < TException >
15
14
{
16
15
/// <summary>
17
- /// 构造一个 <see cref="ServiceAgentBase{TException}"/>
16
+ /// Create a <see cref="ServiceAgentBase{TException}"/>.
18
17
/// </summary>
19
- /// <param name="httpClient">用于访问 API 的 <see cref="HttpClient"/>。 </param>
18
+ /// <param name="httpClient">The underlying <see cref="HttpClient"/> used to access the API. </param>
20
19
protected ServiceAgentBase ( HttpClient httpClient )
21
20
{
22
21
HttpClient = httpClient ;
23
22
}
24
23
25
24
/// <summary>
26
- /// 用于访问 API 的 <see cref="HttpClient"/>。
25
+ /// The underlying <see cref="HttpClient"/>.
27
26
/// </summary>
28
27
protected HttpClient HttpClient { get ; }
29
28
30
29
/// <summary>
31
- /// 发送一个 DELETE 请求。
30
+ /// Execute a command with DELETE method.
32
31
/// </summary>
33
- /// <param name="url">目标 API 路径。 </param>
34
- /// <typeparam name="TResponse">返回结果类型。 </typeparam>
35
- /// <returns>返回结果。 </returns>
32
+ /// <param name="url">The url. </param>
33
+ /// <typeparam name="TResponse">Response type. </typeparam>
34
+ /// <returns>The response. </returns>
36
35
public async Task < TResponse ? > DeleteCommandAsync < TResponse > ( string url )
37
36
{
38
37
try
@@ -47,9 +46,9 @@ protected ServiceAgentBase(HttpClient httpClient)
47
46
}
48
47
49
48
/// <summary>
50
- /// 发起一个 DELETE 请求。
49
+ /// Execute a command with DELETE method.
51
50
/// </summary>
52
- /// <param name="url">API 路径。 </param>
51
+ /// <param name="url">The route of the API. </param>
53
52
public async Task DeleteCommandAsync ( string url )
54
53
{
55
54
HttpResponseMessage response ;
@@ -71,9 +70,9 @@ public async Task DeleteCommandAsync(string url)
71
70
}
72
71
73
72
/// <summary>
74
- /// 发起一个 POST 请求。
73
+ /// Execute a command with POST method.
75
74
/// </summary>
76
- /// <param name="url">路径。 </param>
75
+ /// <param name="url">The route of the API. </param>
77
76
public async Task PostCommandAsync ( string url )
78
77
{
79
78
HttpResponseMessage response ;
@@ -95,11 +94,11 @@ public async Task PostCommandAsync(string url)
95
94
}
96
95
97
96
/// <summary>
98
- /// 发起一个带 Body 的 POST 请求。
97
+ /// Execute a command with POST method and payload.
99
98
/// </summary>
100
- /// <param name="url">路径。 </param>
101
- /// <param name="payload">请求。 </param>
102
- /// <typeparam name="TPayload">请求类型。 </typeparam>
99
+ /// <param name="url">The route of the API. </param>
100
+ /// <param name="payload">The request body. </param>
101
+ /// <typeparam name="TPayload">The type of request body. </typeparam>
103
102
public async Task PostCommandAsync < TPayload > ( string url , TPayload payload )
104
103
{
105
104
HttpResponseMessage response ;
@@ -121,13 +120,13 @@ public async Task PostCommandAsync<TPayload>(string url, TPayload payload)
121
120
}
122
121
123
122
/// <summary>
124
- /// 发起一个带 Body 的 POST 请求。
123
+ /// Execute a command with POST method and payload.
125
124
/// </summary>
126
- /// <param name="url">路径。 </param>
127
- /// <param name="payload">请求。 </param>
128
- /// <typeparam name="TResponse">返回类型。 </typeparam>
129
- /// <typeparam name="TPayload">请求类型。 </typeparam>
130
- /// <returns></returns>
125
+ /// <param name="url">The route of the API. </param>
126
+ /// <param name="payload">The request body. </param>
127
+ /// <typeparam name="TResponse">The type of response body. </typeparam>
128
+ /// <typeparam name="TPayload">The type of request body. </typeparam>
129
+ /// <returns>The response body. </returns>
131
130
public async Task < TResponse > PostCommandAsync < TResponse , TPayload > ( string url , TPayload payload )
132
131
{
133
132
HttpResponseMessage response ;
@@ -160,13 +159,63 @@ public async Task<TResponse> PostCommandAsync<TResponse, TPayload>(string url, T
160
159
}
161
160
162
161
/// <summary>
163
- /// 发起一个 PUT 请求。
162
+ /// Execute a command with PUT method and payload.
163
+ /// </summary>
164
+ /// <param name="url">The route of API.</param>
165
+ public async Task PutCommandAsync ( string url )
166
+ {
167
+ HttpResponseMessage response ;
168
+ try
169
+ {
170
+ response = await HttpClient . PutAsync ( url , new StringContent ( string . Empty ) ) ;
171
+ }
172
+ catch ( Exception e )
173
+ {
174
+ ThrowApiException ( HttpMethod . Put , url , e ) ;
175
+ return ;
176
+ }
177
+
178
+ if ( response . IsSuccessStatusCode == false )
179
+ {
180
+ var content = await response . Content . ReadAsStringAsync ( ) ;
181
+ ThrowApiException ( HttpMethod . Put , response . StatusCode , url , content ) ;
182
+ }
183
+ }
184
+
185
+ /// <summary>
186
+ /// Execute a command with PUT method and payload.
187
+ /// </summary>
188
+ /// <param name="url">The route of API.</param>
189
+ /// <param name="payload">The request body.</param>
190
+ /// <typeparam name="TPayload">The type of request body.</typeparam>
191
+ public async Task PutCommandAsync < TPayload > ( string url , TPayload payload )
192
+ {
193
+ HttpResponseMessage response ;
194
+ try
195
+ {
196
+ response = await HttpClient . PutAsJsonAsync ( url , payload ) ;
197
+ }
198
+ catch ( Exception e )
199
+ {
200
+ ThrowApiException ( HttpMethod . Put , url , payload , e ) ;
201
+ return ;
202
+ }
203
+
204
+ if ( response . IsSuccessStatusCode == false )
205
+ {
206
+ var content = await response . Content . ReadAsStringAsync ( ) ;
207
+ ThrowApiException ( HttpMethod . Put , response . StatusCode , url , payload , content ) ;
208
+ }
209
+ }
210
+
211
+ /// <summary>
212
+ /// Execute a command with PUT method and payload.
164
213
/// </summary>
165
- /// <param name="url">路径。 </param>
166
- /// <param name="payload">请求内容。 </param>
167
- /// <typeparam name="TResponse">返回结果类型。 </typeparam>
168
- /// <typeparam name="TPayload">请求类型。 </typeparam>
169
- /// <returns></returns>
214
+ /// <param name="url">The route of API. </param>
215
+ /// <param name="payload">The request body. </param>
216
+ /// <typeparam name="TResponse">The type of response body. </typeparam>
217
+ /// <typeparam name="TPayload">The type of request body. </typeparam>
218
+ /// <returns>The response body. </returns>
170
219
public async Task < TResponse > PutCommandAsync < TResponse , TPayload > ( string url , TPayload payload )
171
220
{
172
221
HttpResponseMessage response ;
@@ -199,10 +248,11 @@ public async Task<TResponse> PutCommandAsync<TResponse, TPayload>(string url, TP
199
248
}
200
249
201
250
/// <summary>
202
- /// 获取内容。
251
+ /// Query item with GET method.
203
252
/// </summary>
204
- /// <param name="url">路径。</param>
205
- /// <typeparam name="T">结果类型。</typeparam>
253
+ /// <param name="url">The route of the API.</param>
254
+ /// <typeparam name="T">The type of item to get.</typeparam>
255
+ /// <returns>The query result, can be null if item does not exists or status code is 404.</returns>
206
256
public async Task < T ? > GetItemAsync < T > ( string url )
207
257
{
208
258
try
@@ -222,14 +272,14 @@ public async Task<TResponse> PutCommandAsync<TResponse, TPayload>(string url, TP
222
272
}
223
273
224
274
/// <summary>
225
- /// 批量获取实体。
275
+ /// Batch get items with GET method.
226
276
/// </summary>
227
- /// <param name="url">路径。 </param>
228
- /// <param name="paramName">参数名称。 </param>
229
- /// <param name="ids">主键列表。 </param>
230
- /// <typeparam name="TResponse">返回类型。 </typeparam>
231
- /// <typeparam name="TId">主键类型。 </typeparam>
232
- /// <returns></returns>
277
+ /// <param name="url">The route of the API. </param>
278
+ /// <param name="paramName">The name of id field. </param>
279
+ /// <param name="ids">The id list. </param>
280
+ /// <typeparam name="TResponse">The type of the query result item. </typeparam>
281
+ /// <typeparam name="TId">The type of the id. </typeparam>
282
+ /// <returns>A list of items that contains id that in <paramref name="ids"/>, the order or count of the items are not guaranteed. </returns>
233
283
public async Task < List < TResponse > > BatchGetItemsAsync < TResponse , TId > (
234
284
string url ,
235
285
string paramName ,
@@ -260,13 +310,13 @@ public async Task<List<TResponse>> BatchGetItemsAsync<TResponse, TId>(
260
310
}
261
311
262
312
/// <summary>
263
- /// 获取分页列表。
313
+ /// Get paged list of items based on url.
264
314
/// </summary>
265
- /// <param name="url">路径。 </param>
266
- /// <param name="pagingParams">页码。 </param>
267
- /// <param name="orderByString">分页大小。 </param>
268
- /// <typeparam name="TItem">实体类型。 </typeparam>
269
- /// <returns></returns>
315
+ /// <param name="url">The route of the API. </param>
316
+ /// <param name="pagingParams">The paging parameters, including page size and page index. </param>
317
+ /// <param name="orderByString">Specifies the order of items to return. </param>
318
+ /// <typeparam name="TItem">The type of items to query. </typeparam>
319
+ /// <returns>The paged list of items. An empty list is returned when there is no result. </returns>
270
320
public async Task < PagedList < TItem > > ListPagedItemsAsync < TItem > (
271
321
string url ,
272
322
PagingParams ? pagingParams = null ,
@@ -276,13 +326,14 @@ public async Task<PagedList<TItem>> ListPagedItemsAsync<TItem>(
276
326
}
277
327
278
328
/// <summary>
279
- /// 获取分页列表。
329
+ /// Get paged list of items based on url.
280
330
/// </summary>
281
- /// <param name="url">路径。</param>
282
- /// <param name="pageIndex">页码。</param>
283
- /// <param name="pageSize">分页大小。</param>
284
- /// <param name="orderByString">排序字符串。</param>
285
- /// <typeparam name="TItem">实体类型。</typeparam>
331
+ /// <param name="url">The route of the API.</param>
332
+ /// <param name="pageIndex">The page index.</param>
333
+ /// <param name="pageSize">The page size.</param>
334
+ /// <param name="orderByString">Specifies the order of items to return.</param>
335
+ /// <typeparam name="TItem">The type of items to query.</typeparam>
336
+ /// <returns>The paged list of items. An empty list is returned when there is no result.</returns>
286
337
public async Task < PagedList < TItem > > ListPagedItemsAsync < TItem > (
287
338
string url ,
288
339
int ? pageIndex ,
@@ -315,14 +366,14 @@ public async Task<PagedList<TItem>> ListPagedItemsAsync<TItem>(
315
366
}
316
367
317
368
/// <summary>
318
- /// 处理抛出异常的情况。
369
+ /// Throw exceptions.
319
370
/// </summary>
320
- /// <param name="method">请求方法。 </param>
321
- /// <param name="statusCode">状态码,若不适用则是 -1。 </param>
322
- /// <param name="url">请求的 Url </param>
323
- /// <param name="requestBody">请求内容。 </param>
324
- /// <param name="response">返回内容。 </param>
325
- /// <param name="e">异常。 </param>
371
+ /// <param name="method">The method for this request. </param>
372
+ /// <param name="statusCode">HTTP status code, -1 if not available. </param>
373
+ /// <param name="url">The URL to request. </param>
374
+ /// <param name="requestBody">The request body. </param>
375
+ /// <param name="response">The response body. </param>
376
+ /// <param name="e">The exception. </param>
326
377
[ DoesNotReturn ]
327
378
protected virtual void ThrowApiException (
328
379
HttpMethod method ,
@@ -352,4 +403,4 @@ private void ThrowApiException(
352
403
353
404
private void ThrowApiException ( HttpMethod method , string url , object ? body , Exception e )
354
405
=> ThrowApiException ( method , - 1 , url , body , null , e ) ;
355
- }
406
+ }
0 commit comments