Browse Source

Merge pull request #4138 from abpframework/maliming/httpclient-named

Use remote service http client in ApiDescriptionFinder.
pull/4164/head
Halil İbrahim Kalkan 6 years ago
committed by GitHub
parent
commit
53819f18f7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 51
      framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/ApiDescriptionFinder.cs
  2. 6
      framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs
  3. 7
      framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/IApiDescriptionFinder.cs

51
framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/ApiDescriptionFinder.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Net.Http;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -14,8 +15,6 @@ namespace Volo.Abp.Http.Client.DynamicProxying
{ {
public ICancellationTokenProvider CancellationTokenProvider { get; set; } public ICancellationTokenProvider CancellationTokenProvider { get; set; }
protected IDynamicProxyHttpClientFactory HttpClientFactory { get; }
protected IApiDescriptionCache Cache { get; } protected IApiDescriptionCache Cache { get; }
private static readonly JsonSerializerSettings SharedJsonSerializerSettings = new JsonSerializerSettings private static readonly JsonSerializerSettings SharedJsonSerializerSettings = new JsonSerializerSettings
@ -23,18 +22,15 @@ namespace Volo.Abp.Http.Client.DynamicProxying
ContractResolver = new CamelCasePropertyNamesContractResolver() ContractResolver = new CamelCasePropertyNamesContractResolver()
}; };
public ApiDescriptionFinder( public ApiDescriptionFinder(IApiDescriptionCache cache)
IApiDescriptionCache cache,
IDynamicProxyHttpClientFactory httpClientFactory)
{ {
Cache = cache; Cache = cache;
HttpClientFactory = httpClientFactory;
CancellationTokenProvider = NullCancellationTokenProvider.Instance; CancellationTokenProvider = NullCancellationTokenProvider.Instance;
} }
public async Task<ActionApiDescriptionModel> FindActionAsync(string baseUrl, Type serviceType, MethodInfo method) public async Task<ActionApiDescriptionModel> FindActionAsync(HttpClient client, string baseUrl, Type serviceType, MethodInfo method)
{ {
var apiDescription = await GetApiDescriptionAsync(baseUrl); var apiDescription = await GetApiDescriptionAsync(client, baseUrl);
//TODO: Cache finding? //TODO: Cache finding?
@ -56,8 +52,8 @@ namespace Volo.Abp.Http.Client.DynamicProxying
var found = true; var found = true;
for (int i = 0; i < methodParameters.Length; i++) for (int i = 0; i < methodParameters.Length; i++)
{ {
if (!TypeMatches(action.ParametersOnMethod[i], methodParameters[i])) if (!TypeMatches(action.ParametersOnMethod[i], methodParameters[i]))
{ {
found = false; found = false;
break; break;
@ -76,33 +72,30 @@ namespace Volo.Abp.Http.Client.DynamicProxying
throw new AbpException($"Could not found remote action for method: {method} on the URL: {baseUrl}"); throw new AbpException($"Could not found remote action for method: {method} on the URL: {baseUrl}");
} }
public virtual async Task<ApplicationApiDescriptionModel> GetApiDescriptionAsync(string baseUrl) public virtual async Task<ApplicationApiDescriptionModel> GetApiDescriptionAsync(HttpClient client, string baseUrl)
{ {
return await Cache.GetAsync(baseUrl, () => GetApiDescriptionFromServerAsync(baseUrl)); return await Cache.GetAsync(baseUrl, () => GetApiDescriptionFromServerAsync(client, baseUrl));
} }
protected virtual async Task<ApplicationApiDescriptionModel> GetApiDescriptionFromServerAsync(string baseUrl) protected virtual async Task<ApplicationApiDescriptionModel> GetApiDescriptionFromServerAsync(HttpClient client, string baseUrl)
{ {
using (var client = HttpClientFactory.Create()) var response = await client.GetAsync(
{ baseUrl.EnsureEndsWith('/') + "api/abp/api-definition",
var response = await client.GetAsync( CancellationTokenProvider.Token
baseUrl.EnsureEndsWith('/') + "api/abp/api-definition", );
CancellationTokenProvider.Token
);
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{ {
throw new AbpException("Remote service returns error! StatusCode = " + response.StatusCode); throw new AbpException("Remote service returns error! StatusCode = " + response.StatusCode);
} }
var content = await response.Content.ReadAsStringAsync(); var content = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject( var result = JsonConvert.DeserializeObject(
content, content,
typeof(ApplicationApiDescriptionModel), SharedJsonSerializerSettings); typeof(ApplicationApiDescriptionModel), SharedJsonSerializerSettings);
return (ApplicationApiDescriptionModel)result; return (ApplicationApiDescriptionModel)result;
}
} }
protected virtual bool TypeMatches(MethodParameterApiDescriptionModel actionParameter, ParameterInfo methodParameter) protected virtual bool TypeMatches(MethodParameterApiDescriptionModel actionParameter, ParameterInfo methodParameter)

6
framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs

@ -106,7 +106,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying
private async Task<T> MakeRequestAndGetResultAsync<T>(IAbpMethodInvocation invocation) private async Task<T> MakeRequestAndGetResultAsync<T>(IAbpMethodInvocation invocation)
{ {
var responseAsString = await MakeRequestAsync(invocation); var responseAsString = await MakeRequestAsync(invocation);
if (typeof(T) == typeof(string)) if (typeof(T) == typeof(string))
{ {
return (T)Convert.ChangeType(responseAsString, typeof(T)); return (T)Convert.ChangeType(responseAsString, typeof(T));
@ -122,7 +122,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying
var client = HttpClientFactory.Create(clientConfig.RemoteServiceName); var client = HttpClientFactory.Create(clientConfig.RemoteServiceName);
var action = await ApiDescriptionFinder.FindActionAsync(remoteServiceConfig.BaseUrl, typeof(TService), invocation.Method); var action = await ApiDescriptionFinder.FindActionAsync(client, remoteServiceConfig.BaseUrl, typeof(TService), invocation.Method);
var apiVersion = GetApiVersionInfo(action); var apiVersion = GetApiVersionInfo(action);
var url = remoteServiceConfig.BaseUrl.EnsureEndsWith('/') + UrlBuilder.GenerateUrlWithParameters(action, invocation.ArgumentsDictionary, apiVersion); var url = remoteServiceConfig.BaseUrl.EnsureEndsWith('/') + UrlBuilder.GenerateUrlWithParameters(action, invocation.ArgumentsDictionary, apiVersion);
@ -252,4 +252,4 @@ namespace Volo.Abp.Http.Client.DynamicProxying
return CancellationTokenProvider.Token; return CancellationTokenProvider.Token;
} }
} }
} }

7
framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/IApiDescriptionFinder.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Net.Http;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Http.Modeling; using Volo.Abp.Http.Modeling;
@ -7,8 +8,8 @@ namespace Volo.Abp.Http.Client.DynamicProxying
{ {
public interface IApiDescriptionFinder public interface IApiDescriptionFinder
{ {
Task<ActionApiDescriptionModel> FindActionAsync(string baseUrl, Type serviceType, MethodInfo invocationMethod); Task<ActionApiDescriptionModel> FindActionAsync(HttpClient client, string baseUrl, Type serviceType, MethodInfo invocationMethod);
Task<ApplicationApiDescriptionModel> GetApiDescriptionAsync(string baseUrl); Task<ApplicationApiDescriptionModel> GetApiDescriptionAsync(HttpClient client, string baseUrl);
} }
} }

Loading…
Cancel
Save