Class: Raca::HttpClient
- Inherits:
-
Object
- Object
- Raca::HttpClient
- 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
Instance Method Summary collapse
- #delete(path, headers = {}) ⇒ Object
- #get(path, headers = {}, &block) ⇒ Object
- #head(path, headers = {}) ⇒ Object
-
#initialize(account, hostname, opts = {}) ⇒ HttpClient
constructor
A new instance of HttpClient.
- #inspect ⇒ Object
- #post(path, body, headers = {}) ⇒ Object
- #put(path, headers = {}) ⇒ Object
- #streaming_put(path, io, byte_count, headers = {}) ⇒ Object
Constructor Details
#initialize(account, hostname, opts = {}) ⇒ HttpClient
Returns a new instance of HttpClient.
14 15 16 17 18 19 |
# File 'lib/raca/http_client.rb', line 14 def initialize(account, hostname, opts = {}) @account, @hostname = account, 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
35 36 37 38 39 40 |
# File 'lib/raca/http_client.rb', line 35 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
21 22 23 24 25 26 |
# File 'lib/raca/http_client.rb', line 21 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
28 29 30 31 32 33 |
# File 'lib/raca/http_client.rb', line 28 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 |
#inspect ⇒ Object
68 69 70 |
# File 'lib/raca/http_client.rb', line 68 def inspect "#<Raca::HttpClient:#{__id__}>" end |
#post(path, body, headers = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/raca/http_client.rb', line 57 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
42 43 44 45 46 47 |
# File 'lib/raca/http_client.rb', line 42 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
49 50 51 52 53 54 55 |
# File 'lib/raca/http_client.rb', line 49 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 |