Improved DynamicHttpProxyInterceptor

pull/112/head
Halil İbrahim Kalkan 8 years ago
parent 115b4c0c26
commit 0083892153

@ -12,6 +12,8 @@ using Volo.Abp.Threading;
namespace Volo.Abp.Http.Client.DynamicProxying
{
//TODO: Somehow capture cancellationtoken and pass to other methods...?
public class DynamicHttpProxyInterceptor<TService> : AbpInterceptor, ITransientDependency
{
private static MethodInfo GenericInterceptAsyncMethod { get; }
@ -39,47 +41,58 @@ namespace Volo.Abp.Http.Client.DynamicProxying
public override void Intercept(IAbpMethodInvocation invocation)
{
//TODO: Handle this differently because InterceptAsync assumes that given method is async!
AsyncHelper.RunSync(() => InterceptAsync(invocation));
}
public override Task InterceptAsync(IAbpMethodInvocation invocation)
{
if (invocation.Method.ReturnType.GenericTypeArguments.IsNullOrEmpty())
{
return MakeRequest(invocation);
}
invocation.ReturnValue = GenericInterceptAsyncMethod
.MakeGenericMethod(invocation.Method.ReturnType.GenericTypeArguments[0])
.Invoke(this, new object[] { invocation });
.Invoke(this, new object[] {invocation});
return Task.CompletedTask;
}
private async Task<T> InterceptAsync<T>(IAbpMethodInvocation invocation)
{
//TODO: Somehow capture cancellationtoken and pass to other methods...?
var content = await MakeRequest(invocation);
var result = JsonConvert.DeserializeObject(
content,
typeof(T),
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
var proxyConfig = GetProxyConfig();
var actionApiDescription = await _apiDescriptionFinder.FindActionAsync(proxyConfig, invocation.Method);
return (T)result;
}
private async Task<string> MakeRequest(IAbpMethodInvocation invocation)
{
using (var client = _httpClientFactory.Create())
{
var url = proxyConfig.BaseUrl + UrlBuilder.GenerateUrlWithParameters(actionApiDescription, invocation.ArgumentsDictionary);
var proxyConfig = GetProxyConfig();
var actionApiDescription = await _apiDescriptionFinder.FindActionAsync(proxyConfig, invocation.Method);
var url = proxyConfig.BaseUrl + UrlBuilder.GenerateUrlWithParameters(actionApiDescription, invocation.ArgumentsDictionary);
var requestMessage = new HttpRequestMessage(actionApiDescription.GetHttpMethod(), url);
var response = await client.SendAsync(requestMessage);
if (!response.IsSuccessStatusCode)
{
throw new AbpException("Remote service returns error!");
throw new AbpException($"Remote service returns error! HttpStatusCode: {response.StatusCode}, ReasonPhrase: {response.ReasonPhrase}");
}
var content = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject(
content,
typeof(T),
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
return (T)result;
return await response.Content.ReadAsStringAsync();
}
}

@ -13,6 +13,7 @@ using System.Net.Http;
using System.Text;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Json;
using Volo.Abp.TestApp.Application.Dto;
namespace Volo.Abp.AspNetCore.Mvc
{

@ -5,6 +5,7 @@ using Shouldly;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.TestApp.Application;
using Volo.Abp.TestApp.Application.Dto;
using Volo.Abp.TestApp.Domain;
using Xunit;
@ -47,10 +48,10 @@ namespace Volo.Abp.Http.DynamicProxying
new GetWithComplexTypeInput
{
Value1 = "value one",
Inner1 = new GetWithComplexTypeInner
Inner1 = new GetWithComplexTypeInput.GetWithComplexTypeInner
{
Value2 = "value two",
Inner2 = new GetWithComplexTypeInnerInner
Inner2 = new GetWithComplexTypeInput.GetWithComplexTypeInnerInner
{
Value3 = "value three"
}

@ -1,6 +1,6 @@
using Volo.Abp.TestApp.Domain;
namespace Volo.Abp.TestApp.Application
namespace Volo.Abp.TestApp.Application.Dto
{
public class GetPersonPhonesFilter
{

@ -0,0 +1,20 @@
namespace Volo.Abp.TestApp.Application.Dto
{
public class GetWithComplexTypeInput
{
public string Value1 { get; set; }
public GetWithComplexTypeInner Inner1 { get; set; }
public class GetWithComplexTypeInner
{
public string Value2 { get; set; }
public GetWithComplexTypeInnerInner Inner2 { get; set; }
}
public class GetWithComplexTypeInnerInner
{
public string Value3 { get; set; }
}
}
}

@ -1,6 +1,6 @@
using Volo.Abp.Application.Dtos;
namespace Volo.Abp.TestApp.Application
namespace Volo.Abp.TestApp.Application.Dto
{
public class PersonDto : EntityDto
{

@ -1,7 +1,7 @@
using Volo.Abp.Application.Dtos;
using Volo.Abp.TestApp.Domain;
namespace Volo.Abp.TestApp.Application
namespace Volo.Abp.TestApp.Application.Dto
{
public class PhoneDto : EntityDto<long>
{

@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.TestApp.Application.Dto;
namespace Volo.Abp.TestApp.Application
{
@ -15,22 +16,4 @@ namespace Volo.Abp.TestApp.Application
Task<GetWithComplexTypeInput> GetWithComplexType(GetWithComplexTypeInput input);
}
public class GetWithComplexTypeInput
{
public string Value1 { get; set; }
public GetWithComplexTypeInner Inner1 { get; set; }
}
public class GetWithComplexTypeInner
{
public string Value2 { get; set; }
public GetWithComplexTypeInnerInner Inner2 { get; set; }
}
public class GetWithComplexTypeInnerInner
{
public string Value3 { get; set; }
}
}

@ -6,6 +6,7 @@ using Volo.Abp.Application.Dtos;
using Volo.Abp.TestApp.Domain;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Application.Services;
using Volo.Abp.TestApp.Application.Dto;
namespace Volo.Abp.TestApp.Application
{

@ -3,6 +3,7 @@ using Volo.Abp.Modularity;
using Volo.Abp.TestApp.Domain;
using Volo.Abp.AutoMapper;
using Volo.Abp.TestApp.Application;
using Volo.Abp.TestApp.Application.Dto;
namespace Volo.Abp.TestApp
{

Loading…
Cancel
Save