7
7
| 包名 | 描述 | Nuget |
8
8
---|---|--|
9
9
| WebApiClientCore | 基础包 | [ ![ NuGet] ( https://buildstats.info/nuget/WebApiClientCore )] ( https://www.nuget.org/packages/WebApiClientCore ) |
10
- | WebApiClientCore.Extensions.OAuths | OAuth扩展包 | [ ![ NuGet] ( https://buildstats.info/nuget/WebApiClientCore.Extensions.OAuths )] ( https://www.nuget.org/packages/WebApiClientCore.Extensions.OAuths ) |
10
+ | WebApiClientCore.Extensions.SourceGenerator | 编译时代理类生成包 | [ ![ NuGet] ( https://buildstats.info/nuget/WebApiClientCore.Extensions.SourceGenerator )] ( https://www.nuget.org/packages/WebApiClientCore.Extensions.SourceGenerator ) |
11
+ | WebApiClientCore.Extensions.OAuths | OAuth2与token管理扩展包 | [ ![ NuGet] ( https://buildstats.info/nuget/WebApiClientCore.Extensions.OAuths )] ( https://www.nuget.org/packages/WebApiClientCore.Extensions.OAuths ) |
11
12
| WebApiClientCore.Extensions.NewtonsoftJson | Json.Net扩展包 | [ ![ NuGet] ( https://buildstats.info/nuget/WebApiClientCore.Extensions.NewtonsoftJson )] ( https://www.nuget.org/packages/WebApiClientCore.Extensions.NewtonsoftJson ) |
12
13
| WebApiClientCore.Extensions.JsonRpc | JsonRpc调用扩展包 | [ ![ NuGet] ( https://buildstats.info/nuget/WebApiClientCore.Extensions.JsonRpc )] ( https://www.nuget.org/packages/WebApiClientCore.Extensions.JsonRpc ) |
13
14
| WebApiClientCore.OpenApi.SourceGenerator | 将本地或远程OpenApi文档解析生成WebApiClientCore接口代码的dotnet tool | [ ![ NuGet] ( https://buildstats.info/nuget/WebApiClientCore.OpenApi.SourceGenerator )] ( https://www.nuget.org/packages/WebApiClientCore.OpenApi.SourceGenerator ) |
@@ -49,22 +50,17 @@ public class MyService
49
50
进群时请注明** WebApiClient** ,在咨询问题之前,请先认真阅读以下剩余的文档,避免消耗作者不必要的重复解答时间。
50
51
51
52
### 编译时语法分析
52
- WebApiClientCore.Analyzers提供编码时语法分析与提示,声明的接口继承了空方法的IHttpApi接口,语法分析将生效,建议开发者开启这个功能 。
53
+ WebApiClientCore.Analyzers提供接口声明的语法分析与提示,帮助开发者声明接口时避免使用不当的语法 。
53
54
54
- 例如[ Header] 特性,可以声明在Interface、Method和Parameter三个地方,但是必须使用正确的构造器,否则运行时会抛出异常。有了语法分析功能,在声明接口时就不会使用不当的语法。
55
-
56
- ```
57
- /// <summary>
58
- /// 记得要实现IHttpApi
59
- /// </summary>
60
- public interface IUserApi : IHttpApi
61
- {
62
- ...
63
- }
64
- ```
55
+ * 1.x版本,接口继承IHttpApi才获得语法分析提示
56
+ * 2.0以后的版本,不继承IHttpApi也获得语法分析提示
57
+
58
+ ### 全局配置
59
+ 2.0以后的版本,提供services.AddWebApiClient()的全局配置功能,支持提供自定义的IHttpApiActivator<>、IApiActionDescriptorProvider、IApiActionInvokerProvider和IResponseCacheProvider。
65
60
66
- ### 接口配置与选项
67
- 每个接口的选项对应为` HttpApiOptions ` ,选项名称为接口的完整名称,也可以通过HttpApi.GetName()方法获取得到。
61
+ ### 接口注册与选项
62
+ 调用services.AddHttpApi<IUserApi >()即可完成接口注册,
63
+ 每个接口的选项对应为` HttpApiOptions ` ,选项名称通过HttpApi.GetName()方法获取得到。
68
64
69
65
#### 在IHttpClientBuilder配置
70
66
```
@@ -74,7 +70,7 @@ services
74
70
.ConfigureHttpApi(o =>
75
71
{
76
72
// 符合国情的不标准时间格式,有些接口就是这么要求必须不标准
77
- o.JsonSerializeOptions.Converters.Add(new JsonLocalDateTimeConverter ("yyyy-MM-dd HH:mm:ss"));
73
+ o.JsonSerializeOptions.Converters.Add(new JsonDateTimeConverter ("yyyy-MM-dd HH:mm:ss"));
78
74
});
79
75
```
80
76
@@ -100,7 +96,7 @@ services
100
96
.ConfigureHttpApi<IUserApi>(o =>
101
97
{
102
98
// 符合国情的不标准时间格式,有些接口就是这么要求必须不标准
103
- o.JsonSerializeOptions.Converters.Add(new JsonLocalDateTimeConverter ("yyyy-MM-dd HH:mm:ss"));
99
+ o.JsonSerializeOptions.Converters.Add(new JsonDateTimeConverter ("yyyy-MM-dd HH:mm:ss"));
104
100
});
105
101
```
106
102
@@ -337,7 +333,6 @@ await response.SaveAsAsync(fileStream);
337
333
```
338
334
339
335
### 接口声明示例
340
- #### Petstore接口
341
336
这个OpenApi文档在[ petstore.swagger.io] ( https://petstore.swagger.io/ ) ,代码为使用WebApiClientCore.OpenApi.SourceGenerator工具将其OpenApi文档反向生成得到
342
337
343
338
```
@@ -426,59 +421,7 @@ public interface IPetApi : IHttpApi
426
421
[HttpPost("pet/{petId}/uploadImage")]
427
422
ITask<ApiResponse> UploadFileAsync([Required] long petId, [FormDataText] string additionalMetadata, FormDataFile file, CancellationToken cancellationToken = default);
428
423
}
429
- ```
430
- #### IOAuthClient接口
431
- 这个接口是在WebApiClientCore.Extensions.OAuths.IOAuthClient.cs代码中声明
432
-
433
- ```
434
- using System;
435
- using System.ComponentModel.DataAnnotations;
436
- using System.Diagnostics.CodeAnalysis;
437
- using System.Threading.Tasks;
438
- using WebApiClientCore.Attributes;
439
-
440
- namespace WebApiClientCore.Extensions.OAuths
441
- {
442
- /// <summary>
443
- /// 定义Token客户端的接口
444
- /// </summary>
445
- [LoggingFilter]
446
- [XmlReturn(Enable = false)]
447
- [JsonReturn(EnsureMatchAcceptContentType = false, EnsureSuccessStatusCode = false)]
448
- public interface IOAuthClient
449
- {
450
- /// <summary>
451
- /// 以client_credentials授权方式获取token
452
- /// </summary>
453
- /// <param name="endpoint">token请求地址</param>
454
- /// <param name="credentials">身份信息</param>
455
- /// <returns></returns>
456
- [HttpPost]
457
- [FormField("grant_type", "client_credentials")]
458
- Task<TokenResult> RequestTokenAsync([Required, Uri] Uri endpoint, [Required, FormContent] ClientCredentials credentials);
459
-
460
- /// <summary>
461
- /// 以password授权方式获取token
462
- /// </summary>
463
- /// <param name="endpoint">token请求地址</param>
464
- /// <param name="credentials">身份信息</param>
465
- /// <returns></returns>
466
- [HttpPost]
467
- [FormField("grant_type", "password")]
468
- Task<TokenResult> RequestTokenAsync([Required, Uri] Uri endpoint, [Required, FormContent] PasswordCredentials credentials);
469
-
470
- /// <summary>
471
- /// 刷新token
472
- /// </summary>
473
- /// <param name="endpoint">token请求地址</param>
474
- /// <param name="credentials">身份信息</param>
475
- /// <returns></returns>
476
- [HttpPost]
477
- [FormField("grant_type", "refresh_token")]
478
- Task<TokenResult> RefreshTokenAsync([Required, Uri] Uri endpoint, [Required, FormContent] RefreshTokenCredentials credentials);
479
- }
480
- }
481
- ```
424
+ ```
482
425
483
426
### 请求条件性重试
484
427
使用ITask<>异步声明,就有Retry的扩展,Retry的条件可以为捕获到某种Exception或响应模型符合某种条件。
@@ -616,11 +559,15 @@ public class RedisResponseCacheProvider : IResponseCacheProvider
616
559
{
617
560
throw new NotImplementedException();
618
561
}
619
- }
562
+ }
563
+ ```
620
564
621
- // 注册RedisResponseCacheProvider
622
- var services = new ServiceCollection();
623
- services.AddSingleton<IResponseCacheProvider, RedisResponseCacheProvider>();
565
+ ```
566
+ public static IWebApiClientBuilder UseRedisResponseCacheProvider(this IWebApiClientBuilder builder)
567
+ {
568
+ builder.Services.AddSingleton<IResponseCacheProvider, RedisResponseCacheProvider>();
569
+ return builder;
570
+ }
624
571
```
625
572
626
573
### 非模型请求
@@ -996,14 +943,25 @@ services
996
943
997
944
现在,调用IUserApi的任意接口,只要响应的状态码为401,就触发IUserLoginApi登录,然后将登录得到的cookie来重试请求接口,最终响应为正确的结果。你也可以重写CookieAuthorizationHandler的IsUnauthorizedAsync(HttpResponseMessage)方法来指示响应是未授权状态。
998
945
946
+ ### SourceGenerator
947
+ SourceGenerator是一种新的c#编译时代码补充生成技术,可以非常方便的为WebApiClient生成接口的代理实现类。使用WebApiClientCore.Extensions.SourceGenerator扩展包,可以替换默认的内置Emit生成代理类的方案,支持需要完全AOT编译的平台。
948
+
949
+ 要启用SourceGenerator功能,需要如下配置启用
950
+ ```
951
+ // 应用编译时生成接口的代理类型代码
952
+ services
953
+ .AddWebApiClient()
954
+ .UseSourceGeneratorHttpApiActivator();
955
+ ```
956
+
999
957
### OAuths&Token
1000
958
使用WebApiClientCore.Extensions.OAuths扩展,轻松支持token的获取、刷新与应用。
1001
959
1002
960
#### 对象与概念
1003
961
1004
962
对象 | 用途
1005
963
---|---
1006
- ITokenProviderFactory | tokenProvider的创建工厂,提供通过HttpApi接口类型创建tokenProvider
964
+ ITokenProviderFactory | tokenProvider的创建工厂,提供通过HttpApi接口类型获取或创建tokenProvider
1007
965
ITokenProvider | token提供者,用于获取token,在token的过期后的头一次请求里触发重新请求或刷新token
1008
966
OAuthTokenAttribute | token的应用特性,使用ITokenProviderFactory创建ITokenProvider,然后使用ITokenProvider获取token,最后将token应用到请求消息中
1009
967
OAuthTokenHandler | 属于http消息处理器,功能与OAuthTokenAttribute一样,除此之外,如果因为意外的原因导致服务器仍然返回未授权(401状态码),其还会丢弃旧token,申请新token来重试一次请求。
@@ -1102,18 +1060,17 @@ services
1102
1060
#### 多接口共享的TokenProvider
1103
1061
可以给http接口设置基础接口,然后为基础接口配置TokenProvider,例如下面的xxx和yyy接口,都属于IBaidu,只需要给IBaidu配置TokenProvider。
1104
1062
```
1063
+ [OAuthToken]
1105
1064
public interface IBaidu
1106
1065
{
1107
1066
}
1108
1067
1109
- [OAuthToken]
1110
1068
public interface IBaidu_XXX_Api : IBaidu
1111
1069
{
1112
1070
[HttpGet]
1113
1071
Task xxxAsync();
1114
1072
}
1115
1073
1116
- [OAuthToken]
1117
1074
public interface IBaidu_YYY_Api : IBaidu
1118
1075
{
1119
1076
[HttpGet]
0 commit comments