WebApiClient
WebApiClient copied to clipboard
支持多应用同时调用一个接口
1、变更 OAuthTokenAttribute 中的 OnRequestAsync 和 OnResponseAsync 方法为可继承。 2、增加 同套接口支持多应用轮换使用的模式,需要继承 IDynamicTokenProvider。 3、增加 DynamicTokenAttribute 特性,表示此接口支持多个应用Token,构造参数表示应用标识。 3、增加 特性值模板化扩展方法,使用参数替换模板值。如:[DynamicToken("AppID_{app_id}_Token")]表示{app_id}取值于参数app_id。 4、增加 IgnoreAttribute,表示仅用于给特性传值使用,不用于接口传值。
示例:
public class CuntomTokenProvider : DynamicTokenProvider
{
public Dictionary<string, AppOptions> apps { get; set; } = null!;
public CuntomTokenProvider(IServiceProvider service)
: base(service)
{
apps = new Dictionary<string, AppOptions>()
{
{"indentifier1",new AppOptions{ app_id="app1",app_secret="secret1" } },
{"indentifier2",new AppOptions{ app_id="app2",app_secret="secret2" } },
{"indentifier3",new AppOptions{ app_id="app3",app_secret="secret3" } },
{"indentifier4",new AppOptions{ app_id="app4",app_secret="secret4" } }
};
}
protected override async Task<TokenResult?> RequestTokenAsync(IServiceProvider serviceProvider)
{
var app = apps[Identifier];
var token = await GetTokenAsync(appid: app.app_id, appsecret: app.app_secret);
return token;
}
}
[HttpHost("https://open.xx.cn/open-apis/"), DynamicToken("AppID_{app_id}_Token")]
public interface ITestHttp : IHttpApi
{
[HttpGet("applications/{app_id}")]
Task<ResponseDto> GetApplication(string app_id);
}