|
|
|
@ -2,23 +2,34 @@ |
|
|
|
using System.Linq; |
|
|
|
using System.Reflection; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Newtonsoft.Json; |
|
|
|
using Newtonsoft.Json.Serialization; |
|
|
|
using Volo.Abp.DependencyInjection; |
|
|
|
using Volo.Abp.Http.Modeling; |
|
|
|
using Volo.Abp.Threading; |
|
|
|
|
|
|
|
namespace Volo.Abp.Http.Client.DynamicProxying |
|
|
|
{ |
|
|
|
public class ApiDescriptionFinder : IApiDescriptionFinder, ISingletonDependency |
|
|
|
{ |
|
|
|
private readonly IApiDescriptionCache _descriptionCache; |
|
|
|
public ICancellationTokenProvider CancellationTokenProvider { get; set; } |
|
|
|
|
|
|
|
public ApiDescriptionFinder(IApiDescriptionCache descriptionCache) |
|
|
|
protected IDynamicProxyHttpClientFactory HttpClientFactory { get; } |
|
|
|
|
|
|
|
protected IApiDescriptionCache Cache { get; } |
|
|
|
|
|
|
|
public ApiDescriptionFinder( |
|
|
|
IApiDescriptionCache cache, |
|
|
|
IDynamicProxyHttpClientFactory httpClientFactory) |
|
|
|
{ |
|
|
|
_descriptionCache = descriptionCache; |
|
|
|
Cache = cache; |
|
|
|
HttpClientFactory = httpClientFactory; |
|
|
|
CancellationTokenProvider = NullCancellationTokenProvider.Instance; |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<ActionApiDescriptionModel> FindActionAsync(string baseUrl, Type serviceType, MethodInfo method) |
|
|
|
{ |
|
|
|
var apiDescription = await _descriptionCache.GetAsync(baseUrl); |
|
|
|
var apiDescription = await GetApiDescriptionAsync(baseUrl); |
|
|
|
|
|
|
|
//TODO: Cache finding?
|
|
|
|
|
|
|
|
@ -57,7 +68,40 @@ namespace Volo.Abp.Http.Client.DynamicProxying |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
throw new AbpException("Could not found remote action for method: " + method); |
|
|
|
throw new AbpException($"Could not found remote action for method: {method} on the URL: {baseUrl}"); |
|
|
|
} |
|
|
|
|
|
|
|
public virtual async Task<ApplicationApiDescriptionModel> GetApiDescriptionAsync(string baseUrl) |
|
|
|
{ |
|
|
|
return await Cache.GetAsync(baseUrl, () => GetApiDescriptionFromServerAsync(baseUrl)); |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual async Task<ApplicationApiDescriptionModel> GetApiDescriptionFromServerAsync(string baseUrl) |
|
|
|
{ |
|
|
|
using (var client = HttpClientFactory.Create()) |
|
|
|
{ |
|
|
|
var response = await client.GetAsync( |
|
|
|
baseUrl.EnsureEndsWith('/') + "api/abp/api-definition", |
|
|
|
CancellationTokenProvider.Token |
|
|
|
); |
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode) |
|
|
|
{ |
|
|
|
throw new AbpException("Remote service returns error!"); |
|
|
|
} |
|
|
|
|
|
|
|
var content = await response.Content.ReadAsStringAsync(); |
|
|
|
|
|
|
|
var result = JsonConvert.DeserializeObject( |
|
|
|
content, |
|
|
|
typeof(ApplicationApiDescriptionModel), |
|
|
|
new JsonSerializerSettings |
|
|
|
{ |
|
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver() |
|
|
|
}); |
|
|
|
|
|
|
|
return (ApplicationApiDescriptionModel)result; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|