Class: AvroTurf::ConfluentSchemaRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/avro_turf/confluent_schema_registry.rb

Constant Summary collapse

CONTENT_TYPE =
"application/vnd.schemaregistry.v1+json".freeze

Instance Method Summary collapse

Constructor Details

#initialize(url, logger: Logger.new($stdout), proxy: nil, client_cert: nil, client_key: nil, client_key_pass: nil, client_cert_data: nil, client_key_data: nil) ⇒ ConfluentSchemaRegistry

Returns a new instance of ConfluentSchemaRegistry.



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
# File 'lib/avro_turf/confluent_schema_registry.rb', line 6

def initialize(
  url,
  logger: Logger.new($stdout),
  proxy: nil,
  client_cert: nil,
  client_key: nil,
  client_key_pass: nil,
  client_cert_data: nil,
  client_key_data: nil
)
  @logger = logger
  headers = {
    "Content-Type" => CONTENT_TYPE
  }
  headers[:proxy] = proxy if proxy&.present?
  @connection = Excon.new(
    url,
    headers: headers,
    client_cert: client_cert,
    client_key: client_key,
    client_key_pass: client_key_pass,
    client_cert_data: client_cert_data,
    client_key_data: client_key_data
  )
end

Instance Method Details

#check(subject, schema) ⇒ Object

Check if a schema exists. Returns nil if not found.



66
67
68
69
70
71
# File 'lib/avro_turf/confluent_schema_registry.rb', line 66

def check(subject, schema)
  data = post("/subjects/#{subject}",
              expects: [200, 404],
              body: { schema: schema.to_s }.to_json)
  data unless data.has_key?("error_code")
end

#compatible?(subject, schema, version = 'latest') ⇒ Boolean

Check if a schema is compatible with the stored version. Returns:

  • true if compatible

  • nil if the subject or version does not exist

  • false if incompatible

docs.confluent.io/3.1.2/schema-registry/docs/api.html#compatibility

Returns:

  • (Boolean)


79
80
81
82
83
84
# File 'lib/avro_turf/confluent_schema_registry.rb', line 79

def compatible?(subject, schema, version = 'latest')
  data = post("/compatibility/subjects/#{subject}/versions/#{version}",
              expects: [200, 404],
              body: { schema: schema.to_s }.to_json)
  data.fetch('is_compatible', false) unless data.has_key?('error_code')
end

#fetch(id) ⇒ Object



32
33
34
35
36
# File 'lib/avro_turf/confluent_schema_registry.rb', line 32

def fetch(id)
  @logger.info "Fetching schema with id #{id}"
  data = get("/schemas/ids/#{id}")
  data.fetch("schema")
end

#global_configObject

Get global config



87
88
89
# File 'lib/avro_turf/confluent_schema_registry.rb', line 87

def global_config
  get("/config")
end

#register(subject, schema) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/avro_turf/confluent_schema_registry.rb', line 38

def register(subject, schema)
  data = post("/subjects/#{subject}/versions", body: {
    schema: schema.to_s
  }.to_json)

  id = data.fetch("id")

  @logger.info "Registered schema for subject `#{subject}`; id = #{id}"

  id
end

#subject_config(subject) ⇒ Object

Get config for subject



97
98
99
# File 'lib/avro_turf/confluent_schema_registry.rb', line 97

def subject_config(subject)
  get("/config/#{subject}")
end

#subject_version(subject, version = 'latest') ⇒ Object

Get a specific version for a subject



61
62
63
# File 'lib/avro_turf/confluent_schema_registry.rb', line 61

def subject_version(subject, version = 'latest')
  get("/subjects/#{subject}/versions/#{version}")
end

#subject_versions(subject) ⇒ Object

List all versions for a subject



56
57
58
# File 'lib/avro_turf/confluent_schema_registry.rb', line 56

def subject_versions(subject)
  get("/subjects/#{subject}/versions")
end

#subjectsObject

List all subjects



51
52
53
# File 'lib/avro_turf/confluent_schema_registry.rb', line 51

def subjects
  get('/subjects')
end

#update_global_config(config) ⇒ Object

Update global config



92
93
94
# File 'lib/avro_turf/confluent_schema_registry.rb', line 92

def update_global_config(config)
  put("/config", { body: config.to_json })
end

#update_subject_config(subject, config) ⇒ Object

Update config for subject



102
103
104
# File 'lib/avro_turf/confluent_schema_registry.rb', line 102

def update_subject_config(subject, config)
  put("/config/#{subject}", { body: config.to_json })
end