forked from dapr/dotnet-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DaprGenericClientBuilder.cs
226 lines (200 loc) · 8.97 KB
/
DaprGenericClientBuilder.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// ------------------------------------------------------------------------
// Copyright 2024 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
namespace Dapr.Common;
using System;
using System.Net.Http;
using System.Text.Json;
using Grpc.Net.Client;
/// <summary>
/// Builder for building a generic Dapr client.
/// </summary>
public abstract class DaprGenericClientBuilder<TClientBuilder> where TClientBuilder : class
{
/// <summary>
/// Initializes a new instance of the <see cref="DaprGenericClientBuilder{TClientBuilder}"/> class.
/// </summary>
protected DaprGenericClientBuilder()
{
this.GrpcEndpoint = DaprDefaults.GetDefaultGrpcEndpoint();
this.HttpEndpoint = DaprDefaults.GetDefaultHttpEndpoint();
this.GrpcChannelOptions = new GrpcChannelOptions()
{
// The gRPC client doesn't throw the right exception for cancellation
// by default, this switches that behavior on.
ThrowOperationCanceledOnCancellation = true,
};
this.JsonSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
this.DaprApiToken = DaprDefaults.GetDefaultDaprApiToken();
}
/// <summary>
/// Property exposed for testing purposes.
/// </summary>
internal string GrpcEndpoint { get; private set; }
/// <summary>
/// Property exposed for testing purposes.
/// </summary>
internal string HttpEndpoint { get; private set; }
/// <summary>
/// Property exposed for testing purposes.
/// </summary>
internal Func<HttpClient>? HttpClientFactory { get; set; }
/// <summary>
/// Property exposed for testing purposes.
/// </summary>
internal JsonSerializerOptions JsonSerializerOptions { get; private set; }
/// <summary>
/// Property exposed for testing purposes.
/// </summary>
internal GrpcChannelOptions GrpcChannelOptions { get; private set; }
/// <summary>
/// Property exposed for testing purposes.
/// </summary>
internal string DaprApiToken { get; private set; }
/// <summary>
/// Property exposed for testing purposes.
/// </summary>
internal TimeSpan Timeout { get; private set; }
/// <summary>
/// Overrides the HTTP endpoint used by the Dapr client for communicating with the Dapr runtime.
/// </summary>
/// <param name="httpEndpoint">
/// The URI endpoint to use for HTTP calls to the Dapr runtime. The default value will be
/// <c>DAPR_HTTP_ENDPOINT</c> first, or <c>http://127.0.0.1:DAPR_HTTP_PORT</c> as fallback
/// where <c>DAPR_HTTP_ENDPOINT</c> and <c>DAPR_HTTP_PORT</c> represents the value of the
/// corresponding environment variables.
/// </param>
/// <returns>The <see cref="DaprGenericClientBuilder{TClientBuilder}" /> instance.</returns>
public DaprGenericClientBuilder<TClientBuilder> UseHttpEndpoint(string httpEndpoint)
{
ArgumentVerifier.ThrowIfNullOrEmpty(httpEndpoint, nameof(httpEndpoint));
this.HttpEndpoint = httpEndpoint;
return this;
}
/// <summary>
/// Specifies a factory used to create the <see cref="HttpClient"/> to use.
/// </summary>
internal DaprGenericClientBuilder<TClientBuilder> UseHttpClientFactory(Func<HttpClient> factory)
{
this.HttpClientFactory = factory;
return this;
}
/// <summary>
/// Overrides the legacy mechanism for building an HttpClient and uses the new <see cref="IHttpClientFactory"/>
/// introduced in .NET Core 2.1.
/// </summary>
/// <param name="httpClientFactory">The factory used to create <see cref="HttpClient"/> instances.</param>
/// <returns></returns>
public DaprGenericClientBuilder<TClientBuilder> UseHttpClientFactory(IHttpClientFactory httpClientFactory)
{
this.HttpClientFactory = httpClientFactory.CreateClient;
return this;
}
/// <summary>
/// Overrides the gRPC endpoint used by the Dapr client for communicating with the Dapr runtime.
/// </summary>
/// <param name="grpcEndpoint">
/// The URI endpoint to use for gRPC calls to the Dapr runtime. The default value will be
/// <c>http://127.0.0.1:DAPR_GRPC_PORT</c> where <c>DAPR_GRPC_PORT</c> represents the value of the
/// <c>DAPR_GRPC_PORT</c> environment variable.
/// </param>
/// <returns>The <see cref="DaprGenericClientBuilder{TClientBuilder}" /> instance.</returns>
public DaprGenericClientBuilder<TClientBuilder> UseGrpcEndpoint(string grpcEndpoint)
{
ArgumentVerifier.ThrowIfNullOrEmpty(grpcEndpoint, nameof(grpcEndpoint));
this.GrpcEndpoint = grpcEndpoint;
return this;
}
/// <summary>
/// <para>
/// Uses the specified <see cref="JsonSerializerOptions"/> when serializing or deserializing using <see cref="System.Text.Json"/>.
/// </para>
/// <para>
/// The default value is created using <see cref="JsonSerializerDefaults.Web" />.
/// </para>
/// </summary>
/// <param name="options">Json serialization options.</param>
/// <returns>The <see cref="DaprGenericClientBuilder{TClientBuilder}" /> instance.</returns>
public DaprGenericClientBuilder<TClientBuilder> UseJsonSerializationOptions(JsonSerializerOptions options)
{
this.JsonSerializerOptions = options;
return this;
}
/// <summary>
/// Uses the provided <paramref name="grpcChannelOptions" /> for creating the <see cref="GrpcChannel" />.
/// </summary>
/// <param name="grpcChannelOptions">The <see cref="GrpcChannelOptions" /> to use for creating the <see cref="GrpcChannel" />.</param>
/// <returns>The <see cref="DaprGenericClientBuilder{TClientBuilder}" /> instance.</returns>
public DaprGenericClientBuilder<TClientBuilder> UseGrpcChannelOptions(GrpcChannelOptions grpcChannelOptions)
{
this.GrpcChannelOptions = grpcChannelOptions;
return this;
}
/// <summary>
/// Adds the provided <paramref name="apiToken" /> on every request to the Dapr runtime.
/// </summary>
/// <param name="apiToken">The token to be added to the request headers/>.</param>
/// <returns>The <see cref="DaprGenericClientBuilder{TClientBuilder}" /> instance.</returns>
public DaprGenericClientBuilder<TClientBuilder> UseDaprApiToken(string apiToken)
{
this.DaprApiToken = apiToken;
return this;
}
/// <summary>
/// Sets the timeout for the HTTP client used by the Dapr client.
/// </summary>
/// <param name="timeout"></param>
/// <returns></returns>
public DaprGenericClientBuilder<TClientBuilder> UseTimeout(TimeSpan timeout)
{
this.Timeout = timeout;
return this;
}
/// <summary>
/// Builds out the inner DaprClient that provides the core shape of the
/// runtime gRPC client used by the consuming package.
/// </summary>
/// <exception cref="InvalidOperationException"></exception>
protected (GrpcChannel channel, HttpClient httpClient, Uri httpEndpoint) BuildDaprClientDependencies()
{
var grpcEndpoint = new Uri(this.GrpcEndpoint);
if (grpcEndpoint.Scheme != "http" && grpcEndpoint.Scheme != "https")
{
throw new InvalidOperationException("The gRPC endpoint must use http or https.");
}
if (grpcEndpoint.Scheme.Equals(Uri.UriSchemeHttp))
{
// Set correct switch to make secure gRPC service calls. This switch must be set before creating the GrpcChannel.
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
}
var httpEndpoint = new Uri(this.HttpEndpoint);
if (httpEndpoint.Scheme != "http" && httpEndpoint.Scheme != "https")
{
throw new InvalidOperationException("The HTTP endpoint must use http or https.");
}
var channel = GrpcChannel.ForAddress(this.GrpcEndpoint, this.GrpcChannelOptions);
var httpClient = HttpClientFactory is not null ? HttpClientFactory() : new HttpClient();
if (this.Timeout > TimeSpan.Zero)
{
httpClient.Timeout = this.Timeout;
}
return (channel, httpClient, httpEndpoint);
}
/// <summary>
/// Builds the client instance from the properties of the builder.
/// </summary>
/// <returns>The Dapr client instance.</returns>
/// <summary>
/// Builds the client instance from the properties of the builder.
/// </summary>
public abstract TClientBuilder Build();
}