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)
- - (Object) basic_auth_header
- - (Object) client_id
-
- (Hash) default_headers
Default header hash sent with every request, based on settings in the client.
-
- (Hash) delete(expect, resource, headers = {}, &block)
Performs a DELETE request to the specified resource on the Riak server.
-
- (Hash) get(expect, resource, headers = {}, &block)
Performs a GET request to the specified resource on the Riak server.
-
- (Hash) head(expect, resource, headers = {})
Performs a HEAD request to the specified resource on the Riak server.
-
- (URI) path(*segments)
Calculates an absolute URI from a relative path specification.
-
- (Hash) perform(method, uri, headers, expect, body = nil) {|chunk| ... }
abstract
Executes requests according to the underlying HTTP client library semantics.
-
- (Hash) post(expect, resource, body, headers = {}, &block)
Performs a POST request to the specified resource on the Riak server.
-
- (Hash) put(expect, resource, body, headers = {}, &block)
Performs a PUT request to the specified resource on the Riak server.
-
- (Boolean) return_body?(method, code, has_block)
Checks whether a combination of the HTTP method, response code, and block should result in returning the :body in the response hash.
-
- (URI) root_uri
The calculated root URI for the Riak HTTP endpoint.
-
- (Boolean) valid_response?(expected, actual)
Checks the expected response codes against the actual response code.
-
- (Object) verify_body!(body)
Checks whether the submitted body is valid.
Instance Method Details
- (Object) basic_auth_header
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 |
- (Object) client_id
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 |
- (Hash) default_headers
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 |
- (Hash) delete(expect, *resource) - (Hash) delete(expect, *resource, headers) - (Hash) delete(expect, *resource, headers = {}) {|chunk| ... }
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 |
- (Hash) get(expect, *resource) - (Hash) get(expect, *resource, headers) - (Hash) get(expect, *resource, headers = {}) {|chunk| ... }
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 |
- (Hash) head(expect, *resource) - (Hash) head(expect, *resource, headers)
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 |
- (URI) path(*segments)
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 |
- (Hash) perform(method, uri, headers, expect, body = nil) {|chunk| ... }
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 |
- (Hash) post(expect, *resource, body) - (Hash) post(expect, *resource, body, headers) - (Hash) post(expect, *resource, body, headers = {}) {|chunk| ... }
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 |
- (Hash) put(expect, *resource, body) - (Hash) put(expect, *resource, body, headers) - (Hash) put(expect, *resource, body, headers = {}) {|chunk| ... }
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 |
- (Boolean) return_body?(method, code, has_block)
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 |
- (URI) root_uri
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 |
- (Boolean) valid_response?(expected, actual)
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 |
- (Object) verify_body!(body)
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 |