class Raca::HttpClient

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

Public Class Methods

new(account, hostname, opts = {}) click to toggle source
   # File lib/raca/http_client.rb
14 def initialize(account, hostname, opts = {})
15   @account, @hostname = account, hostname.to_s
16   raise ArgumentError, "hostname must be plain hostname, leave the protocol out" if @hostname[/\Ahttp/]
17   @logger = opts[:logger]
18   @logger ||= Rails.logger if defined?(Rails)
19 end

Public Instance Methods

delete(path, headers = {}) click to toggle source
   # File lib/raca/http_client.rb
35 def delete(path, headers = {})
36   cloud_request(Net::HTTP::Delete.new(path, headers))
37 rescue Raca::UnauthorizedError
38   @account.refresh_cache
39   cloud_request(Net::HTTP::Delete.new(path, headers))
40 end
get(path, headers = {}, &block) click to toggle source
   # File lib/raca/http_client.rb
21 def get(path, headers = {}, &block)
22   cloud_request(Net::HTTP::Get.new(path, headers), &block)
23 rescue Raca::UnauthorizedError
24   @account.refresh_cache
25   cloud_request(Net::HTTP::Get.new(path, headers), &block)
26 end
head(path, headers = {}) click to toggle source
   # File lib/raca/http_client.rb
28 def head(path, headers = {})
29   cloud_request(Net::HTTP::Head.new(path, headers))
30 rescue Raca::UnauthorizedError
31   @account.refresh_cache
32   cloud_request(Net::HTTP::Head.new(path, headers))
33 end
inspect() click to toggle source
   # File lib/raca/http_client.rb
68 def inspect
69   "#<Raca::HttpClient:#{__id__}>"
70 end
post(path, body, headers = {}) click to toggle source
   # File lib/raca/http_client.rb
57 def post(path, body, headers = {})
58   request = Net::HTTP::Post.new(path, headers)
59   request.body = body if body
60   cloud_request(request)
61 rescue Raca::UnauthorizedError
62   @account.refresh_cache
63   request = Net::HTTP::Post.new(path, headers)
64   request.body = body if body
65   cloud_request(request)
66 end
put(path, headers = {}) click to toggle source
   # File lib/raca/http_client.rb
42 def put(path, headers = {})
43   cloud_request(Net::HTTP::Put.new(path, headers))
44 rescue Raca::UnauthorizedError
45   @account.refresh_cache
46   cloud_request(Net::HTTP::Put.new(path, headers))
47 end
streaming_put(path, io, byte_count, headers = {}) click to toggle source
   # File lib/raca/http_client.rb
49 def streaming_put(path, io, byte_count, headers = {})
50   cloud_request(build_streaming_put_request(path, io, byte_count, headers))
51 rescue Raca::UnauthorizedError
52   @account.refresh_cache
53   io.rewind if io.respond_to?(:rewind)
54   cloud_request(build_streaming_put_request(path, io, byte_count, headers))
55 end

Private Instance Methods

build_streaming_put_request(path, io, byte_count, headers) click to toggle source
   # File lib/raca/http_client.rb
74 def build_streaming_put_request(path, io, byte_count, headers)
75   request = Net::HTTP::Put.new(path, headers)
76   request.body_stream = io
77   request.content_length = byte_count
78   request
79 end
cloud_http(&block) click to toggle source
    # File lib/raca/http_client.rb
103 def cloud_http(&block)
104   Net::HTTP.start(@hostname, 443, use_ssl: true, read_timeout: 70) do |http|
105     response = block.call(http)
106     if response.is_a?(Net::HTTPSuccess)
107       response
108     else
109       raise_on_error(response)
110     end
111   end
112 end
cloud_request(request, &block) click to toggle source

perform an HTTP request to rackpsace.

request is a Net::HTTP request object. This can be called with and without a block. Without a block, the response is returned as you’d expect

response = http_client.cloud_request(request)

With the block form, the response is yielded to the block:

http_client.cloud_request(request) do |response|
  puts response
end
    # File lib/raca/http_client.rb
 95 def cloud_request(request, &block)
 96   cloud_http do |http|
 97     request['X-Auth-Token'] = @account.auth_token
 98     request['User-Agent'] = "raca 0.4.1 (http://rubygems.org/gems/raca)"
 99     http.request(request, &block)
100   end
101 end
log(msg) click to toggle source
    # File lib/raca/http_client.rb
126 def log(msg)
127   if @logger.respond_to?(:debug)
128     @logger.debug msg
129   end
130 end
raise_on_error(response) click to toggle source
    # File lib/raca/http_client.rb
114 def raise_on_error(response)
115   error_klass = case response.code.to_i
116   when 400 then BadRequestError
117   when 401 then UnauthorizedError
118   when 404 then NotFoundError
119   when 500 then ServerError
120   else
121     HTTPError
122   end
123   raise error_klass, "Rackspace returned HTTP status #{response.code} (rackspace transaction id: #{response["X-TRANS-ID"]})"
124 end