-
Notifications
You must be signed in to change notification settings - Fork 2
/
schema_registry.go
179 lines (149 loc) · 4.39 KB
/
schema_registry.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
package avrostry
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
// SchemaRegistryClient Interface for manage Schema Registry
type SchemaRegistryClient interface {
Register(subject, schema string) (id int32, err error)
GetByID(id int32) (schema string, err error)
}
// SchemaMetadata Metainformation about Schemas
type SchemaMetadata struct {
ID int32
Version int32
Schema string
}
// CompatibilityLevel Schema Registry compatibility level
type CompatibilityLevel string
const (
BackwardCompatibilityLevel CompatibilityLevel = "BACKWARD"
ForwardCompatibilityLevel CompatibilityLevel = "FORWARD"
FullCompatibilityLevel CompatibilityLevel = "FULL"
NoneCompatibilityLevel CompatibilityLevel = "NONE"
)
const (
GetSchemaByID = "/schemas/ids/%d"
GetSubjects = "/subjects"
GetSubjectVersions = "/subjects/%s/versions"
GetSpecificSubjectVersion = "/subjects/%s/versions/%s"
RegisterNewSchema = "/subjects/%s/versions"
CheckIsRegistered = "/subjects/%s"
TestCompatibility = "/compatibility/subjects/%s/versions/%s"
Config = "/config"
)
type ErrorMessage struct {
Code int32 `json:"error_code"`
Message string `json:"message"`
}
func (err *ErrorMessage) Error() string {
return fmt.Sprintf("%d: %s ", err.Code, err.Message)
}
// RegisterSchemaResponse ID Schema response
type RegisterSchemaResponse struct {
ID int32 `json:"id"`
}
// GetSchemaResponse Schema string response
type GetSchemaResponse struct {
Schema string `json:"schema"`
}
type HttpDoer interface {
Do(*http.Request) (*http.Response, error)
}
// SchemaRegistryManager Client and cache schema registry
type SchemaRegistryManager struct {
registryURL string
cache *CacheSchemaRegistry
httpDoer HttpDoer
}
// NewSchemaRegistryManager SchemaRegistryManager Constructor
func NewSchemaRegistryManager(registryURL string, cache *CacheSchemaRegistry, httpDoer HttpDoer) *SchemaRegistryManager {
return &SchemaRegistryManager{
registryURL: strings.TrimRight(registryURL, "/"),
cache: cache,
httpDoer: httpDoer,
}
}
// Register set a subject schema in Schema Registry if there is not in cache.
func (srm *SchemaRegistryManager) Register(subject string, schema string) (int32, error) {
id, exists := srm.cache.GetIDBySubjectAndSquema(subject, schema)
if exists {
return id, nil
}
request, err := srm.newDefaultRequest("POST",
fmt.Sprintf(RegisterNewSchema, subject),
strings.NewReader(fmt.Sprintf(`{"schema": %s}`, strconv.Quote(schema))))
response, err := srm.httpDoer.Do(request)
if err != nil {
return 0, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return 0, err
}
if !isOK(response.StatusCode) {
return 0, newError(body)
}
var decodedResponse RegisterSchemaResponse
err = json.Unmarshal(body, &decodedResponse)
if err != nil {
return 0, err
}
srm.cache.SetBySubjectSquema(subject, schema, decodedResponse.ID)
return decodedResponse.ID, err
}
// GetByID given an id, retrieve the related Schema from Kafka Schema Registry
func (srm *SchemaRegistryManager) GetByID(id int32) (string, error) {
schema, exists := srm.cache.GetByID(id)
if exists {
return schema, nil
}
request, err := srm.newDefaultRequest("GET", fmt.Sprintf(GetSchemaByID, id), nil)
if err != nil {
return "", err
}
response, err := srm.httpDoer.Do(request)
if err != nil {
return "", err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
if !isOK(response.StatusCode) {
return "", newError(body)
}
var decodedResponse GetSchemaResponse
err = json.Unmarshal(body, &decodedResponse)
if err != nil {
return "", err
}
srm.cache.SetSchemaByID(id, decodedResponse.Schema)
return decodedResponse.Schema, err
}
func (srm *SchemaRegistryManager) newDefaultRequest(method string, uri string, reader io.Reader) (*http.Request, error) {
request, err := http.NewRequest(method, srm.registryURL+uri, reader)
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", "application/vnd.schemaregistry.v1+json")
return request, nil
}
func isOK(statusCode int) bool {
return statusCode >= 200 && statusCode < 300
}
func newError(respBody []byte) error {
var registryError ErrorMessage
err := json.Unmarshal(respBody, ®istryError)
if err != nil {
return err
}
return ®istryError
}