Browse Source

Add abp headers to Dapr's `HttpClient`.

pull/14877/head
maliming 4 years ago
parent
commit
dc68fe77d7
No known key found for this signature in database GPG Key ID: 96224957E51C89E
  1. 44
      framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprClientFactory.cs

44
framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprClientFactory.cs

@ -1,10 +1,14 @@
using System; using System;
using System.Globalization;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json; using System.Text.Json;
using Dapr.Client; using Dapr.Client;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Json.SystemTextJson; using Volo.Abp.Json.SystemTextJson;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Tracing;
namespace Volo.Abp.Dapr; namespace Volo.Abp.Dapr;
@ -13,13 +17,21 @@ public class AbpDaprClientFactory : IAbpDaprClientFactory, ISingletonDependency
protected AbpDaprOptions DaprOptions { get; } protected AbpDaprOptions DaprOptions { get; }
protected JsonSerializerOptions JsonSerializerOptions { get; } protected JsonSerializerOptions JsonSerializerOptions { get; }
protected IDaprApiTokenProvider DaprApiTokenProvider { get; } protected IDaprApiTokenProvider DaprApiTokenProvider { get; }
protected ICurrentTenant CurrentTenant { get; }
protected ICorrelationIdProvider CorrelationIdProvider { get; }
protected IOptions<AbpCorrelationIdOptions> AbpCorrelationIdOptions { get; }
public AbpDaprClientFactory( public AbpDaprClientFactory(
IOptions<AbpDaprOptions> options, IOptions<AbpDaprOptions> options,
IOptions<AbpSystemTextJsonSerializerOptions> systemTextJsonSerializerOptions, IOptions<AbpSystemTextJsonSerializerOptions> systemTextJsonSerializerOptions,
IDaprApiTokenProvider daprApiTokenProvider) IDaprApiTokenProvider daprApiTokenProvider,
ICurrentTenant currentTenant, ICorrelationIdProvider correlationIdProvider,
IOptions<AbpCorrelationIdOptions> abpCorrelationIdOptions)
{ {
DaprApiTokenProvider = daprApiTokenProvider; DaprApiTokenProvider = daprApiTokenProvider;
CurrentTenant = currentTenant;
CorrelationIdProvider = correlationIdProvider;
AbpCorrelationIdOptions = abpCorrelationIdOptions;
DaprOptions = options.Value; DaprOptions = options.Value;
JsonSerializerOptions = CreateJsonSerializerOptions(systemTextJsonSerializerOptions.Value); JsonSerializerOptions = CreateJsonSerializerOptions(systemTextJsonSerializerOptions.Value);
} }
@ -61,11 +73,39 @@ public class AbpDaprClientFactory : IAbpDaprClientFactory, ISingletonDependency
daprEndpoint = DaprOptions.HttpEndpoint; daprEndpoint = DaprOptions.HttpEndpoint;
} }
return DaprClient.CreateInvokeHttpClient( var httpClient = DaprClient.CreateInvokeHttpClient(
appId, appId,
daprEndpoint, daprEndpoint,
daprApiToken ?? DaprApiTokenProvider.GetDaprApiToken() daprApiToken ?? DaprApiTokenProvider.GetDaprApiToken()
); );
AddHeaders(httpClient);
return httpClient;
}
protected virtual void AddHeaders(HttpClient httpClient)
{
//CorrelationId
httpClient.DefaultRequestHeaders.Add(AbpCorrelationIdOptions.Value.HttpHeaderName, CorrelationIdProvider.Get());
//TenantId
if (CurrentTenant.Id.HasValue)
{
//TODO: Use AbpAspNetCoreMultiTenancyOptions to get the key
httpClient.DefaultRequestHeaders.Add(TenantResolverConsts.DefaultTenantKey, CurrentTenant.Id.Value.ToString());
}
//Culture
//TODO: Is that the way we want? Couldn't send the culture (not ui culture)
var currentCulture = CultureInfo.CurrentUICulture.Name ?? CultureInfo.CurrentCulture.Name;
if (!currentCulture.IsNullOrEmpty())
{
httpClient.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue(currentCulture));
}
//X-Requested-With
httpClient.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");
} }
protected virtual JsonSerializerOptions CreateJsonSerializerOptions(AbpSystemTextJsonSerializerOptions systemTextJsonSerializerOptions) protected virtual JsonSerializerOptions CreateJsonSerializerOptions(AbpSystemTextJsonSerializerOptions systemTextJsonSerializerOptions)

Loading…
Cancel
Save