/// <summary> /// 添加HttpApiClient的别名HttpClient /// </summary> /// <typeparam name="TInterface"></typeparam> /// <param name="services"></param> /// <param name="config"></param> /// <returns></returns> public static IServiceCollection AddWebApiClientUseHttpClientFactory <TInterface>(this IServiceCollection services, Action <HttpApiConfig, IServiceProvider> config) where TInterface : class, IHttpApi { if (config == null) { throw new ArgumentNullException(nameof(config)); } var name = typeof(TInterface).ToString(); services .AddHttpClient(name) .AddTypedClient <TInterface>((httpClient, provider) => { var httpApiConfig = new HttpApiConfig(httpClient) { ServiceProvider = provider, ResponseCacheProvider = new EasyCachingResponseCacheProvider(provider) }; config.Invoke(httpApiConfig, provider); var interceptor = new EasyCachingInterceptor(httpApiConfig); return(HttpApi.Create(typeof(TInterface), interceptor) as TInterface); }); return(services); }
public static IHttpClientBuilder AddHttpApiClient <THttpApi>( this IServiceCollection services, Action <HttpApiConsulOptions, IServiceProvider> configureOptions) where THttpApi : class { string name = typeof(THttpApi).FullName; services .AddHttpApi() .AddOptions <HttpApiConsulOptions>(name) .Configure(configureOptions); return(services .AddHttpClient(name) .AddTypedClient((client, provider) => { HttpApiConsulOptions options = provider .GetRequiredService <IOptionsMonitor <HttpApiConsulOptions> >() .Get(name); provider.ServiceDiscoveryAsync(options).Wait(); return HttpApi.Create <THttpApi>(client, provider, options); })); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //注册Swagger生成器,定义一个和多个Swagger 文档 services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Version = "v1", Title = "API", Description = "权限管理", TermsOfService = "None" }); // 为 Swagger JSON and UI设置xml文档注释路径 var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//获取应用程序所在目录(绝对,不受工作目录影响,建议采用此方法获取路径) var xmlPath = Path.Combine(basePath, "WebApiSwagger.xml"); c.IncludeXmlComments(xmlPath); }); services.AddHttpClient <IWebApiClientBase>().AddTypedClient <IWebApiClientBase>((client, p) => { var httpApiConfig = new HttpApiConfig(client) { HttpHost = new Uri("https://localhost:44376/"), LoggerFactory = p.GetRequiredService <ILoggerFactory>() }; return(HttpApi.Create <IWebApiClientBase>(httpApiConfig)); }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); }
private WMSApiAccessor() { HttpApiConfig config = new HttpApiConfig(); config.HttpHost = new Uri("http://localhost:23456/hook/wcs/"); _apiProxy = HttpApi.Create <IWMSApiProxy>(config); }
public async Task <ActionResult <ResultMsg> > GetAsync(string jscode = "") { var api = HttpApi.Create <IMyWebApi>(); var response = await api.Jscode2Session("", "", jscode); return(response); }
public static IHuyiApis Create(HuyiConfig config) { var client = new HttpClient { Timeout = TimeSpan.FromMilliseconds(config.Timeout) }; return(HttpApi.Create <IHuyiApis>(new HttpApiConfig(client))); }
/// <summary> /// 创建OAuth客户端 /// </summary> /// <returns></returns> protected IOAuthClient CreateOAuthClient() { var options = new HttpApiOptions(); options.KeyValueSerializeOptions.IgnoreNullValues = true; var client = this.services.GetRequiredService <IHttpClientFactory>().CreateClient(); return(HttpApi.Create <IOAuthClient>(client, services, options)); }
private WCSApiAccessor() { if (!string.IsNullOrEmpty(Host)) { HttpApiConfig config = new HttpApiConfig(); config.HttpHost = new Uri(Host); _apiProxy = HttpApi.Create <IWCSApiProxy>(config); } }
public void CreateTest() { var api = HttpApi.Create <IMyApi>(); Assert.True(api != null); Assert.Throws <ArgumentException>(() => HttpApi.Create <MyApi>()); Assert.Throws <NotSupportedException>(() => HttpApi.Create <IMyApi2>()); Assert.Throws <NotSupportedException>(() => HttpApi.Create <IMyApi3>()); Assert.Throws <NotSupportedException>(() => HttpApi.Create <IMyApi4>()); }
private static IBooruClient CreateClient(string host) { var config = new HttpApiConfig { HttpHost = new Uri(host), }; var bApi = HttpApi.Create <IBooruClient>(config); return(bApi); }
/// <summary> /// 初始化一个<see cref="KdniaoClient"/>类型的实例 /// </summary> /// <param name="config">配置</param> /// <param name="exceptionHandler">异常处理操作</param> public KdniaoClient(KdniaoConfig config, Action <Exception> exceptionHandler = null) { _config = config ?? throw new ArgumentNullException(nameof(config)); _account = config.Account ?? throw new ArgumentNullException(nameof(config.Account)); _proxy = HttpApi.Create <IKdniaoApi>(); var globalHandle = ExceptionHandleResolver.ResolveHandler(); globalHandle += exceptionHandler; _exceptionHandler = globalHandle; }
public RongCloudClient(RongCloudConfig config, Action <Exception> exceptionHandler = null) { _config = config ?? throw new ArgumentNullException(nameof(config)); _rongAccount = config.Account ?? throw new ArgumentNullException(nameof(config.Account)); _proxy = HttpApi.Create <IRongCloudSmsApis>(); var globalHandle = ExceptionHandleResolver.ResolveHandler(); globalHandle += exceptionHandler; _exceptionHandler = globalHandle; }
public KsyunSmsClient(KsyunConfig config, Action <Exception> exceptionHandler = null) { _config = config ?? throw new ArgumentNullException(nameof(config)); _account = _config.Account ?? throw new ArgumentNullException(nameof(_config.Account)); _apiServerUrl = $"{GetHttpPrefix(config)}://{KsyunSmsConstants.Host}"; _proxy = HttpApi.Create <IKsyunSmsApis>(_apiServerUrl); var globalHandle = ExceptionHandleResolver.ResolveHandler(); globalHandle += exceptionHandler; _exceptionHandler = globalHandle; }
public static IBaiduYunSmsApis Create(string apiUri, int timeout) { var client = new HttpClient { Timeout = TimeSpan.FromMilliseconds(timeout), BaseAddress = new Uri(apiUri) }; var httpConfig = new HttpApiConfig(client); return(HttpApi.Create <IBaiduYunSmsApis>(httpConfig)); }
public static IChuanglanApi Create(ChuanglanConfig config) { var client = new HttpClient { Timeout = TimeSpan.FromMilliseconds(config.Timeout), BaseAddress = new Uri(config.Account.ApiUrl) }; var httpConfig = new HttpApiConfig(client); return(HttpApi.Create <IChuanglanApi>(httpConfig)); }
/// <summary> /// 添加IdentityServer验证 /// </summary> /// <returns></returns> public DependencyInjectionService AddWebApiClient <T>() where T : class, IHttpApi { _services.AddHttpClient <T>().AddTypedClient <T>((client, p) => { var httpApiConfig = new HttpApiConfig(client) { HttpHost = new Uri("https://localhost:44376/"), LoggerFactory = p.GetRequiredService <ILoggerFactory>() }; return(HttpApi.Create <T>(httpApiConfig)); }); return(_dependencyInjectionConfiguration); }
public YuntongxunSmsClient(YuntongxunSmsConfig config, Action <Exception> exceptionHandler = null) { _config = config ?? throw new ArgumentNullException(nameof(config)); _yuntongxunAccount = config.Account ?? throw new ArgumentNullException(nameof(config.Account)); _proxy = _config.Production ? HttpApi.Create <ICloopenApis>("https://app.cloopen.com:8883") : HttpApi.Create <ICloopenApis>("https://sandboxapp.cloopen.com:8883"); var globalHandle = ExceptionHandleResolver.ResolveHandler(); globalHandle += exceptionHandler; _exceptionHandler = globalHandle; }
public static IHuaweiCloudSmsApis Create(HuaweiCloudSmsConfig config) { var handler = new HttpClientHandler(); handler.ServerCertificateCustomValidationCallback = delegate { return(true); }; var client = new HttpClient(handler); client.Timeout = TimeSpan.FromMilliseconds(config.TimeOut); client.BaseAddress = new Uri(ApiAddressHelper.GetGateway(config.RegionName)); var httpConfig = new HttpApiConfig(client); return(HttpApi.Create <IHuaweiCloudSmsApis>(httpConfig)); }
public WeimiSmsClient(WeimiSmsConfig config, Action <Exception> exceptionHandler = null) { _config = config ?? throw new ArgumentNullException(nameof(config)); _weimiSmsAccount = config.Account ?? throw new ArgumentNullException(nameof(config.Account)); _proxy = config.Security ? HttpApi.Create <IWeimiSmsApi>("https://api.weimi.cc") : HttpApi.Create <IWeimiSmsApi>("http://api.weimi.cc"); var globalHandle = ExceptionHandleResolver.ResolveHandler(); globalHandle += exceptionHandler; _exceptionHandler = globalHandle; }
public static IAliyunDysmsApi Create(AliyunDysmsConfig config) { var client = new HttpClient { Timeout = TimeSpan.FromMilliseconds(config.TimeOut), BaseAddress = config.Security ? new Uri("https://dysmsapi.aliyuncs.com") : new Uri("http://dysmsapi.aliyuncs.com") }; var httpConfig = new HttpApiConfig(client); return(HttpApi.Create <IAliyunDysmsApi>(httpConfig)); }
/// <summary> /// 添加HttpApi代理类到服务 /// </summary> /// <typeparam name="THttpApi"></typeparam> /// <param name="services"></param> /// <param name="configureOptions"></param> /// <returns></returns> public static IHttpClientBuilder AddHttpApi <THttpApi>(this IServiceCollection services, Action <HttpApiOptions <THttpApi>, IServiceProvider> configureOptions) where THttpApi : class { services .AddHttpApi() .AddOptions <HttpApiOptions <THttpApi> >() .Configure(configureOptions); return(services .AddHttpClient(typeof(THttpApi).FullName) .AddTypedClient((client, serviceProvider) => { var options = serviceProvider.GetRequiredService <IOptions <HttpApiOptions <THttpApi> > >(); return HttpApi.Create <THttpApi>(client, serviceProvider, options.Value); })); }
public static IHttpClientBuilder AddNacosTypedClient <TInterface>(this IServiceCollection services, Action <NacosHttpApiConfig, IServiceProvider> config) where TInterface : class, IHttpApi { return(services.AddHttpClient(nameof(TInterface)) .AddTypedClient <TInterface>((client, serviceProvider) => { var nacosHttpApiConfig = new NacosHttpApiConfig { ServiceProvider = serviceProvider }; config.Invoke(nacosHttpApiConfig, serviceProvider); return HttpApi.Create <TInterface>(nacosHttpApiConfig); })); }
/// <summary> /// 添加HttpApi代理类到服务 /// </summary> /// <typeparam name="THttpApi"></typeparam> /// <param name="services"></param> /// <returns></returns> public static IHttpClientBuilder AddHttpApi <THttpApi>(this IServiceCollection services) where THttpApi : class { services.AddOptions(); services.AddMemoryCache(); services.TryAddSingleton <IXmlSerializer, XmlSerializer>(); services.TryAddSingleton <IJsonSerializer, JsonSerializer>(); services.TryAddSingleton <IKeyValueSerializer, KeyValueSerializer>(); services.TryAddSingleton <IResponseCacheProvider, ResponseCacheProvider>(); return(services .AddHttpClient(typeof(THttpApi).FullName) .AddTypedClient((httpClient, serviceProvider) => { var name = typeof(THttpApi).FullName; var httpApiOptions = serviceProvider.GetRequiredService <IOptionsMonitor <HttpApiOptions> >().Get(name); return HttpApi.Create <THttpApi>(httpClient, serviceProvider, httpApiOptions); })); }
public WMSBaseApiAccessor(Wms_warehouse warehouse, ISqlSugarClient client, SysUserDto userDto) { this.Warehouse = warehouse; HttpApiConfig config = new HttpApiConfig(); config.HttpHost = new Uri(warehouse.IFAddress); if (!string.IsNullOrWhiteSpace(WMSProxy)) { config.HttpHandler.UseProxy = true; config.HttpHandler.Proxy = new HttpProxy(WMSProxy); WebRequest.DefaultWebProxy = new WebProxy(WMSProxy) { BypassProxyOnLocal = false }; } _apiProxy = HttpApi.Create <IWMSApiProxy>(config); _selfAccessor = new SelfWMSBaseApiAccessor(warehouse, client, userDto); }
/// <summary> /// 添加HttpApiClient的别名HttpClient /// </summary> /// <typeparam name="TInterface">接口类型</typeparam> /// <param name="services"></param> /// <param name="configOptions">配置选项</param> /// <exception cref="ArgumentNullException"></exception> /// <returns></returns> public static IHttpClientBuilder AddHttpApiTypedClient <TInterface>(this IServiceCollection services, Action <HttpApiConfig, IServiceProvider> configOptions) where TInterface : class, IHttpApi { if (configOptions == null) { throw new ArgumentNullException(nameof(configOptions)); } return(services .AddHttpClient(typeof(TInterface).ToString()) .AddTypedClient((httpClient, provider) => { var httpApiConfig = new HttpApiConfig(httpClient) { ServiceProvider = provider }; configOptions.Invoke(httpApiConfig, provider); return HttpApi.Create <TInterface>(httpApiConfig); })); }
/// <summary> /// 添加HttpApi代理类到服务 /// </summary> /// <typeparam name="THttpApi"></typeparam> /// <param name="services"></param> /// <returns></returns> public static IHttpClientBuilder AddHttpApi <THttpApi>(this IServiceCollection services) where THttpApi : class { services.AddOptions(); services.AddMemoryCache(); services.TryAddSingleton <IXmlSerializer, XmlSerializer>(); services.TryAddSingleton <IJsonSerializer, JsonSerializer>(); services.TryAddSingleton <IKeyValueSerializer, KeyValueSerializer>(); services.TryAddSingleton <IResponseCacheProvider, ResponseCacheProvider>(); var name = HttpApi.GetName(typeof(THttpApi)); services.NamedHttpApiType(name, typeof(THttpApi)); services.TryAddTransient(serviceProvider => { var httpClient = serviceProvider.GetRequiredService <IHttpClientFactory>().CreateClient(name); var httpApiOptions = serviceProvider.GetRequiredService <IOptionsMonitor <HttpApiOptions> >().Get(name); var httpClientContext = new HttpClientContext(httpClient, serviceProvider, httpApiOptions, name); return(HttpApi.Create <THttpApi>(httpClientContext)); }); return(services.AddHttpClient(name)); }
public async Task <HeaderResult <List <UserDto> > > WebApiClientBaseAsync() { SearchUserDto search = new SearchUserDto() { PageIndex = 1, Pagesize = 10, SearchPwd = "", SearchName = "" }; try { var myWebApi = HttpApi.Create <IWebApiClientBase>(); var userList = await myWebApi.GetUserListAsync(search); return(userList); } catch (Exception e) { Console.WriteLine(e); throw; } }
/// <summary> /// POSのカート商品単品のAPIを取得します。 /// </summary> /// <param name="cartId">カート識別文字列</param> /// <param name="itemCode">商品コード</param> /// <returns>API</returns> public HttpApi GetCartItemApi(string cartId, string itemCode) => HttpApi.Create(this._client, this.GetCartItemUri(cartId, itemCode), Settings.Instance.PosApiKeyHeader, Settings.Instance.PosApiKey);
/// <summary> /// POSのカート精算APIを取得します。 /// </summary> /// <param name="cartId">カート識別文字列</param> /// <returns>API</returns> public HttpApi GetCartBillApi(string cartId) => HttpApi.Create(this._client, this.GetCartBillUri(cartId), Settings.Instance.PosApiKeyHeader, Settings.Instance.PosApiKey);