Module: Riak::Client::HTTPBackend::TransportMethods
- Included in:
- Riak::Client::HTTPBackend
- Defined in:
- lib/riak/client/http_backend/transport_methods.rb
Overview
Methods related to performing HTTP requests in a consistent fashion across multiple client libraries. HTTP/1.1 verbs are presented as methods.
Instance Method Summary collapse
- #basic_auth_header ⇒ Object
- #client_id ⇒ Object
-
#default_headers ⇒ Hash
Default header hash sent with every request, based on settings in the client.
-
#delete(expect, resource, headers = {}, &block) ⇒ Hash
Performs a DELETE request to the specified resource on the Riak server.
-
#get(expect, resource, headers = {}, &block) ⇒ Hash
Performs a GET request to the specified resource on the Riak server.
-
#head(expect, resource, headers = {}) ⇒ Hash
Performs a HEAD request to the specified resource on the Riak server.
-
#path(*segments) ⇒ URI
Calculates an absolute URI from a relative path specification.
-
#perform(method, uri, headers, expect, body = nil) {|chunk| ... } ⇒ Hash
abstract
Executes requests according to the underlying HTTP client library semantics.
-
#post(expect, resource, body, headers = {}, &block) ⇒ Hash
Performs a POST request to the specified resource on the Riak server.
-
#put(expect, resource, body, headers = {}, &block) ⇒ Hash
Performs a PUT request to the specified resource on the Riak server.
-
#return_body?(method, code, has_block) ⇒ Boolean
Checks whether a combination of the HTTP method, response code, and block should result in returning the :body in the response hash.
-
#root_uri ⇒ URI
The calculated root URI for the Riak HTTP endpoint.
-
#valid_response?(expected, actual) ⇒ Boolean
Checks the expected response codes against the actual response code.
-
#verify_body!(body) ⇒ Object
Checks whether the submitted body is valid.
Instance Method Details
#basic_auth_header ⇒ Object
142 143 144 |
# File 'lib/riak/client/http_backend/transport_methods.rb', line 142 def basic_auth_header @node.basic_auth ? {"Authorization" => "Basic #{Base64::encode64(@node.basic_auth)}"} : {} end |
#client_id ⇒ Object
132 133 134 135 136 137 138 139 140 |
# File 'lib/riak/client/http_backend/transport_methods.rb', line 132 def client_id value = @client.client_id case value when Integer b64encode(value) when String value end end |
#default_headers ⇒ Hash
Default header hash sent with every request, based on settings in the client
125 126 127 128 129 130 |
# File 'lib/riak/client/http_backend/transport_methods.rb', line 125 def default_headers { "Accept" => "multipart/mixed, application/json;q=0.7, */*;q=0.5", "X-Riak-ClientId" => client_id }.merge(basic_auth_header) end |
#delete(expect, *resource) ⇒ Hash #delete(expect, *resource, headers) ⇒ Hash #delete(expect, *resource, headers = {}) {|chunk| ... } ⇒ Hash
Performs a DELETE request to the specified resource on the Riak server.
103 104 105 106 |
# File 'lib/riak/client/http_backend/transport_methods.rb', line 103 def delete(expect, resource, headers={}, &block) headers = default_headers.merge(headers) perform(:delete, resource, headers, expect, &block) end |
#get(expect, *resource) ⇒ Hash #get(expect, *resource, headers) ⇒ Hash #get(expect, *resource, headers = {}) {|chunk| ... } ⇒ Hash
Performs a GET request to the specified resource on the Riak server.
42 43 44 45 |
# File 'lib/riak/client/http_backend/transport_methods.rb', line 42 def get(expect, resource, headers={}, &block) headers = default_headers.merge(headers) perform(:get, resource, headers, expect, &block) end |
#head(expect, *resource) ⇒ Hash #head(expect, *resource, headers) ⇒ Hash
Performs a HEAD request to the specified resource on the Riak server.
23 24 25 26 |
# File 'lib/riak/client/http_backend/transport_methods.rb', line 23 def head(expect, resource, headers={}) headers = default_headers.merge(headers) perform(:head, resource, headers, expect) end |
#path(*segments) ⇒ URI
Calculates an absolute URI from a relative path specification
155 156 157 158 159 160 161 |
# File 'lib/riak/client/http_backend/transport_methods.rb', line 155 def path(*segments) segments = segments.flatten query = segments..to_param root_uri.merge(segments.join("/").gsub(/\/+/, "/").sub(/^\//, '')).tap do |uri| uri.query = query if query.present? end end |
#perform(method, uri, headers, expect, body = nil) {|chunk| ... } ⇒ Hash
Subclasses must implement this internal method to perform HTTP requests according to the API of their HTTP libraries.
Executes requests according to the underlying HTTP client library semantics.
119 120 121 |
# File 'lib/riak/client/http_backend/transport_methods.rb', line 119 def perform(method, uri, headers, expect, body=nil) raise NotImplementedError end |
#post(expect, *resource, body) ⇒ Hash #post(expect, *resource, body, headers) ⇒ Hash #post(expect, *resource, body, headers = {}) {|chunk| ... } ⇒ Hash
Performs a POST request to the specified resource on the Riak server.
83 84 85 86 87 |
# File 'lib/riak/client/http_backend/transport_methods.rb', line 83 def post(expect, resource, body, headers={}, &block) headers = default_headers.merge(headers) verify_body!(body) perform(:post, resource, headers, expect, body, &block) end |
#put(expect, *resource, body) ⇒ Hash #put(expect, *resource, body, headers) ⇒ Hash #put(expect, *resource, body, headers = {}) {|chunk| ... } ⇒ Hash
Performs a PUT request to the specified resource on the Riak server.
62 63 64 65 66 |
# File 'lib/riak/client/http_backend/transport_methods.rb', line 62 def put(expect, resource, body, headers={}, &block) headers = default_headers.merge(headers) verify_body!(body) perform(:put, resource, headers, expect, body, &block) end |
#return_body?(method, code, has_block) ⇒ Boolean
Checks whether a combination of the HTTP method, response code, and block should result in returning the :body in the response hash. Use internally when implementing #perform.
178 179 180 |
# File 'lib/riak/client/http_backend/transport_methods.rb', line 178 def return_body?(method, code, has_block) method != :head && !valid_response?([204,205,304], code) && !has_block end |
#root_uri ⇒ URI
Returns The calculated root URI for the Riak HTTP endpoint.
147 148 149 150 |
# File 'lib/riak/client/http_backend/transport_methods.rb', line 147 def root_uri protocol = node.ssl_enabled? ? "https" : "http" URI.parse("#{protocol}://#{node.host}:#{node.http_port}") end |
#valid_response?(expected, actual) ⇒ Boolean
Checks the expected response codes against the actual response code. Use internally when implementing #perform.
168 169 170 |
# File 'lib/riak/client/http_backend/transport_methods.rb', line 168 def valid_response?(expected, actual) Array(expected).map(&:to_i).include?(actual.to_i) end |
#verify_body!(body) ⇒ Object
Checks whether the submitted body is valid. That is, it must be a String or respond to the ‘read’ method.
186 187 188 |
# File 'lib/riak/client/http_backend/transport_methods.rb', line 186 def verify_body!(body) raise ArgumentError, t('request_body_type') unless String === body || body.respond_to?(:read) end |