forked from man-group/dapr-sidekick-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DaprClient.cs
88 lines (80 loc) · 4.08 KB
/
DaprClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#if !NET35
// Original source : https://github.com/dapr/dotnet-sdk/blob/master/src/Dapr.Client/DaprClient.cs
// See DAPR_DOTNET_SDK_LICENSE in this directory for license information.
// IMPORTANT : This class has been modified to allow injection of the createInvocationHandler() method.
using System;
using System.Collections.Generic;
using System.Net.Http;
namespace Man.Dapr.Sidekick.DaprClient
{
internal class DaprClient
{
/// <summary>
/// <para>
/// Creates an <see cref="HttpClient" /> that can be used to perform Dapr service
/// invocation using <see cref="HttpRequestMessage" /> objects.
/// </para>
/// <para>
/// The client will read the <see cref="HttpRequestMessage.RequestUri" /> property, and
/// interpret the hostname as the destination <c>app-id</c>. The <see cref="HttpRequestMessage.RequestUri" />
/// property will be replaced with a new URI with the authority section replaced by <paramref name="daprEndpoint" />
/// and the path portion of the URI rewitten to follow the format of a Dapr service invocation request.
/// </para>
/// </summary>
/// <param name="appId">
/// An optional <c>app-id</c>. If specified, the <c>app-id</c> will be configured as the value of
/// <see cref="HttpClient.BaseAddress" /> so that relative URIs can be used.
/// </param>
/// <param name="daprEndpoint">The HTTP endpoint of the Dapr process to use for service invocation calls.</param>
/// <param name="daprApiToken">The token to be added to all request headers to Dapr runtime.</param>
/// <param name="createInvocationHandler">An optional method for externally creating the <see cref="InvocationHandler"/> instance.</param>
/// <param name="createInnerHandler">An optional method for externally creating the inner handler for the <see cref="InvocationHandler"/>. If not specified a <see cref="HttpClientHandler"/> instance will be used.</param>
/// <returns>An <see cref="HttpClient" /> that can be used to perform service invocation requests.</returns>
/// <remarks>
/// <para>
/// The <see cref="HttpClient" /> object is intended to be a long-lived and holds access to networking resources.
/// Since the value of <paramref name="daprEndpoint" /> will not change during the lifespan of the application,
/// a single client object can be reused for the life of the application.
/// </para>
/// </remarks>
public static HttpClient CreateInvokeHttpClient(
string appId = null,
string daprEndpoint = null,
string daprApiToken = null,
Func<InvocationHandler> createInvocationHandler = null,
Func<HttpMessageHandler> createInnerHandler = null)
{
var handler = createInvocationHandler?.Invoke() ?? new InvocationHandler();
handler.InnerHandler ??= createInnerHandler?.Invoke() ?? new HttpClientHandler();
handler.DaprApiToken ??= daprApiToken;
if (daprEndpoint is string)
{
// DaprEndpoint performs validation.
handler.DaprEndpoint = daprEndpoint;
}
var httpClient = new HttpClient(handler);
if (appId is string)
{
try
{
httpClient.BaseAddress = new Uri($"http://{appId}");
}
catch (UriFormatException inner)
{
throw new ArgumentException("The appId must be a valid hostname.", nameof(appId), inner);
}
}
return httpClient;
}
internal static KeyValuePair<string, string> GetDaprApiTokenHeader(string apiToken)
{
KeyValuePair<string, string> apiTokenHeader = default;
if (!string.IsNullOrWhiteSpace(apiToken))
{
apiTokenHeader = new KeyValuePair<string, string>(DaprConstants.DaprApiTokenHeader, apiToken);
}
return apiTokenHeader;
}
}
}
#endif