Class: Raca::HttpClient

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

Overview

A thin wrapper around Net::HTTP. It’s aware of some common details of the rackspace APIs and has an API to match.

You probably don’t want to instantiate this directly, see Raca::Account#http_client

Constant Summary collapse

RETRY_PAUSE =
5

Instance Method Summary collapse

Constructor Details

#initialize(account, hostname, opts = {}) ⇒ HttpClient

Returns a new instance of HttpClient.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
# File 'lib/raca/http_client.rb', line 15

def initialize(, hostname, opts = {})
  @account, @hostname = , hostname.to_s
  raise ArgumentError, "hostname must be plain hostname, leave the protocol out" if @hostname[/\Ahttp/]
  @logger = opts[:logger]
  @logger ||= Rails.logger if defined?(Rails)
end

Instance Method Details

#delete(path, headers = {}) ⇒ Object



36
37
38
39
40
41
# File 'lib/raca/http_client.rb', line 36

def delete(path, headers = {})
  cloud_request(Net::HTTP::Delete.new(path, headers))
rescue Raca::UnauthorizedError
  @account.refresh_cache
  cloud_request(Net::HTTP::Delete.new(path, headers))
end

#get(path, headers = {}, &block) ⇒ Object



22
23
24
25
26
27
# File 'lib/raca/http_client.rb', line 22

def get(path, headers = {}, &block)
  cloud_request(Net::HTTP::Get.new(path, headers), &block)
rescue Raca::UnauthorizedError
  @account.refresh_cache
  cloud_request(Net::HTTP::Get.new(path, headers), &block)
end

#head(path, headers = {}) ⇒ Object



29
30
31
32
33
34
# File 'lib/raca/http_client.rb', line 29

def head(path, headers = {})
  cloud_request(Net::HTTP::Head.new(path, headers))
rescue Raca::UnauthorizedError
  @account.refresh_cache
  cloud_request(Net::HTTP::Head.new(path, headers))
end

#post(path, body, headers = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/raca/http_client.rb', line 58

def post(path, body, headers = {})
  request = Net::HTTP::Post.new(path, headers)
  request.body = body if body
  cloud_request(request)
rescue Raca::UnauthorizedError
  @account.refresh_cache
  request = Net::HTTP::Post.new(path, headers)
  request.body = body if body
  cloud_request(request)
end

#put(path, headers = {}) ⇒ Object



43
44
45
46
47
48
# File 'lib/raca/http_client.rb', line 43

def put(path, headers = {})
  cloud_request(Net::HTTP::Put.new(path, headers))
rescue Raca::UnauthorizedError
  @account.refresh_cache
  cloud_request(Net::HTTP::Put.new(path, headers))
end

#streaming_put(path, io, byte_count, headers = {}) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/raca/http_client.rb', line 50

def streaming_put(path, io, byte_count, headers = {})
  cloud_request(build_streaming_put_request(path, io, byte_count, headers))
rescue Raca::UnauthorizedError
  @account.refresh_cache
  io.rewind if io.respond_to?(:rewind)
  cloud_request(build_streaming_put_request(path, io, byte_count, headers))
end