Class: ApiClient

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

Constant Summary collapse

VENDOR_HOST =
"api.replicated.com"
VERB_MAP =
{
	:get	 => Net::HTTP::Get,
	:post	=> Net::HTTP::Post,
	:put	 => Net::HTTP::Put,
	:delete => Net::HTTP::Delete
}

Instance Method Summary collapse

Constructor Details

#initializeApiClient

Returns a new instance of ApiClient.



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

def initialize
	# Create persistent HTTP connection
	@http = Net::HTTP.new(VENDOR_HOST, URI::HTTPS::DEFAULT_PORT)
	@http.use_ssl = true
end

Instance Method Details

#request(method, uri, params) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/client/client.rb', line 34

def request(method, uri, params)
	method_sym = method.downcase.to_sym

	request = VERB_MAP[method_sym].new(uri)

	unless method_sym == :get
		request.set_form_data(params)
	end

	if @api_token
		request['Authorization'] = @api_token
	end

	@http.request(request)
end

#request_json(method, uri, params = nil) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/client/client.rb', line 25

def request_json(method, uri, params = nil)
	response = request(method, uri, params)
	body = JSON.parse(response.body)

	OpenStruct.new(:code => response.code, :body => body)
rescue JSON::ParserError
	response
end

#set_token(api_token) ⇒ Object



21
22
23
# File 'lib/client/client.rb', line 21

def set_token(api_token)
	@api_token = api_token
end