Module: Bisques::AwsConnection

Included in:
Client
Defined in:
lib/bisques/aws_connection.rb

Overview

This module is for making API classes more convenient. The including class must pass the correct params via super from it’s #initialize call. Two useful methods are added to the including class, #request and #action.

Examples:

class Sqs
  include AwsConnection

  def initialize(region)
    super(region, 'sqs')
  end
end

Instance Method Summary collapse

Instance Method Details

#action(action_name, path = "/", options = {}) ⇒ AwsResponse

Note:

The API call will be automatically retried once if the returned status code is in the 500 range.

Call an AWS API with the given name at the given path. An optional hash of options can be passed as arguments for the API call.

Parameters:

  • action_name (String)
  • path (String) (defaults to: "/")
  • options (Hash) (defaults to: {})

Returns:

Raises:

  • (AwsActionError)

    if the response is not successful. AWS error information can be extracted from the exception.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/bisques/aws_connection.rb', line 96

def action(action_name, path = "/", options = {})
  retries = 0

  begin
    # If options given in place of path assume /
    options, path = path, "/" if path.is_a?(Hash) && options.empty?
    request(:post, path, {}, options.merge("Action" => action_name)).response.tap do |response|
      unless response.success?
        element = response.doc.xpath("//Error")
        raise AwsActionError.new(element.xpath("Type").text, element.xpath("Code").text, element.xpath("Message").text, response.http_response.status)
      end
    end
  rescue AwsActionError => e
    if retries < 2 && (500..599).include?(e.status.to_i)
      retries += 1
      retry
    else
      raise e
    end
  end
end

#initialize(region, service, credentials = AwsCredentials.default) ⇒ Object

Give the region, service and optionally the AwsCredentials.

Examples:

class Sqs
  include AwsConnection

  def initialize(region)
    super(region, 'sqs')
  end
end

Parameters:

  • region (String)

    the AWS region (ex. us-east-1)

  • service (String)

    the AWS service (ex. sqs)

  • credentials (AwsCredentials) (defaults to: AwsCredentials.default)


55
56
57
# File 'lib/bisques/aws_connection.rb', line 55

def initialize(region, service, credentials = AwsCredentials.default)
  @region, @service, @credentials = region, service, credentials
end

#marshal_dumpObject



118
119
120
# File 'lib/bisques/aws_connection.rb', line 118

def marshal_dump
  [@region, @service, @credentials]
end

#marshal_load(array) ⇒ Object



122
123
124
# File 'lib/bisques/aws_connection.rb', line 122

def marshal_load array
  @region, @service, @credentials = array
end

#request(method, path, query = {}, body = nil, headers = {}) ⇒ AwsRequest

Perform an HTTP query to the given path using the given method (GET, POST, PUT, DELETE). A hash of query params can be specified. A POST or PUT body cna be specified as either a string or a hash of form params. A hash of HTTP headers can be specified.

Parameters:

  • method (String)

    HTTP method, should be GET, POST, PUT or DELETE

  • path (String)
  • query (Hash) (defaults to: {})

    HTTP query params to send. Specify these as a hash, do not append them to the path.

  • body (Hash, #to_s) (defaults to: nil)

    HTTP request body. This can be form data as a hash or a String. Only applies to POST and PUT HTTP methods.

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

    additional HTTP headers to send.

Returns:



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bisques/aws_connection.rb', line 70

def request(method, path, query = {}, body = nil, headers = {})
  AwsRequest.new(aws_http_connection).tap do |aws_request|
    aws_request.credentials = credentials
    aws_request.path = path
    aws_request.region = region
    aws_request.service = service
    aws_request.method = method
    aws_request.query = query
    aws_request.body = body
    aws_request.headers = headers
    aws_request.make_request
  end
end