Skip to content

Commit

Permalink
移除创建不明确的ITokenProvider`1
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Sep 14, 2020
1 parent 5c9cc21 commit a40788c
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
Expand Down
7 changes: 0 additions & 7 deletions WebApiClientCore.Extensions.OAuths/ITokenProviderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ namespace WebApiClientCore.Extensions.OAuths
/// </summary>
public interface ITokenProviderFactory
{
/// <summary>
/// 返回是否可以创建token提供者
/// </summary>
/// <param name="httpApiType">接口类型</param>
/// <returns></returns>
public bool CanCreate(Type httpApiType);

/// <summary>
/// 创建token提供者
/// </summary>
Expand Down
10 changes: 0 additions & 10 deletions WebApiClientCore.Extensions.OAuths/ITokenProvider`.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static IHttpClientBuilder AddOAuthTokenHandler<TOAuthTokenHandler>(this I
var httpApiType = builder.GetHttpApiType();
if (httpApiType == null)
{
throw new InvalidOperationException($"无效的{nameof(IHttpClientBuilder)}");
throw new InvalidOperationException($"无效的{nameof(IHttpClientBuilder)},找不到其关联的http接口类型");
}

builder.Services.TryAddTransient(serviceProvider =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ private static ITokenProviderBuilder AddTokenProviderCore<THttpApi, TTokenProvid
where TTokenProvider : class, ITokenProvider
{
var name = $"{typeof(TTokenProvider).Name}+{typeof(THttpApi).Name}";
var hashCode = HashCode.Combine(typeof(THttpApi), typeof(TTokenProvider));
name = hashCode < 0 ? $"{name}{hashCode}" : $"{name}+{hashCode}";

services
.AddOptions<TokenProviderFactoryOptions>()
.Configure(o => o.Register<THttpApi, TTokenProvider>(name));

services.TryAddSingleton<ITokenProviderFactory, TokenProviderFactory>();
services.TryAddSingleton(typeof(ITokenProvider<>), typeof(TypeTokenProvider<>));
services.AddHttpApi<IOAuthTokenClientApi>(o => o.KeyValueSerializeOptions.IgnoreNullValues = true);

return new TokenProviderBuilder(name, services);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,14 @@ public override int GetHashCode()
{
return HashCode.Combine(this.Name, this.Type);
}

/// <summary>
/// 转换为string
/// </summary>
/// <returns></returns>
public override string ToString()
{
return this.Name;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,23 @@ public TokenProviderFactory(IServiceProvider serviceProvider, IOptions<TokenProv
this.options = options.Value;
}

/// <summary>
/// 返回是否可以创建token提供者
/// </summary>
/// <param name="httpApiType">接口类型</param>
/// <returns></returns>
public bool CanCreate(Type httpApiType)
{
if (httpApiType == null)
{
return false;
}
return this.options.ContainsKey(httpApiType);
}


/// <summary>
/// 创建token提供者
/// </summary>
/// <param name="httpApiType">接口类型</param>
/// <param name="typeMatchMode">类型匹配模式</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="InvalidOperationException"></exception>
public ITokenProvider Create(Type httpApiType, TypeMatchMode typeMatchMode)
{
if (httpApiType == null)
{
throw new ArgumentNullException(nameof(httpApiType));
}

if (typeMatchMode == TypeMatchMode.TypeOnly)
{
return this.Create(httpApiType);
Expand All @@ -64,6 +58,17 @@ public ITokenProvider Create(Type httpApiType, TypeMatchMode typeMatchMode)
}


/// <summary>
/// 返回是否可以创建token提供者
/// </summary>
/// <param name="httpApiType">接口类型</param>
/// <returns></returns>
private bool CanCreate(Type httpApiType)
{
return this.options.ContainsKey(httpApiType);
}


/// <summary>
/// 创建token提供者
/// </summary>
Expand Down

This file was deleted.

0 comments on commit a40788c

Please sign in to comment.