Class: SchemaRegistry::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_registry/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, username = nil, password = nil) ⇒ Client

Returns a new instance of Client.



18
19
20
21
# File 'lib/schema_registry/client.rb', line 18

def initialize(endpoint, username = nil, password = nil)
  @endpoint = URI(endpoint)
  @username, @password = username, password
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



16
17
18
# File 'lib/schema_registry/client.rb', line 16

def endpoint
  @endpoint
end

#passwordObject (readonly)

Returns the value of attribute password.



16
17
18
# File 'lib/schema_registry/client.rb', line 16

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



16
17
18
# File 'lib/schema_registry/client.rb', line 16

def username
  @username
end

Instance Method Details

#default_compatibility_levelObject



36
37
38
# File 'lib/schema_registry/client.rb', line 36

def default_compatibility_level
  request(:get, "/config")["compatibilityLevel"]
end

#default_compatibility_level=(level) ⇒ Object



40
41
42
# File 'lib/schema_registry/client.rb', line 40

def default_compatibility_level=(level)
  request(:put, "/config", compatibility: level)
end

#request(method, path, body = nil) ⇒ Object



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
# File 'lib/schema_registry/client.rb', line 44

def request(method, path, body = nil)
  Net::HTTP.start(endpoint.host, endpoint.port, use_ssl: endpoint.scheme == 'https') do |http|
    request_class = case method
      when :get;    Net::HTTP::Get
      when :post;   Net::HTTP::Post
      when :put;    Net::HTTP::Put
      when :delete; Net::HTTP::Delete
      else raise ArgumentError, "Unsupported request method"
    end

    request = request_class.new(path)
    request.basic_auth(username, password) if username && password
    request['Accept'] = "application/vnd.schemaregistry.v1+json"
    if body
      request['Content-Type'] = "application/json"
      request.body = JSON.dump(body)
    end

    response = http.request(request)
    response_data = JSON.parse(response.body)
    case response
      when Net::HTTPOK; response_data
      else raise SchemaRegistry::ResponseError.new(response_data['error_code'], response_data['message'])
    end
  end
end

#schema(id) ⇒ Object



23
24
25
# File 'lib/schema_registry/client.rb', line 23

def schema(id)
  request(:get, "/schemas/ids/#{id}")['schema']
end

#subject(name) ⇒ Object



32
33
34
# File 'lib/schema_registry/client.rb', line 32

def subject(name)
  SchemaRegistry::Subject.new(self, name)
end

#subjectsObject



27
28
29
30
# File 'lib/schema_registry/client.rb', line 27

def subjects
  data = request(:get, "/subjects")
  data.map { |subject| SchemaRegistry::Subject.new(self, subject) }
end