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.



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

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.



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

def endpoint
  @endpoint
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#default_compatibility_levelObject



46
47
48
# File 'lib/schema_registry/client.rb', line 46

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

#default_compatibility_level=(level) ⇒ Object



50
51
52
# File 'lib/schema_registry/client.rb', line 50

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

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



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

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::HTTPSuccess;
      response_data
    else
      error_class = RESPONSE_ERROR_CODES[response_data['error_code']] || SchemaRegistry::ResponseError
      raise error_class.new(response_data['error_code'], response_data['message'])
    end
  end
end

#schema(id) ⇒ Object



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

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

#subject(name) ⇒ Object



42
43
44
# File 'lib/schema_registry/client.rb', line 42

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

#subjectsObject



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

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