Browse Source

lookup api request service implementation

pull/6647/head
Ilkay Ilknur 5 years ago
parent
commit
6598c3703f
  1. 82
      framework/src/Volo.Abp.AspNetCore.Components.Server/Volo/Abp/AspNetCore/Components/Server/Extensibility/BlazorServerLookupApiRequestService.cs
  2. 10
      framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/Extensibility/ILookupApiRequestService.cs
  3. 69
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/Extensibility/WebAssemblyLookupApiRequestService.cs
  4. 84
      framework/src/Volo.Abp.BlazoriseUI/Components/ObjectExtending/LookupExtensionProperty.razor.cs

82
framework/src/Volo.Abp.AspNetCore.Components.Server/Volo/Abp/AspNetCore/Components/Server/Extensibility/BlazorServerLookupApiRequestService.cs

@ -0,0 +1,82 @@
using System;
using System.Globalization;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Options;
using Volo.Abp.AspNetCore.Components.Web.Extensibility;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Http.Client;
using Volo.Abp.Http.Client.Authentication;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.AspNetCore.Components.Server.Extensibility
{
public class BlazorServerLookupApiRequestService : ILookupApiRequestService, ITransientDependency
{
public IHttpClientFactory HttpClientFactory { get; }
public IRemoteServiceHttpClientAuthenticator HttpClientAuthenticator { get; }
public AbpRemoteServiceOptions RemoteServiceOptions { get; }
public ICurrentTenant CurrentTenant { get; }
public NavigationManager NavigationManager { get; }
public BlazorServerLookupApiRequestService(IHttpClientFactory httpClientFactory,
IRemoteServiceHttpClientAuthenticator httpClientAuthenticator,
ICurrentTenant currentTenant,
IOptions<AbpRemoteServiceOptions> remoteServiceOptions,
NavigationManager navigationManager)
{
HttpClientFactory = httpClientFactory;
HttpClientAuthenticator = httpClientAuthenticator;
RemoteServiceOptions = remoteServiceOptions.Value;
CurrentTenant = currentTenant;
NavigationManager = navigationManager;
}
public async Task<string> SendAsync(string url)
{
var client = HttpClientFactory.CreateClient();
var requestMessage = new HttpRequestMessage(HttpMethod.Get, url);
AddHeaders(requestMessage);
var uri = new Uri(url, UriKind.RelativeOrAbsolute);
if (!uri.IsAbsoluteUri)
{
var baseUrl = string.Empty;
try
{
var remoteServiceConfig = RemoteServiceOptions.RemoteServices.GetConfigurationOrDefault("Default");
baseUrl = remoteServiceConfig.BaseUrl;
}
catch (AbpException)
{
baseUrl = NavigationManager.BaseUri;
}
client.BaseAddress = new Uri(baseUrl);
await HttpClientAuthenticator.Authenticate(new RemoteServiceHttpClientAuthenticateContext(client,
requestMessage, new RemoteServiceConfiguration(baseUrl), string.Empty));
}
var response = await client.SendAsync(requestMessage);
return await response.Content.ReadAsStringAsync();
}
protected virtual void AddHeaders(HttpRequestMessage requestMessage)
{
if (CurrentTenant.Id.HasValue)
{
requestMessage.Headers.Add(TenantResolverConsts.DefaultTenantKey, CurrentTenant.Id.Value.ToString());
}
var currentCulture = CultureInfo.CurrentUICulture.Name ?? CultureInfo.CurrentCulture.Name;
if (!currentCulture.IsNullOrEmpty())
{
requestMessage.Headers.AcceptLanguage.Add(new(currentCulture));
}
}
}
}

10
framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/Extensibility/ILookupApiRequestService.cs

@ -0,0 +1,10 @@
using System.Threading.Tasks;
using JetBrains.Annotations;
namespace Volo.Abp.AspNetCore.Components.Web.Extensibility
{
public interface ILookupApiRequestService
{
Task<string> SendAsync([NotNull]string url);
}
}

69
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/Extensibility/WebAssemblyLookupApiRequestService.cs

@ -0,0 +1,69 @@
using System;
using System.Globalization;
using System.Net.Http;
using System.Threading.Tasks;
using Castle.Components.DictionaryAdapter;
using Fody;
using Volo.Abp.AspNetCore.Components.Web.Extensibility;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Http.Client.Authentication;
using Volo.Abp.Http.Client;
using Microsoft.Extensions.Options;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.AspNetCore.Components.WebAssembly.Extensibility
{
public class WebAssemblyLookupApiRequestService : ILookupApiRequestService, ITransientDependency
{
public IHttpClientFactory HttpClientFactory { get; }
public IRemoteServiceHttpClientAuthenticator HttpClientAuthenticator { get; }
public AbpRemoteServiceOptions RemoteServiceOptions { get; }
public ICurrentTenant CurrentTenant { get; }
public WebAssemblyLookupApiRequestService(IHttpClientFactory httpClientFactory,
IRemoteServiceHttpClientAuthenticator httpClientAuthenticator,
ICurrentTenant currentTenant,
IOptions<AbpRemoteServiceOptions> remoteServiceOptions)
{
HttpClientFactory = httpClientFactory;
HttpClientAuthenticator = httpClientAuthenticator;
RemoteServiceOptions = remoteServiceOptions.Value;
CurrentTenant = currentTenant;
}
public async Task<string> SendAsync(string url)
{
var client = HttpClientFactory.CreateClient();
var requestMessage = new HttpRequestMessage(HttpMethod.Get, url);
AddHeaders(requestMessage);
var uri = new Uri(url, UriKind.RelativeOrAbsolute);
if (!uri.IsAbsoluteUri)
{
var remoteServiceConfig = RemoteServiceOptions.RemoteServices.GetConfigurationOrDefault("Default");
client.BaseAddress = new Uri(remoteServiceConfig.BaseUrl);
await HttpClientAuthenticator.Authenticate(new RemoteServiceHttpClientAuthenticateContext(client, requestMessage, new RemoteServiceConfiguration(remoteServiceConfig.BaseUrl), string.Empty));
}
var response = await client.SendAsync(requestMessage);
return await response.Content.ReadAsStringAsync();
}
protected virtual void AddHeaders(HttpRequestMessage requestMessage)
{
if (CurrentTenant.Id.HasValue)
{
requestMessage.Headers.Add(TenantResolverConsts.DefaultTenantKey, CurrentTenant.Id.Value.ToString());
}
var currentCulture = CultureInfo.CurrentUICulture.Name ?? CultureInfo.CurrentCulture.Name;
if (!currentCulture.IsNullOrEmpty())
{
requestMessage.Headers.AcceptLanguage.Add(new (currentCulture));
}
}
}
}

84
framework/src/Volo.Abp.BlazoriseUI/Components/ObjectExtending/LookupExtensionProperty.razor.cs

@ -9,10 +9,10 @@ using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Components.Web.Extensibility;
using Volo.Abp.Data;
using Volo.Abp.Http;
using Volo.Abp.Http.Client;
//using Volo.Abp.Http.Client.Authentication;
using Volo.Abp.MultiTenancy;
using Volo.Abp.ObjectExtending;
@ -23,35 +23,20 @@ namespace Volo.Abp.BlazoriseUI.Components.ObjectExtending
{
protected List<SelectItem<object>> lookupItems;
[Inject]
public IStringLocalizerFactory StringLocalizerFactory { get; set; }
[Inject] public IStringLocalizerFactory StringLocalizerFactory { get; set; }
[Parameter]
public TEntity Entity { get; set; }
[Parameter] public TEntity Entity { get; set; }
[Parameter]
public ObjectExtensionPropertyInfo PropertyInfo { get; set; }
[Parameter] public ObjectExtensionPropertyInfo PropertyInfo { get; set; }
//[Inject]
//public IRemoteServiceHttpClientAuthenticator ClientAuthenticator { get; set; }
//[Inject]
//public IHttpClientFactory HttpClientFactory { get; set; }
[Inject]
public ICurrentTenant CurrentTenant { get; set; }
//[Inject]
//public IOptions<AbpRemoteServiceOptions> RemoteServiceOptions { get; set; }
[Inject] public ILookupApiRequestService LookupApiService { get; set; }
public string TextPropertyName => PropertyInfo.Name + "_Text";
public object SelectedValue
{
get
{
return Entity.GetProperty(PropertyInfo.Name);
}
get { return Entity.GetProperty(PropertyInfo.Name); }
set
{
Entity.SetProperty(PropertyInfo.Name, value, false);
@ -67,7 +52,8 @@ namespace Volo.Abp.BlazoriseUI.Components.ObjectExtending
protected override void OnParametersSet()
{
var value = Entity.GetProperty(PropertyInfo.Name);
if (value != null)
var text = Entity.GetProperty(TextPropertyName);
if (value != null && text != null)
{
lookupItems.Add(new SelectItem<object>
{
@ -88,34 +74,24 @@ namespace Volo.Abp.BlazoriseUI.Components.ObjectExtending
var selectItems = new List<SelectItem<object>>();
var url = PropertyInfo.Lookup.Url;
var uri = new Uri(url, UriKind.RelativeOrAbsolute);
if (!filter.IsNullOrEmpty())
{
url += $"?{PropertyInfo.Lookup.FilterParamName}={filter.Trim()}";
}
//var client = HttpClientFactory.CreateClient();
//var requestMessage = new HttpRequestMessage(HttpMethod.Get, url);
//AddHeaders(requestMessage);
//if (!uri.IsAbsoluteUri)
//{
// var remoteServiceConfig = RemoteServiceOptions.Value.RemoteServices.GetConfigurationOrDefault("Default");
// client.BaseAddress = new Uri(remoteServiceConfig.BaseUrl);
// await ClientAuthenticator.Authenticate(new RemoteServiceHttpClientAuthenticateContext(client, requestMessage, new RemoteServiceConfiguration(remoteServiceConfig.BaseUrl), string.Empty));
//}
//var response = await client.SendAsync(requestMessage);
//var document = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync());
//var itemsArrayProp = document.RootElement.GetProperty(PropertyInfo.Lookup.ResultListPropertyName);
//foreach (var item in itemsArrayProp.EnumerateArray())
//{
// selectItems.Add(new SelectItem<object>
// {
// Text = item.GetProperty(PropertyInfo.Lookup.DisplayPropertyName).GetString(),
// Value = JsonSerializer.Deserialize(item.GetProperty(PropertyInfo.Lookup.ValuePropertyName).GetRawText(), PropertyInfo.Type)
// });
//}
var response = await LookupApiService.SendAsync(url);
var document = JsonDocument.Parse(response);
var itemsArrayProp = document.RootElement.GetProperty(PropertyInfo.Lookup.ResultListPropertyName);
foreach (var item in itemsArrayProp.EnumerateArray())
{
selectItems.Add(new SelectItem<object>
{
Text = item.GetProperty(PropertyInfo.Lookup.DisplayPropertyName).GetString(),
Value = JsonSerializer.Deserialize(
item.GetProperty(PropertyInfo.Lookup.ValuePropertyName).GetRawText(), PropertyInfo.Type)
});
}
return selectItems;
}
@ -129,21 +105,5 @@ namespace Volo.Abp.BlazoriseUI.Components.ObjectExtending
{
lookupItems = await GetLookupItemsAsync(filter);
}
protected virtual void AddHeaders(HttpRequestMessage requestMessage)
{
if (CurrentTenant.Id.HasValue)
{
requestMessage.Headers.Add(TenantResolverConsts.DefaultTenantKey, CurrentTenant.Id.Value.ToString());
}
var currentCulture = CultureInfo.CurrentUICulture.Name ?? CultureInfo.CurrentCulture.Name;
if (!currentCulture.IsNullOrEmpty())
{
requestMessage.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue(currentCulture));
}
//requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(MimeTypes.Application.Json));
}
}
}
}
Loading…
Cancel
Save