Skip to content

Commit e7ad2f2

Browse files
authored
Merge pull request #150 from dotnetcore/feature/CompletionOption
Feature/completion option
2 parents 6cdbfa6 + ff444ac commit e7ad2f2

File tree

6 files changed

+70
-15
lines changed

6 files changed

+70
-15
lines changed

App/Clients/IUserApi.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace App.Clients
1515
[LoggingFilter]
1616
[OAuthToken]
1717
[HttpHost("http://localhost:6000/")]
18+
[JsonNetReturn]
1819
public interface IUserApi : IHttpApi
1920
{
2021
[HttpGet("api/users/{account}")]
@@ -39,6 +40,7 @@ public interface IUserApi : IHttpApi
3940
ITask<byte[]> GetAsByteArrayAsync([Required] string account, CancellationToken token = default);
4041

4142
[HttpGet("api/users/{account}")]
43+
[HttpCompletionOption(HttpCompletionOption.ResponseHeadersRead)]
4244
ITask<Stream> GetAsStreamAsync([Required] string account, CancellationToken token = default);
4345

4446
[HttpGet("api/users/{account}")]

WebApiClientCore.Extensions.NewtonsoftJson/Attributes/JsonNetReturnAttribute.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,19 @@ public JsonNetReturnAttribute(double acceptQuality)
3535
/// <returns></returns>
3636
public override async Task SetResultAsync(ApiResponseContext context)
3737
{
38-
var response = context.HttpContext.ResponseMessage;
39-
if (response == null || response.Content == null)
38+
var content = context.HttpContext.ResponseMessage?.Content;
39+
if (content == null)
4040
{
4141
return;
4242
}
4343

44-
if (context.ApiAction.Return.DataType.IsRawType == false)
45-
{
46-
var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
47-
var resultType = context.ApiAction.Return.DataType.Type;
44+
var json = await content.ReadAsStringAsync().ConfigureAwait(false);
45+
var resultType = context.ApiAction.Return.DataType.Type;
4846

49-
var name = context.HttpContext.OptionsName;
50-
var options = context.HttpContext.ServiceProvider.GetService<IOptionsMonitor<JsonNetSerializerOptions>>().Get(name);
47+
var name = context.HttpContext.OptionsName;
48+
var options = context.HttpContext.ServiceProvider.GetService<IOptionsMonitor<JsonNetSerializerOptions>>().Get(name);
5149

52-
context.Result = JsonConvert.DeserializeObject(json, resultType, options.JsonDeserializeOptions);
53-
}
50+
context.Result = JsonConvert.DeserializeObject(json, resultType, options.JsonDeserializeOptions);
5451
}
5552
}
5653
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
4+
5+
namespace WebApiClientCore.Attributes
6+
{
7+
/// <summary>
8+
/// 指示请求完成选项的特性
9+
/// </summary>
10+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
11+
public class HttpCompletionOptionAttribute : ApiActionAttribute
12+
{
13+
/// <summary>
14+
/// 请求完成选项
15+
/// </summary>
16+
private readonly HttpCompletionOption completionOption;
17+
18+
/// <summary>
19+
/// 指示请求完成选项的特性
20+
/// </summary>
21+
/// <param name="completionOption">请求完成选项</param>
22+
public HttpCompletionOptionAttribute(HttpCompletionOption completionOption)
23+
{
24+
this.completionOption = completionOption;
25+
}
26+
27+
/// <summary>
28+
/// 执行前
29+
/// </summary>
30+
/// <param name="context">上下文</param>
31+
/// <returns></returns>
32+
public override Task OnRequestAsync(ApiRequestContext context)
33+
{
34+
context.HttpContext.CompletionOption = this.completionOption;
35+
return Task.CompletedTask;
36+
}
37+
}
38+
}

WebApiClientCore/Attributes/FilterAttributes/LoggingFilterAttribute.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public sealed async override Task OnResponseAsync(ApiResponseContext context)
8585
{
8686
logMessage.HasResponse = true;
8787
logMessage.ResponseHeaders = response.GetHeadersString();
88-
logMessage.ResponseContent = await this.ReadResponseContentAsync(response).ConfigureAwait(false);
88+
logMessage.ResponseContent = await this.ReadResponseContentAsync(context).ConfigureAwait(false);
8989
}
9090

9191
await this.WriteLogAsync(context, logMessage).ConfigureAwait(false);
@@ -111,11 +111,23 @@ public sealed async override Task OnResponseAsync(ApiResponseContext context)
111111
/// <summary>
112112
/// 读取响应内容
113113
/// </summary>
114-
/// <param name="response"></param>
114+
/// <param name="context"></param>
115115
/// <returns></returns>
116-
private async Task<string?> ReadResponseContentAsync(HttpResponseMessage response)
116+
private async Task<string?> ReadResponseContentAsync(ApiResponseContext context)
117117
{
118-
return response.Content == null ? null : await response.Content.ReadAsStringAsync().ConfigureAwait(false);
118+
var content = context.HttpContext.ResponseMessage?.Content;
119+
if (content == null)
120+
{
121+
return null;
122+
}
123+
124+
if (content.IsBuffered() == true ||
125+
context.HttpContext.CompletionOption == HttpCompletionOption.ResponseContentRead)
126+
{
127+
return await content.ReadAsStringAsync().ConfigureAwait(false);
128+
}
129+
130+
return "...";
119131
}
120132

121133
/// <summary>

WebApiClientCore/BuildInProxies/Invokers/HttpRequest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ public static async Task<ApiResponseContext> SendAsync(ApiRequestContext context
3737
{
3838
var client = context.HttpContext.HttpClient;
3939
var request = context.HttpContext.RequestMessage;
40+
var completionOption = context.HttpContext.CompletionOption;
4041
using var tokenLinker = new CancellationTokenLinker(context.HttpContext.CancellationTokens);
41-
var response = await client.SendAsync(request, tokenLinker.Token).ConfigureAwait(false);
42+
var response = await client.SendAsync(request, completionOption, tokenLinker.Token).ConfigureAwait(false);
4243

4344
context.HttpContext.ResponseMessage = response;
4445
await SetCacheAsync(context, actionCache?.Key, response).ConfigureAwait(false);

WebApiClientCore/HttpContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ namespace WebApiClientCore
1010
/// </summary>
1111
public class HttpContext : HttpClientContext, IDisposable
1212
{
13+
/// <summary>
14+
/// 获取或设置指示请求完成选项
15+
/// </summary>
16+
public HttpCompletionOption CompletionOption { get; set; }
17+
1318
/// <summary>
1419
/// 获取请求取消令牌集合
1520
/// </summary>

0 commit comments

Comments
 (0)