This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
404 lines (334 loc) · 12.1 KB
/
client.go
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package schemaregistry
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
// Option function used to apply modifications to the client.
type Option func(*Client)
// Client used to interact with the registry schema REST API.
type Client struct {
baseURL *url.URL
client *http.Client
username string
password string
}
// Schema describes a schema, look `GetSchema` for more.
type Schema struct {
// Schema is the Avro schema string.
Schema string `json:"schema"`
// Subject where the schema is registered for.
Subject string `json:"subject"`
// Version of the returned schema.
Version int `json:"version"`
ID int `json:"id,omitempty"`
}
// Config describes a subject or globa schema-registry configuration
type Config struct {
// Compatibility mode of subject or global
Compatibility string `json:"compatibility"`
}
// UsingClient modifies the underline HTTP Client that schema registry is using for contact with the backend server.
func UsingClient(httpClient *http.Client) Option {
return func(c *Client) {
c.client = httpClient
}
}
func WithBasicAuth(user string, password string) Option {
return func(c *Client) {
c.username = user
c.password = password
}
}
// NewClient instantiate a new Client.
func NewClient(baseURL string, options ...Option) (*Client, error) {
url, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
client := &Client{
baseURL: url,
client: http.DefaultClient,
}
for _, opt := range options {
opt(client)
}
return client, nil
}
// GetSchemaByID returns the Avro schema string identified by the id.
//
// https://docs.confluent.io/current/schema-registry/docs/api.html#get--schemas-ids-int-%20id
func (c *Client) GetSchemaByID(ctx context.Context, subjectID int) (string, error) {
type responseBody struct {
Schema string `json:"schema"`
}
rawBody, err := c.execRequest(ctx, "GET", fmt.Sprintf("schemas/ids/%d", subjectID), nil)
if err != nil {
return "", err
}
var resBody responseBody
err = json.Unmarshal(rawBody, &resBody)
if err != nil {
return "", fmt.Errorf("failed to decode the response: %s", err)
}
return resBody.Schema, nil
}
// Subjects returns a list of the available subjects(schemas).
//
// https://docs.confluent.io/current/schema-registry/docs/api.html#subjects
func (c *Client) Subjects(ctx context.Context) (subjects []string, err error) {
type responseBody []string
rawBody, err := c.execRequest(ctx, "GET", "subjects", nil)
if err != nil {
return nil, err
}
var resBody responseBody
err = json.Unmarshal(rawBody, &resBody)
if err != nil {
return nil, fmt.Errorf("failed to decode the response: %s", err)
}
return resBody, nil
}
// Versions returns all schema version numbers registered for this subject.
//
// https://docs.confluent.io/current/schema-registry/docs/api.html#get--subjects-(string-%20subject)-versions
func (c *Client) Versions(ctx context.Context, subject string) (versions []int, err error) {
type responseBody []int
rawBody, err := c.execRequest(ctx, "GET", fmt.Sprintf("subjects/%s/versions", subject), nil)
if err != nil {
return nil, err
}
var resBody responseBody
err = json.Unmarshal(rawBody, &resBody)
if err != nil {
return nil, fmt.Errorf("failed to decode the response: %s", err)
}
return resBody, nil
}
// DeleteSubject deletes the specified subject and its associated compatibility level if registered.
// It is recommended to use this API only when a topic needs to be recycled or in development environment.
// Returns the versions of the schema deleted under this subject.
//
// https://docs.confluent.io/current/schema-registry/docs/api.html#delete--subjects-(string-%20subject)
func (c *Client) DeleteSubject(ctx context.Context, subject string, permanent bool) (versions []int, err error) {
type responseBody []int
rawBody, err := c.execRequest(ctx, "DELETE", fmt.Sprintf("subjects/%s?permanent=%v", subject, permanent), nil)
if err != nil {
return nil, err
}
var resBody responseBody
err = json.Unmarshal(rawBody, &resBody)
if err != nil {
return nil, fmt.Errorf("failed to decode the response: %s", err)
}
return resBody, nil
}
// IsRegistered tells if the given "schema" is registered for this "subject".
//
// https://docs.confluent.io/current/schema-registry/docs/api.html#post--subjects-(string-%20subject)
func (c *Client) IsRegistered(ctx context.Context, subject string, schema string) (bool, *Schema, error) {
type requestBody struct {
Schema string `json:"schema"`
}
// nolint
// Error not possible here.
reqBody, _ := json.Marshal(&requestBody{Schema: schema})
rawBody, err := c.execRequest(ctx, "POST", fmt.Sprintf("subjects/%s", subject), bytes.NewReader(reqBody))
if IsSchemaNotFound(err) || IsSchemaNotFound(err) {
return false, nil, nil
}
if err != nil {
return false, nil, err
}
var resBody Schema
err = json.Unmarshal(rawBody, &resBody)
if err != nil {
return false, nil, fmt.Errorf("failed to decode the response: %s", err)
}
return true, &resBody, nil
}
// RegisterNewSchema registers a schema.
// The returned identifier should be used to retrieve this schema from the
// schemas resource and is different from the schema’s version which is
// associated with that name.
//
// https://docs.confluent.io/current/schema-registry/docs/api.html#post--subjects-(string-%20subject)-versions
func (c *Client) RegisterNewSchema(ctx context.Context, subject string, avroSchema string) (int, error) {
type requestBody struct {
Schema string `json:"schema"`
}
type responseBody struct {
ID int `json:"id"`
}
// nolint
// Error not possible here.
reqBody, _ := json.Marshal(&requestBody{Schema: avroSchema})
rawBody, err := c.execRequest(ctx, "POST", fmt.Sprintf("subjects/%s/versions", subject), bytes.NewReader(reqBody))
if err != nil {
return -1, err
}
var resBody responseBody
err = json.Unmarshal(rawBody, &resBody)
if err != nil {
return -1, fmt.Errorf("failed to decode the response: %s", err)
}
return resBody.ID, nil
}
func (c *Client) getSchemaBySubjectAndVersion(ctx context.Context, subject string, version string) (*Schema, error) {
rawBody, err := c.execRequest(ctx, "GET", fmt.Sprintf("subjects/%s/versions/%s", subject, version), nil)
if err != nil {
return nil, err
}
var schema Schema
err = json.Unmarshal(rawBody, &schema)
if err != nil {
return nil, fmt.Errorf("failed to decode the response: %s", err)
}
return &schema, nil
}
// GetSchemaBySubjectAndVersion returns the schema for a particular subject and version.
//
// https://docs.confluent.io/current/schema-registry/docs/api.html#get--subjects-(string-%20subject)-versions-(versionId-%20version)
func (c *Client) GetSchemaBySubjectAndVersion(ctx context.Context, subject string, version int) (*Schema, error) {
return c.getSchemaBySubjectAndVersion(ctx, subject, strconv.Itoa(version))
}
// GetLatestSchema returns the latest version of a schema.
// See `GetSchemaAtVersion` to retrieve a subject schema by a specific version.
func (c *Client) GetLatestSchema(ctx context.Context, subject string) (*Schema, error) {
return c.getSchemaBySubjectAndVersion(ctx, subject, "latest")
}
// GetConfig returns the configuration (Config type) for global Schema-Registry or a specific
// subject. When Config returned has "compatibilityLevel" empty, it's using global settings.
//
// https://docs.confluent.io/current/schema-registry/docs/api.html#get--config-(string-%20subject)
func (c *Client) GetConfig(ctx context.Context, subject string) (*Config, error) {
rawBody, err := c.execRequest(ctx, "GET", fmt.Sprintf("config/%s", subject), nil)
if err != nil {
return nil, err
}
var config Config
err = json.Unmarshal(rawBody, &config)
if err != nil {
return nil, fmt.Errorf("failed to decode the response: %s", err)
}
return &config, nil
}
func (c *Client) SetGlobalConfig(ctx context.Context, config Config) (*Config, error) {
// nolint
// Error not possible here.
reqBody, _ := json.Marshal(&config)
rawBody, err := c.execRequest(ctx, "PUT", "config", bytes.NewReader(reqBody))
if err != nil {
return nil, err
}
var newConfig Config
err = json.Unmarshal(rawBody, &newConfig)
if err != nil {
return nil, fmt.Errorf("failed to decode the response: %s", err)
}
return &newConfig, nil
}
func (c *Client) deleteSchemaVersion(ctx context.Context, subject string, version string, permanent bool) (int, error) {
rawBody, err := c.execRequest(ctx, "DELETE", fmt.Sprintf("subjects/%s/versions/%s?permanent=%v", subject, version, permanent), nil)
if err != nil {
return -1, err
}
var id int
err = json.Unmarshal(rawBody, &id)
if err != nil {
return -1, fmt.Errorf("failed to decode the response: %s", err)
}
return id, nil
}
// DeleteSchemaVersion deletes a specific version of the schema registered
//
// under this subject.
//
// This only deletes the version and the schema ID remains intact making it still
// possible to decode data using the schema ID. This API is recommended to be
// used only in development environments or under extreme circumstances where-in,
// its required to delete a previously registered schema for compatibility
// purposes or re-register previously registered schema.
//
// https://docs.confluent.io/current/schema-registry/docs/api.html#delete--subjects-(string-%20subject)-versions-(versionId-%20version)
func (c *Client) DeleteSchemaVersion(ctx context.Context, subject string, version int, permanent bool) (int, error) {
return c.deleteSchemaVersion(ctx, subject, strconv.Itoa(version), permanent)
}
// DeleteLatestSchemaVersion remove the latest version of a schema.
//
// See `DeleteLatestSchemaVersion` to retrieve a subject schema by a specific version.
func (c *Client) DeleteLatestSchemaVersion(ctx context.Context, subject string, permanent bool) (int, error) {
return c.deleteSchemaVersion(ctx, subject, "latest", permanent)
}
// SchemaCompatibleWith test input schema against a particular version of a subject's
// schema for compatibility.
//
// Note that the compatibility level applied for the check is the configured
// compatibility level for the subject (http:get:: /config/(string: subject)).
// If this subject's compatibility level was never changed, then the global
// compatibility level applies (http:get:: /config).
//
// https://docs.confluent.io/current/schema-registry/docs/api.html#post--compatibility-subjects-(string-%20subject)-versions-(versionId-%20version)
func (c *Client) SchemaCompatibleWith(ctx context.Context, schema string, subject string, version int) (bool, error) {
type requestBody struct {
Schema string `json:"schema"`
}
type responseBody struct {
IsCompatible bool `json:"is_compatible"`
}
// nolint
// Error not possible here.
reqBody, _ := json.Marshal(&requestBody{Schema: schema})
rawBody, err := c.execRequest(ctx, "POST", fmt.Sprintf("compatibility/subjects/%s/versions/%d", subject, version), bytes.NewReader(reqBody))
if err != nil {
return false, err
}
var resBody responseBody
err = json.Unmarshal(rawBody, &resBody)
if err != nil {
return false, fmt.Errorf("failed to decode the response: %s", err)
}
return resBody.IsCompatible, nil
}
// Execute the request and check for an error into the response.
//
// In case of succes it return the raw body.
//
// It return an error if:
// - an error occure with the network
// - an error occure with the remote api
// - the request the params have an invalid
// - the response have an invalid format
// - the response is an error
func (c *Client) execRequest(ctx context.Context, method string, rawPath string, body io.Reader) ([]byte, error) {
path, err := url.Parse(rawPath)
if err != nil {
return nil, err
}
// nolint
// The request is always valid
req, _ := http.NewRequest(method, c.baseURL.ResolveReference(path).String(), body)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/vnd.schemaregistry.v1+json, application/vnd.schemaregistry+json, application/json")
req.SetBasicAuth(c.username, c.password)
res, err := c.client.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
defer res.Body.Close()
err = parseResponseError(req, res)
if err != nil {
return nil, err
}
rawBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return rawBody, nil
}