-
Notifications
You must be signed in to change notification settings - Fork 5
/
schema_registry_client.clj
147 lines (132 loc) · 4.65 KB
/
schema_registry_client.clj
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
(ns kafka-avro-confluent.v2.schema-registry-client
(:require [abracad.avro :as avro]
[cheshire.core :as json]
[clj-http.client :as http]
[clojure.core.memoize :refer [memo]]
[clojure.pprint :as pprint]
[clojure.spec.alpha :as s]
[clojure.tools.logging :as log])
(:import clojure.lang.ExceptionInfo))
(require 'kafka-avro-confluent.v2.specs)
(defn- schema->json
[schema]
(let [schema-str (if (avro/schema? schema)
(str schema)
(json/generate-string schema))]
(json/generate-string {"schema" schema-str})))
(defn- pretty
[x]
(with-out-str (pprint/pprint x)))
(defn- -get-config
[{:schema-registry/keys [base-url username password]
:as _config}]
(-> (http/get
(str base-url "/config")
{:basic-auth [username password]
:as :json
:conn-timeout 1000})
:body))
(defn -healthy?
[config]
(try
(contains? (-get-config config)
:compatibilityLevel)
(catch Exception e
false)))
(defn -list-subjects
[{:schema-registry/keys [base-url username password]
:as _config}]
(-> (http/get
(str base-url "/subjects")
{:basic-auth [username password]
:as :json
:conn-timeout 1000})
:body))
(defn- -post-schema
[{:schema-registry/keys [base-url username password]
:as _config}
subject
schema]
;; TODO better url creation
(let [url (str base-url
"/subjects/"
subject
"/versions")
body (schema->json schema)]
(try
(-> (http/post url
{:basic-auth [username password]
:content-type "application/vnd.schemaregistry.v1+json"
:as :json
:body body})
(get-in [:body :id]))
(catch Exception ex
(let [exi (ex-info "Post to schema registry failed!"
{:post-url url
:schema schema}
ex)]
(log/error ex
"Post to schema registry failed!"
(pretty (ex-data exi)))
(throw exi))))))
(defn- -get-schema-by-id
[{:schema-registry/keys [base-url username password]
:as _config}
id]
(let [url (str base-url "/schemas/ids/" id)
resp (http/get url
{:as :json
:basic-auth [username password]})]
(-> resp
:body
:schema
(json/parse-string true))))
(defn- -get-latest-schema-by-subject
[{:schema-registry/keys [base-url username password]
:as _config}
subject]
(let [url (str base-url
"/subjects/"
subject
"/versions/latest")
resp (http/get url
{:as :json
:basic-auth [username password]})]
(-> resp
:body
:schema
(json/parse-string true))))
(defprotocol SchemaRegistry
"A Confluent Schema Registry client."
(get-config [this] "Returns the Schema Registry configuration map.")
(healthy? [this] "Can the Schema Registry be contacted, and do its responses look right?")
(list-subjects [this] "List all subjects.")
(post-schema [this subject schema] "Posts an Avro Schema. Return the Schema Id.")
(get-schema-by-id [this id] "Fetches a Schema by Schema Id.")
(get-latest-schema-by-subject [this subject] "Gets the latest Schema for a given subject."))
(defrecord SchemaRegistryImpl [memoized-fns config]
SchemaRegistry
(healthy? [this] (-healthy? config))
(get-config [_] (-get-config config))
(list-subjects [_] (-list-subjects config))
(post-schema
[_ subject schema]
((:post-schema memoized-fns) config subject schema))
(get-latest-schema-by-subject
[_ subject]
((:get-latest-schema-by-subject memoized-fns) config subject))
(get-schema-by-id
[_ id]
((:get-schema-by-id memoized-fns) config id)))
(s/fdef ->schema-registry-client
:args (s/cat :config :kafka.serde/config))
(defn ->schema-registry-client
"Returns an instance of the schema-registry-client"
(^kafka_avro_confluent.schema_registry_client.SchemaRegistry
[config]
(s/assert :kafka.serde/config config)
(let [memoized-fns
{:post-schema (memo -post-schema)
:get-schema-by-id (memo -get-schema-by-id)
:get-latest-schema-by-subject (memo -get-latest-schema-by-subject)}]
(->SchemaRegistryImpl memoized-fns config))))