Skip to content

Commit b67dcfa

Browse files
authored
Update README.md
1 parent e28e522 commit b67dcfa

File tree

1 file changed

+35
-78
lines changed

1 file changed

+35
-78
lines changed

README.md

Lines changed: 35 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
| 包名 | 描述 | Nuget |
88
---|---|--|
99
| 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) |
1112
| WebApiClientCore.Extensions.NewtonsoftJson | Json.Net扩展包 | [![NuGet](https://buildstats.info/nuget/WebApiClientCore.Extensions.NewtonsoftJson)](https://www.nuget.org/packages/WebApiClientCore.Extensions.NewtonsoftJson) |
1213
| WebApiClientCore.Extensions.JsonRpc | JsonRpc调用扩展包 | [![NuGet](https://buildstats.info/nuget/WebApiClientCore.Extensions.JsonRpc)](https://www.nuget.org/packages/WebApiClientCore.Extensions.JsonRpc) |
1314
| 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
4950
进群时请注明**WebApiClient**,在咨询问题之前,请先认真阅读以下剩余的文档,避免消耗作者不必要的重复解答时间。
5051

5152
### 编译时语法分析
52-
WebApiClientCore.Analyzers提供编码时语法分析与提示,声明的接口继承了空方法的IHttpApi接口,语法分析将生效,建议开发者开启这个功能
53+
WebApiClientCore.Analyzers提供接口声明的语法分析与提示,帮助开发者声明接口时避免使用不当的语法
5354

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。
6560

66-
### 接口配置与选项
67-
每个接口的选项对应为`HttpApiOptions`,选项名称为接口的完整名称,也可以通过HttpApi.GetName()方法获取得到。
61+
### 接口注册与选项
62+
调用services.AddHttpApi<IUserApi>()即可完成接口注册,
63+
每个接口的选项对应为`HttpApiOptions`,选项名称通过HttpApi.GetName()方法获取得到。
6864

6965
#### 在IHttpClientBuilder配置
7066
```
@@ -74,7 +70,7 @@ services
7470
.ConfigureHttpApi(o =>
7571
{
7672
// 符合国情的不标准时间格式,有些接口就是这么要求必须不标准
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"));
7874
});
7975
```
8076

@@ -100,7 +96,7 @@ services
10096
.ConfigureHttpApi<IUserApi>(o =>
10197
{
10298
// 符合国情的不标准时间格式,有些接口就是这么要求必须不标准
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"));
104100
});
105101
```
106102

@@ -337,7 +333,6 @@ await response.SaveAsAsync(fileStream);
337333
```
338334

339335
### 接口声明示例
340-
#### Petstore接口
341336
这个OpenApi文档在[petstore.swagger.io](https://petstore.swagger.io/),代码为使用WebApiClientCore.OpenApi.SourceGenerator工具将其OpenApi文档反向生成得到
342337

343338
```
@@ -426,59 +421,7 @@ public interface IPetApi : IHttpApi
426421
[HttpPost("pet/{petId}/uploadImage")]
427422
ITask<ApiResponse> UploadFileAsync([Required] long petId, [FormDataText] string additionalMetadata, FormDataFile file, CancellationToken cancellationToken = default);
428423
}
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+
```
482425

483426
### 请求条件性重试
484427
使用ITask<>异步声明,就有Retry的扩展,Retry的条件可以为捕获到某种Exception或响应模型符合某种条件。
@@ -616,11 +559,15 @@ public class RedisResponseCacheProvider : IResponseCacheProvider
616559
{
617560
throw new NotImplementedException();
618561
}
619-
}
562+
}
563+
```
620564

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+
}
624571
```
625572

626573
### 非模型请求
@@ -996,14 +943,25 @@ services
996943

997944
现在,调用IUserApi的任意接口,只要响应的状态码为401,就触发IUserLoginApi登录,然后将登录得到的cookie来重试请求接口,最终响应为正确的结果。你也可以重写CookieAuthorizationHandler的IsUnauthorizedAsync(HttpResponseMessage)方法来指示响应是未授权状态。
998945

946+
### SourceGenerator
947+
SourceGenerator是一种新的c#编译时代码补充生成技术,可以非常方便的为WebApiClient生成接口的代理实现类。使用WebApiClientCore.Extensions.SourceGenerator扩展包,可以替换默认的内置Emit生成代理类的方案,支持需要完全AOT编译的平台。
948+
949+
要启用SourceGenerator功能,需要如下配置启用
950+
```
951+
// 应用编译时生成接口的代理类型代码
952+
services
953+
.AddWebApiClient()
954+
.UseSourceGeneratorHttpApiActivator();
955+
```
956+
999957
### OAuths&Token
1000958
使用WebApiClientCore.Extensions.OAuths扩展,轻松支持token的获取、刷新与应用。
1001959

1002960
#### 对象与概念
1003961

1004962
对象 | 用途
1005963
---|---
1006-
ITokenProviderFactory | tokenProvider的创建工厂,提供通过HttpApi接口类型创建tokenProvider
964+
ITokenProviderFactory | tokenProvider的创建工厂,提供通过HttpApi接口类型获取或创建tokenProvider
1007965
ITokenProvider | token提供者,用于获取token,在token的过期后的头一次请求里触发重新请求或刷新token
1008966
OAuthTokenAttribute | token的应用特性,使用ITokenProviderFactory创建ITokenProvider,然后使用ITokenProvider获取token,最后将token应用到请求消息中
1009967
OAuthTokenHandler | 属于http消息处理器,功能与OAuthTokenAttribute一样,除此之外,如果因为意外的原因导致服务器仍然返回未授权(401状态码),其还会丢弃旧token,申请新token来重试一次请求。
@@ -1102,18 +1060,17 @@ services
11021060
#### 多接口共享的TokenProvider
11031061
可以给http接口设置基础接口,然后为基础接口配置TokenProvider,例如下面的xxx和yyy接口,都属于IBaidu,只需要给IBaidu配置TokenProvider。
11041062
```
1063+
[OAuthToken]
11051064
public interface IBaidu
11061065
{
11071066
}
11081067
1109-
[OAuthToken]
11101068
public interface IBaidu_XXX_Api : IBaidu
11111069
{
11121070
[HttpGet]
11131071
Task xxxAsync();
11141072
}
11151073
1116-
[OAuthToken]
11171074
public interface IBaidu_YYY_Api : IBaidu
11181075
{
11191076
[HttpGet]

0 commit comments

Comments
 (0)