This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 230
/
csrc.go
133 lines (119 loc) · 3.62 KB
/
csrc.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
package csrc
import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
"github.com/featurebasedb/featurebase/v3/idk"
"github.com/pkg/errors"
)
type Client struct {
URL string
httpClient *http.Client
Auth *BasicAuth
}
type BasicAuth struct {
KafkaSchemaApiKey string
KafkaSchemaApiSecret string
}
func NewClient(url string, tlsConfig *tls.Config, authConfig *BasicAuth) *Client {
if !strings.HasPrefix(url, "http") {
url = "http://" + url
}
c := http.DefaultClient
if strings.HasPrefix(url, "https://") {
log.Printf("getting http client with tls config: %v", tlsConfig)
c = idk.GetHTTPClient(tlsConfig)
}
return &Client{
URL: url,
httpClient: c,
Auth: authConfig,
}
}
// GetSchema gets the schema with the ID.
// https://docs.confluent.io/current/schema-registry/develop/api.html#get--schemas-ids-int-%20id
func (c *Client) GetSchema(id int) (string, error) {
// schemaUrlResponse, err := s.httpClient.Get(schemaUrl)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/schemas/ids/%d", c.URL, id), http.NoBody)
if err != nil {
return "", errors.Wrap(err, "building request for getting schema from registry")
}
if c.Auth != nil {
req.SetBasicAuth(c.Auth.KafkaSchemaApiKey, c.Auth.KafkaSchemaApiSecret)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return "", errors.Wrap(err, "getting schema from registry")
}
defer resp.Body.Close()
sr := SchemaResponse{}
err = unmarshalRespErr(resp, err, &sr)
if err != nil {
return "", errors.Wrap(err, "making http request")
}
return sr.Schema, nil
}
type SchemaResponse struct {
Schema string `json:"schema"` // The actual AVRO schema
Subject string `json:"subject"` // Subject where the schema is registered for
Version int `json:"version"` // Version within this subject
ID int `json:"id"` // Registry's unique id
}
type ErrorResponse struct { //nolint:errname
StatusCode int `json:"error_code"`
Body string `json:"message"`
}
func (e *ErrorResponse) Error() string {
return fmt.Sprintf("status %d: %s", e.StatusCode, e.Body)
}
func (c *Client) PostSubjects(subj, schema string) (*SchemaResponse, error) {
schema = strings.Replace(schema, "\t", "", -1)
schema = strings.Replace(schema, "\n", `\n`, -1)
schema = fmt.Sprintf(`{"schema": "%s"}`, strings.Replace(schema, `"`, `\"`, -1)) // this is probably terrible
// schemaUrlResponse, err := s.httpClient.Get(schemaUrl)
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/subjects/%s/versions", c.URL, subj), strings.NewReader(schema))
if err != nil {
return nil, errors.Wrap(err, "building request to post subjects")
}
if c.Auth != nil {
req.SetBasicAuth(c.Auth.KafkaSchemaApiKey, c.Auth.KafkaSchemaApiSecret)
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, errors.Wrap(err, "posting subjects")
}
defer resp.Body.Close()
sr := &SchemaResponse{}
err = unmarshalRespErr(resp, err, sr)
if err != nil {
return nil, errors.Wrapf(err, "unmarshaling resp to %s", fmt.Sprintf("%s/subjects/%s/versions", c.URL, subj))
}
return sr, nil
}
func unmarshalRespErr(resp *http.Response, err error, into interface{}) error {
if err != nil {
return errors.Wrap(err, "making http request")
}
if resp.StatusCode != 200 {
bod, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "reading body")
}
errResp := &ErrorResponse{
StatusCode: resp.StatusCode,
Body: string(bod),
}
return errResp
}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(into)
if err != nil {
return errors.Wrap(err, "unmarshaling body")
}
return nil
}