-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
SchemaRegistryClient.cs
322 lines (294 loc) · 17.4 KB
/
SchemaRegistryClient.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Data.SchemaRegistry.Models;
namespace Azure.Data.SchemaRegistry
{
/// <summary>
/// The Schema Registry client provides operations to interact with the Schema Registry service.
/// </summary>
public class SchemaRegistryClient
{
private readonly ClientDiagnostics _clientDiagnostics;
internal SchemaRestClient RestClient { get; }
private const string CredentialScope = "https://eventhubs.azure.net/.default";
/// <summary>
/// Initializes a new instance of the <see cref="SchemaRegistryClient"/>.
/// </summary>
public SchemaRegistryClient(string fullyQualifiedNamespace, TokenCredential credential) : this(fullyQualifiedNamespace, credential, new SchemaRegistryClientOptions())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SchemaRegistryClient"/>.
/// </summary>
public SchemaRegistryClient(string fullyQualifiedNamespace, TokenCredential credential, SchemaRegistryClientOptions options) : this(
new ClientDiagnostics(options),
HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, CredentialScope)),
fullyQualifiedNamespace,
options.Version)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SchemaRegistryClient"/> class for mocking use in testing.
/// </summary>
/// <remarks>
/// This constructor exists only to support mocking. When used, class state is not fully initialized, and
/// will not function correctly; virtual members are meant to be mocked.
///</remarks>
protected SchemaRegistryClient()
{
}
/// <summary>Initializes a new instance of <see cref="SchemaRegistryClient"/>.</summary>
/// <param name="clientDiagnostics">The handler for diagnostic messaging in the client.</param>
/// <param name="pipeline">The HTTP pipeline for sending and receiving REST requests and responses.</param>
/// <param name="fullyQualifiedNamespace">The fully qualified namespace. For example, myschemaregistry.servicebus.windows.net.</param>
/// <param name="apiVersion">The API version of the service.</param>
internal SchemaRegistryClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, string fullyQualifiedNamespace, string apiVersion)
{
RestClient = new SchemaRestClient(clientDiagnostics, pipeline, fullyQualifiedNamespace, apiVersion);
_clientDiagnostics = clientDiagnostics;
FullyQualifiedNamespace = fullyQualifiedNamespace;
}
/// <summary>
/// Gets the fully qualified namespace that the client is connecting to.
/// </summary>
public string FullyQualifiedNamespace { get; }
private const string RegisterSchemaScopeName = "SchemaRegistryClient.RegisterSchema";
private const string GetSchemaIdScopeName = "SchemaRegistryClient.GetSchemaId";
private const string GetSchemaScopeName = "SchemaRegistryClient.GetSchema";
/// <summary>
/// Registers a schema with the SchemaRegistry service.
/// </summary>
/// <param name="groupName">The name of the SchemaRegistry group.</param>
/// <param name="schemaName">The name of the schema.</param>
/// <param name="schemaDefinition">The string representation of the schema's content.</param>
/// <param name="format">The serialization format of the schema.</param>
/// <param name="cancellationToken">The cancellation token for the operation.</param>
/// <returns>The properties of the schema.</returns>
public virtual async Task<Response<SchemaProperties>> RegisterSchemaAsync(
string groupName,
string schemaName,
string schemaDefinition,
SchemaFormat format,
CancellationToken cancellationToken = default) =>
await RegisterSchemaInternalAsync(groupName, schemaName, schemaDefinition, format, true, cancellationToken)
.ConfigureAwait(false);
/// <summary>
/// Registers a schema with the SchemaRegistry service.
/// If the schema did not previously exist in the Schema Registry instance, it is added to the instance and assigned a schema ID.
/// If the schema did previous exist in the Schema Registry instance, a new version of the schema is added to the instance and assigned a new schema ID.
/// </summary>
/// <param name="groupName">The name of the SchemaRegistry group.</param>
/// <param name="schemaName">The name of the schema.</param>
/// <param name="schemaDefinition">The string representation of the schema's content.</param>
/// <param name="format">The serialization format of the schema.</param>
/// <param name="cancellationToken">The cancellation token for the operation.</param>
/// <returns>The properties of the schema.</returns>
public virtual Response<SchemaProperties> RegisterSchema(
string groupName,
string schemaName,
string schemaDefinition,
SchemaFormat format,
CancellationToken cancellationToken = default) =>
RegisterSchemaInternalAsync(groupName, schemaName, schemaDefinition, format, false, cancellationToken)
.EnsureCompleted();
private async Task<Response<SchemaProperties>> RegisterSchemaInternalAsync(
string groupName,
string schemaName,
string schemaDefinition,
SchemaFormat format,
bool async,
CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope(RegisterSchemaScopeName);
scope.Start();
try
{
ResponseWithHeaders<SchemaRegisterHeaders> response;
if (async)
{
response = await RestClient.RegisterAsync(groupName, schemaName, format.ContentType, new BinaryData(schemaDefinition).ToStream(), cancellationToken).ConfigureAwait(false);
}
else
{
response = RestClient.Register(groupName, schemaName,format.ContentType, new BinaryData(schemaDefinition).ToStream(), cancellationToken);
}
var properties = new SchemaProperties(format, response.Headers.SchemaId, response.Headers.SchemaGroupName, response.Headers.SchemaName, response.Headers.SchemaVersion!.Value);
return Response.FromValue(properties, response);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Gets the schema ID associated with the schema from the SchemaRegistry service.
/// </summary>
/// <param name="groupName">The name of the SchemaRegistry group.</param>
/// <param name="schemaName">The name of the schema.</param>
/// <param name="schemaDefinition">The string representation of the schema's content.</param>
/// <param name="format">The serialization format of the schema.</param>
/// <param name="cancellationToken">The cancellation token for the operation.</param>
/// <returns>The properties of the schema, including the schema ID provided by the service.</returns>
#pragma warning disable AZC0015 // Unexpected client method return type.
public virtual async Task<Response<SchemaProperties>> GetSchemaPropertiesAsync(
string groupName,
string schemaName,
string schemaDefinition,
SchemaFormat format,
CancellationToken cancellationToken = default) =>
#pragma warning restore AZC0015 // Unexpected client method return type.
await GetSchemaPropertiesInternalAsync(groupName, schemaName, schemaDefinition, format, true, cancellationToken)
.ConfigureAwait(false);
/// <summary>
/// Gets the schema ID associated with the schema from the SchemaRegistry service.
/// </summary>
/// <param name="groupName">The name of the SchemaRegistry group.</param>
/// <param name="schemaName">The name of the schema.</param>
/// <param name="schemaDefinition">The string representation of the schema's content.</param>
/// <param name="format">The serialization format of the schema.</param>
/// <param name="cancellationToken">The cancellation token for the operation.</param>
/// <returns>The properties of the schema, including the schema ID provided by the service.</returns>
#pragma warning disable AZC0015 // Unexpected client method return type.
public virtual Response<SchemaProperties> GetSchemaProperties(
string groupName,
string schemaName,
string schemaDefinition,
SchemaFormat format,
CancellationToken cancellationToken = default) =>
#pragma warning restore AZC0015 // Unexpected client method return type.
GetSchemaPropertiesInternalAsync(groupName, schemaName, schemaDefinition, format, false, cancellationToken).EnsureCompleted();
private async Task<Response<SchemaProperties>> GetSchemaPropertiesInternalAsync(
string groupName,
string schemaName,
string schemaDefinition,
SchemaFormat format,
bool async,
CancellationToken cancellationToken)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope(GetSchemaIdScopeName);
scope.Start();
try
{
ResponseWithHeaders<SchemaQueryIdByContentHeaders> response;
if (async)
{
response = await RestClient.QueryIdByContentAsync(groupName, schemaName, format.ContentType, new BinaryData(schemaDefinition).ToStream(), cancellationToken).ConfigureAwait(false);
}
else
{
response = RestClient.QueryIdByContent(groupName, schemaName, format.ContentType, new BinaryData(schemaDefinition).ToStream(), cancellationToken);
}
var properties = new SchemaProperties(format, response.Headers.SchemaId, response.Headers.SchemaGroupName, response.Headers.SchemaName, response.Headers.SchemaVersion!.Value);
return Response.FromValue(properties, response);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
/// <summary>
/// Gets the schema content associated with the schema ID from the SchemaRegistry service.
/// </summary>
/// <param name="schemaId">The schema ID of the the schema from the SchemaRegistry.</param>
/// <param name="cancellationToken">The cancellation token for the operation.</param>
/// <returns>The properties of the schema, including the schema content provided by the service.</returns>
#pragma warning disable AZC0015 // Unexpected client method return type.
public virtual async Task<Response<SchemaRegistrySchema>> GetSchemaAsync(string schemaId, CancellationToken cancellationToken = default) =>
#pragma warning restore AZC0015 // Unexpected client method return type.
await GetSchemaInternalAsync(schemaId, true, cancellationToken).ConfigureAwait(false);
/// <summary>
/// Gets the schema content associated with the group name, schema name, and version from the SchemaRegistry service.
/// </summary>
/// <param name="groupName"> Schema group under which schema is registered. Group's serialization type should match the serialization type specified in the request</param>
/// <param name="schemaName"> Name of schema. </param>
/// <param name="schemaVersion"> Version number of specific schema. </param>
/// <param name="cancellationToken">The cancellation token for the operation.</param>
/// <returns>The properties of the schema, including the schema content provided by the service.</returns>
#pragma warning disable AZC0015 // Unexpected client method return type.
public virtual async Task<Response<SchemaRegistrySchema>> GetSchemaAsync(string groupName, string schemaName, int schemaVersion, CancellationToken cancellationToken = default) =>
#pragma warning restore AZC0015 // Unexpected client method return type.
await GetSchemaInternalAsync(groupName, schemaName, schemaVersion, true, cancellationToken).ConfigureAwait(false);
/// <summary>
/// Gets the schema content associated with the schema ID from the SchemaRegistry service.
/// </summary>
/// <param name="schemaId">The schema ID of the the schema from the SchemaRegistry.</param>
/// <param name="cancellationToken">The cancellation token for the operation.</param>
/// <returns>The properties of the schema, including the schema content provided by the service.</returns>
#pragma warning disable AZC0015 // Unexpected client method return type.
public virtual Response<SchemaRegistrySchema> GetSchema(string schemaId, CancellationToken cancellationToken = default) =>
#pragma warning restore AZC0015 // Unexpected client method return type.
GetSchemaInternalAsync(schemaId, false, cancellationToken).EnsureCompleted();
/// <summary>
/// Gets the schema content associated with the group name, schema name, and version from the SchemaRegistry service.
/// </summary>
/// <param name="groupName"> Schema group under which schema is registered. Group's serialization type should match the serialization type specified in the request. </param>
/// <param name="schemaName"> Name of schema. </param>
/// <param name="schemaVersion"> Version number of specific schema. </param>
/// <param name="cancellationToken">The cancellation token for the operation.</param>
/// <returns>The properties of the schema, including the schema content provided by the service.</returns>
#pragma warning disable AZC0015 // Unexpected client method return type.
public virtual Response<SchemaRegistrySchema> GetSchema(string groupName, string schemaName, int schemaVersion, CancellationToken cancellationToken = default) =>
#pragma warning restore AZC0015 // Unexpected client method return type.
GetSchemaInternalAsync(groupName, schemaName, schemaVersion, false, cancellationToken).EnsureCompleted();
private async Task<Response<SchemaRegistrySchema>> GetSchemaInternalAsync(string groupName, string schemaName, int version, bool async, CancellationToken cancellationToken)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope(GetSchemaScopeName);
scope.Start();
try
{
ResponseWithHeaders<Stream, SchemaGetSchemaVersionHeaders> response;
if (async)
{
response = await RestClient.GetSchemaVersionAsync(groupName, schemaName, version, cancellationToken).ConfigureAwait(false);
}
else
{
response = RestClient.GetSchemaVersion(groupName, schemaName, version, cancellationToken);
}
SchemaFormat format = new SchemaFormat(response.Headers.ContentType.Split('=')[1]);
var properties = new SchemaProperties(format, response.Headers.SchemaId, response.Headers.SchemaGroupName, response.Headers.SchemaName, response.Headers.SchemaVersion!.Value);
var schema = new SchemaRegistrySchema(properties, BinaryData.FromStream(response.Value).ToString());
return Response.FromValue(schema, response);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
private async Task<Response<SchemaRegistrySchema>> GetSchemaInternalAsync(string schemaId, bool async, CancellationToken cancellationToken)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope(GetSchemaScopeName);
scope.Start();
try
{
ResponseWithHeaders<Stream, SchemaGetByIdHeaders> response;
if (async)
{
response = await RestClient.GetByIdAsync(schemaId, cancellationToken).ConfigureAwait(false);
}
else
{
response = RestClient.GetById(schemaId, cancellationToken);
}
SchemaFormat format = new SchemaFormat(response.Headers.ContentType.Split('=')[1]);
var properties = new SchemaProperties(format, response.Headers.SchemaId, response.Headers.SchemaGroupName, response.Headers.SchemaName, response.Headers.SchemaVersion!.Value);
var schema = new SchemaRegistrySchema(properties, BinaryData.FromStream(response.Value).ToString());
return Response.FromValue(schema, response);
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}
}
}