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

Instance Method Details

#basic_auth_headerObject



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_idObject



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_headersHash

Default header hash sent with every request, based on settings in the client

Returns:

  • (Hash)

    headers that will be merged with user-specified headers on every request



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.

Overloads:

  • #delete(expect, *resource, headers) ⇒ Hash

    Send the request with custom headers

    Parameters:

    • headers (Hash)

      custom headers to send with the request

  • #delete(expect, *resource, headers = {}) {|chunk| ... } ⇒ Hash

    Stream the response body through the supplied block

    Parameters:

    • headers (Hash) (defaults to: {})

      custom headers to send with the request

    Yields:

    • (chunk)

      yields successive chunks of the response body as strings

    Returns:

    • (Hash)

      response data, containing only the :headers and :code keys

Parameters:

  • expect (Fixnum, Array)

    the expected HTTP response code(s) from Riak

  • resource (String, Array<String,Hash>)

    a relative path or array of path segments and optional query params Hash that will be joined to the root URI

Returns:

  • (Hash)

    response data, containing :headers, :code and :body keys

Raises:

  • (FailedRequest)

    if the response code doesn’t match the expected response



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.

Overloads:

  • #get(expect, *resource, headers) ⇒ Hash

    Send the request with custom headers

    Parameters:

    • headers (Hash)

      custom headers to send with the request

  • #get(expect, *resource, headers = {}) {|chunk| ... } ⇒ Hash

    Stream the response body through the supplied block

    Parameters:

    • headers (Hash) (defaults to: {})

      custom headers to send with the request

    Yields:

    • (chunk)

      yields successive chunks of the response body as strings

    Returns:

    • (Hash)

      response data, containing only the :headers and :code keys

Parameters:

  • expect (Fixnum, Array)

    the expected HTTP response code(s) from Riak

  • resource (String, Array<String,Hash>)

    a relative path or array of path segments and optional query params Hash that will be joined to the root URI

Returns:

  • (Hash)

    response data, containing :headers, :body, and :code keys

Raises:

  • (FailedRequest)

    if the response code doesn’t match the expected response



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.

Overloads:

  • #head(expect, *resource, headers) ⇒ Hash

    Send the request with custom headers

    Parameters:

    • headers (Hash)

      custom headers to send with the request

Parameters:

  • expect (Fixnum, Array)

    the expected HTTP response code(s) from Riak

  • resource (String, Array<String,Hash>)

    a relative path or array of path segments and optional query params Hash that will be joined to the root URI

Returns:

  • (Hash)

    response data, containing only the :headers and :code keys

Raises:

  • (FailedRequest)

    if the response code doesn’t match the expected response



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

Parameters:

  • segments (Array<String,Hash>)

    a relative path or sequence of path segments and optional query params Hash that will be joined to the root URI

Returns:

  • (URI)

    an absolute URI for the resource



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.extract_options!.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

This method is abstract.

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.

Parameters:

  • method (Symbol)

    one of :head, :get, :post, :put, :delete

  • uri (URI)

    the HTTP URI to request

  • headers (Hash)

    headers to send along with the request

  • expect (Fixnum, Array)

    the expected response code(s)

  • body (String, #read) (defaults to: nil)

    the PUT or POST request body

Yields:

  • (chunk)

    if the method is not :head, successive chunks of the response body will be yielded as strings

Returns:

  • (Hash)

    response data, containing :headers, :code and :body keys. Only :headers and :code should be present when the body is streamed or the method is :head.

Raises:

  • (NotImplementedError)

    if a subclass does not implement this method



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.

Overloads:

  • #post(expect, *resource, body, headers) ⇒ Hash

    Send the request with custom headers

    Parameters:

    • headers (Hash)

      custom headers to send with the request

  • #post(expect, *resource, body, headers = {}) {|chunk| ... } ⇒ Hash

    Stream the response body through the supplied block

    Parameters:

    • headers (Hash) (defaults to: {})

      custom headers to send with the request

    Yields:

    • (chunk)

      yields successive chunks of the response body as strings

    Returns:

    • (Hash)

      response data, containing only the :headers and :code keys

Parameters:

  • expect (Fixnum, Array)

    the expected HTTP response code(s) from Riak

  • resource (String, Array<String>)

    a relative path or array of path segments that will be joined to the root URI

  • body (String)

    the request body to send to the server

Returns:

  • (Hash)

    response data, containing :headers, :code and :body keys

Raises:

  • (FailedRequest)

    if the response code doesn’t match the expected response



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.

Overloads:

  • #put(expect, *resource, body, headers) ⇒ Hash

    Send the request with custom headers

    Parameters:

    • headers (Hash)

      custom headers to send with the request

  • #put(expect, *resource, body, headers = {}) {|chunk| ... } ⇒ Hash

    Stream the response body through the supplied block

    Parameters:

    • headers (Hash) (defaults to: {})

      custom headers to send with the request

    Yields:

    • (chunk)

      yields successive chunks of the response body as strings

    Returns:

    • (Hash)

      response data, containing only the :headers and :code keys

Parameters:

  • expect (Fixnum, Array)

    the expected HTTP response code(s) from Riak

  • resource (String, Array<String,Hash>)

    a relative path or array of path segments and optional query params Hash that will be joined to the root URI

  • body (String)

    the request body to send to the server

Returns:

  • (Hash)

    response data, containing :headers, :code, and :body keys

Raises:

  • (FailedRequest)

    if the response code doesn’t match the expected response



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.

Parameters:

  • method (Symbol)

    the HTTP method

  • code (String, Fixnum)

    the received response code

  • has_block (Boolean)

    whether a streaming block was passed to #perform. Pass block_given? to this parameter.

Returns:

  • (Boolean)

    whether to return the body in the response hash



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_uriURI

Returns The calculated root URI for the Riak HTTP endpoint.

Returns:

  • (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

#valid_response?(expected, actual) ⇒ Boolean

Checks the expected response codes against the actual response code. Use internally when implementing #perform.

Parameters:

  • expected (String, Fixnum, Array<String,Fixnum>)

    the expected response code(s)

  • actual (String, Fixnum)

    the received response code

Returns:

  • (Boolean)

    whether the actual response code is acceptable given the expectations



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.

Parameters:

  • body (String, #read)

    the body

Raises:

  • (ArgumentError)

    if the body is of invalid type



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