|
|
|
@ -3,9 +3,13 @@ using System.Globalization; |
|
|
|
using System.Net.Http; |
|
|
|
using System.Net.Http.Headers; |
|
|
|
using System.Text.Json; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Dapr.Client; |
|
|
|
using IdentityModel.Client; |
|
|
|
using Microsoft.Extensions.Options; |
|
|
|
using Volo.Abp.DependencyInjection; |
|
|
|
using Volo.Abp.Http.Client; |
|
|
|
using Volo.Abp.Http.Client.Authentication; |
|
|
|
using Volo.Abp.Json.SystemTextJson; |
|
|
|
using Volo.Abp.MultiTenancy; |
|
|
|
using Volo.Abp.Tracing; |
|
|
|
@ -20,23 +24,26 @@ public class AbpDaprClientFactory : IAbpDaprClientFactory, ISingletonDependency |
|
|
|
protected ICurrentTenant CurrentTenant { get; } |
|
|
|
protected ICorrelationIdProvider CorrelationIdProvider { get; } |
|
|
|
protected IOptions<AbpCorrelationIdOptions> AbpCorrelationIdOptions { get; } |
|
|
|
protected IRemoteServiceHttpClientAuthenticator RemoteServiceHttpClientAuthenticator { get; } |
|
|
|
|
|
|
|
public AbpDaprClientFactory( |
|
|
|
IOptions<AbpDaprOptions> options, |
|
|
|
IOptions<AbpSystemTextJsonSerializerOptions> systemTextJsonSerializerOptions, |
|
|
|
IDaprApiTokenProvider daprApiTokenProvider, |
|
|
|
ICurrentTenant currentTenant, ICorrelationIdProvider correlationIdProvider, |
|
|
|
IOptions<AbpCorrelationIdOptions> abpCorrelationIdOptions) |
|
|
|
IOptions<AbpCorrelationIdOptions> abpCorrelationIdOptions, |
|
|
|
IRemoteServiceHttpClientAuthenticator remoteServiceHttpClientAuthenticator) |
|
|
|
{ |
|
|
|
DaprApiTokenProvider = daprApiTokenProvider; |
|
|
|
CurrentTenant = currentTenant; |
|
|
|
CorrelationIdProvider = correlationIdProvider; |
|
|
|
AbpCorrelationIdOptions = abpCorrelationIdOptions; |
|
|
|
RemoteServiceHttpClientAuthenticator = remoteServiceHttpClientAuthenticator; |
|
|
|
DaprOptions = options.Value; |
|
|
|
JsonSerializerOptions = CreateJsonSerializerOptions(systemTextJsonSerializerOptions.Value); |
|
|
|
} |
|
|
|
|
|
|
|
public virtual DaprClient Create(Action<DaprClientBuilder>? builderAction = null) |
|
|
|
public virtual Task<DaprClient> CreateAsync(Action<DaprClientBuilder>? builderAction = null) |
|
|
|
{ |
|
|
|
var builder = new DaprClientBuilder() |
|
|
|
.UseJsonSerializationOptions(JsonSerializerOptions); |
|
|
|
@ -59,10 +66,10 @@ public class AbpDaprClientFactory : IAbpDaprClientFactory, ISingletonDependency |
|
|
|
|
|
|
|
builderAction?.Invoke(builder); |
|
|
|
|
|
|
|
return builder.Build(); |
|
|
|
return Task.FromResult(builder.Build()); |
|
|
|
} |
|
|
|
|
|
|
|
public virtual HttpClient CreateHttpClient( |
|
|
|
public virtual async Task<HttpClient> CreateHttpClientAsync( |
|
|
|
string? appId = null, |
|
|
|
string? daprEndpoint = null, |
|
|
|
string? daprApiToken = null) |
|
|
|
@ -81,6 +88,22 @@ public class AbpDaprClientFactory : IAbpDaprClientFactory, ISingletonDependency |
|
|
|
|
|
|
|
AddHeaders(httpClient); |
|
|
|
|
|
|
|
var request = new HttpRequestMessage(); |
|
|
|
await RemoteServiceHttpClientAuthenticator.Authenticate( |
|
|
|
new RemoteServiceHttpClientAuthenticateContext( |
|
|
|
httpClient, |
|
|
|
request, |
|
|
|
new RemoteServiceConfiguration("/"), |
|
|
|
string.Empty |
|
|
|
) |
|
|
|
); |
|
|
|
|
|
|
|
var bearerToken = request.Headers.Authorization?.Parameter; |
|
|
|
if (!bearerToken.IsNullOrWhiteSpace()) |
|
|
|
{ |
|
|
|
httpClient.SetBearerToken(bearerToken); |
|
|
|
} |
|
|
|
|
|
|
|
return httpClient; |
|
|
|
} |
|
|
|
|
|
|
|
|