Class: Bazil::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/bazil/client.rb

Defined Under Namespace

Classes: Options

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



124
125
126
127
128
129
130
131
# File 'lib/bazil/client.rb', line 124

def initialize(options={})
  opt = Options.new(options)
  http = Net::HTTP.new(opt.host, opt.port)
  set_ssl_options(http,opt)

  @http_cli = REST.new(http, api_root: opt.api_root)
  set_api_keys(opt.api_key, opt.secret_key)
end

Instance Method Details

#api_versionObject

TODO: make this changable



189
190
191
# File 'lib/bazil/client.rb', line 189

def api_version
  'v2'
end

#create_config(model_id, config) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/bazil/client.rb', line 164

def create_config(model_id, config)
  data = config.to_json
  res, body = @http_cli.post(gen_uri("models/#{model_id}/configs"), data, {'Content-Type' => 'application/json; charset=UTF-8', 'Content-Length' => data.length.to_s})
  raise_error("Failed to create new configuration", res) unless res.code =~ /2[0-9][0-9]/ # TODO: return detailed error information
  js = JSON.parse(res.body)
  Model.new(self, model_id, js['config_id'].to_i)
end

#create_model(config) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/bazil/client.rb', line 150

def create_model(config)
  data = config.to_json
  res, body = @http_cli.post(gen_uri('models'), data, {'Content-Type' => 'application/json; charset=UTF-8', 'Content-Length' => data.length.to_s})
  raise_error("Failed to create model", res) unless res.code =~ /2[0-9][0-9]/ # TODO: return detailed error information
  js = JSON.parse(res.body)
  Model.new(self, js['model_id'].to_i, js['config_id'].to_i)
end

#delete_config(model_id, config_id) ⇒ Object



172
173
174
175
176
# File 'lib/bazil/client.rb', line 172

def delete_config(model_id, config_id)
  res, body = @http_cli.delete(gen_uri("models/#{model_id}/configs/#{config_id}"))
  raise_error("Failed to delete configuration", res) unless res.code =~ /2[0-9][0-9]/ # TODO: return detailed error information
  JSON.parse(res.body)
end

#delete_model(model_id) ⇒ Object



158
159
160
161
162
# File 'lib/bazil/client.rb', line 158

def delete_model(model_id)
  res, body = @http_cli.delete(gen_uri("models/#{model_id}"))
  raise_error("Failed to delete model", res) unless res.code =~ /2[0-9][0-9]/ # TODO: return detailed error information
  JSON.parse(res.body)
end

#http_clientObject



184
185
186
# File 'lib/bazil/client.rb', line 184

def http_client
  @http_cli
end

#model(model_id, config_id) ⇒ Object



178
179
180
181
182
# File 'lib/bazil/client.rb', line 178

def model(model_id, config_id)
  model = Model.new(self, model_id, config_id)
  model.status
  model
end

#models(options = {}) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/bazil/client.rb', line 135

def models(options = {})
  queries = {}
  queries[:tag_id] = options[:tag_id].to_i if options.has_key? :tag_id
  queries[:page] = options[:page].to_i if options.has_key? :page
  queries[:per_page] = options[:per_page].to_i if options.has_key? :per_page

  res, body = @http_cli.get(gen_uri("models",queries))
  raise_error("Failed to get models", res) unless res.code =~ /2[0-9][0-9]/
  JSON.parse(res.body)["models"].map{|model|
    model["config_ids"].map{|config_id|
      Model.new(self, model["id"].to_i, config_id.to_i)
    }
  }.flatten
end

#set_ssl_options(http, options) ⇒ Object



117
118
119
120
121
122
# File 'lib/bazil/client.rb', line 117

def set_ssl_options(http, options)
  http.use_ssl = options.scheme == 'https'
  http.ca_file = options.ca_file
  http.ssl_version = options.ssl_version
  http.verify_mode = options.verify_mode
end