@ -5,9 +5,12 @@ using System.Net.Http;
using System.Net.Http.Headers ;
using System.Reflection ;
using System.Threading.Tasks ;
using Microsoft.AspNetCore.Authentication ;
using Microsoft.AspNetCore.Http ;
using Microsoft.Extensions.Options ;
using Volo.Abp.DependencyInjection ;
using Volo.Abp.DynamicProxy ;
using Volo.Abp.Http.Client.Authentication ;
using Volo.Abp.Http.Modeling ;
using Volo.Abp.Http.ProxyScripting.Generators ;
using Volo.Abp.Json ;
@ -27,6 +30,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying
private readonly RemoteServiceOptions _remoteServiceOptions ;
private readonly AbpHttpClientOptions _clientOptions ;
private readonly IJsonSerializer _jsonSerializer ;
private readonly IAccessTokenProvider _accessTokenProvider ;
static DynamicHttpProxyInterceptor ( )
{
@ -40,11 +44,13 @@ namespace Volo.Abp.Http.Client.DynamicProxying
IOptions < AbpHttpClientOptions > clientOptions ,
IOptionsSnapshot < RemoteServiceOptions > remoteServiceOptions ,
IApiDescriptionFinder apiDescriptionFinder ,
IJsonSerializer jsonSerializer )
IJsonSerializer jsonSerializer ,
IAccessTokenProvider accessTokenProvider )
{
_httpClientFactory = httpClientFactory ;
_apiDescriptionFinder = apiDescriptionFinder ;
_jsonSerializer = jsonSerializer ;
_accessTokenProvider = accessTokenProvider ;
_clientOptions = clientOptions . Value ;
_remoteServiceOptions = remoteServiceOptions . Value ;
}
@ -105,7 +111,9 @@ namespace Volo.Abp.Http.Client.DynamicProxying
{
using ( var client = _httpClientFactory . Create ( ) )
{
var baseUrl = GetBaseUrl ( ) ;
var clientConfig = _clientOptions . HttpClientProxies . GetOrDefault ( typeof ( TService ) ) ? ? throw new AbpException ( $"Could not get DynamicHttpClientProxyConfig for {typeof(TService).FullName}." ) ;
var baseUrl = GetBaseUrl ( clientConfig ) ;
var action = await _apiDescriptionFinder . FindActionAsync ( baseUrl , typeof ( TService ) , invocation . Method ) ;
var apiVersion = GetApiVersionInfo ( action ) ;
var url = baseUrl + UrlBuilder . GenerateUrlWithParameters ( action , invocation . ArgumentsDictionary , apiVersion ) ;
@ -117,6 +125,13 @@ namespace Volo.Abp.Http.Client.DynamicProxying
AddHeaders ( invocation , action , requestMessage , apiVersion ) ;
var accessToken = await _accessTokenProvider . GetOrNullAsync ( ) ;
if ( accessToken ! = null )
{
//TODO: "Bearer" should not by static.
client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , accessToken ) ;
}
var response = await client . SendAsync ( requestMessage ) ;
if ( ! response . IsSuccessStatusCode )
@ -167,7 +182,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying
}
var headers = action . Parameters . Where ( p = > p . BindingSourceId = = ParameterBindingSources . Header ) . ToArray ( ) ;
foreach ( var headerParameter in headers )
{
var value = HttpActionParameterHelper . FindParameterValue ( invocation . ArgumentsDictionary , headerParameter ) ;
@ -178,12 +193,9 @@ namespace Volo.Abp.Http.Client.DynamicProxying
}
}
private string GetBaseUrl ( )
private string GetBaseUrl ( DynamicHttpClientProxyConfig config )
{
var clientConfig = _clientOptions . HttpClientProxies . GetOrDefault ( typeof ( TService ) )
? ? throw new AbpException ( $"Could not get DynamicHttpClientProxyConfig for {typeof(TService).FullName}." ) ;
return _remoteServiceOptions . RemoteServices . GetOrDefault ( clientConfig . RemoteServiceName ) ? . BaseUrl
return _remoteServiceOptions . RemoteServices . GetOrDefault ( config . RemoteServiceName ) ? . BaseUrl
? ? _remoteServiceOptions . RemoteServices . Default ? . BaseUrl
? ? throw new AbpException ( $"Could not find Base URL for {typeof(TService).FullName}." ) ;
}