-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathDaprGenericClientBuilder.cs
More file actions
209 lines (183 loc) · 8.53 KB
/
DaprGenericClientBuilder.cs
File metadata and controls
209 lines (183 loc) · 8.53 KB
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
// ------------------------------------------------------------------------
// 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.
// ------------------------------------------------------------------------
using System.Text.Json;
using Dapr.Common.Http;
using Grpc.Net.Client;
using Microsoft.Extensions.Configuration;
namespace Dapr.Common;
/// <summary>
/// Builder for building a generic Dapr client.
/// </summary>
public abstract class DaprGenericClientBuilder<TClientBuilder> where TClientBuilder : class, IDaprClient
{
private readonly IDaprHttpClientFactory daprHttpClientFactory;
/// <summary>
/// Initializes a new instance of the <see cref="DaprGenericClientBuilder{TClientBuilder}"/> class.
/// </summary>
protected DaprGenericClientBuilder(IDaprHttpClientFactory daprHttpClientFactory, IConfiguration? configuration = null)
{
ArgumentNullException.ThrowIfNull(daprHttpClientFactory);
this.daprHttpClientFactory = daprHttpClientFactory;
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(configuration);
}
/// <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>
public 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>
public 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>
/// 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 internal (GrpcChannel channel, HttpClient httpClient, Uri httpEndpoint, string daprApiToken) 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.");
}
//Configure the HTTP client
var httpClient = daprHttpClientFactory.CreateClient();
//Update the timeout to use the one provided in this builder
if (this.Timeout > TimeSpan.Zero)
{
httpClient.Timeout = this.Timeout;
}
this.GrpcChannelOptions.HttpClient = httpClient;
var channel = GrpcChannel.ForAddress(this.GrpcEndpoint, this.GrpcChannelOptions);
return (channel, httpClient, httpEndpoint, this.DaprApiToken);
}
/// <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();
}