Class: Bisques::AwsRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/bisques/aws_request.rb

Overview

A request to an AWS API call. This class must be initiated with a client instance of HTTPClient. A number of mandatory attributes must be set before calling #make_request to return the response. #make_request returns an AwsResponse object.

Examples:


request = AwsRequest.new(httpclient)
request.method = "GET" or "POST"
request.query = {"hash" => "of query params"}
request.body = {"hash" => "of form params"} or "text body"
request.headers = {"hash" => "of optional headers"}
request.path = "optional path"
request.region = "AWS region (ex. us-east-1)"
request.service = "AWS service (ex. sqs)"
request.credentials = AwsCredentials.new("aws_key", "aws_secret")
response = request.make_request

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(httpclient) ⇒ AwsRequest

Create a new Bisques::AwsRequest using the given HTTPClient object.

Parameters:

  • httpclient (HTTPClient)


63
64
65
# File 'lib/bisques/aws_request.rb', line 63

def initialize(httpclient)
  @httpclient = httpclient
end

Instance Attribute Details

#bodyHash, String

Returns A hash or string describing the form params or HTTP body. Only used when the method is POST or PUT.

Returns:

  • (Hash, String)

    A hash or string describing the form params or HTTP body. Only used when the method is POST or PUT.



30
31
32
# File 'lib/bisques/aws_request.rb', line 30

def body
  @body
end

#credentialsAwsCredentials

Returns The AWS credentials. Should respond to aws_key and aws_secret.

Returns:

  • (AwsCredentials)

    The AWS credentials. Should respond to aws_key and aws_secret.



41
42
43
# File 'lib/bisques/aws_request.rb', line 41

def credentials
  @credentials
end

#headersHash

Returns A hash of additional HTTP headers to send with the request.

Returns:

  • (Hash)

    A hash of additional HTTP headers to send with the request.



32
33
34
# File 'lib/bisques/aws_request.rb', line 32

def headers
  @headers
end

#methodString

Returns The HTTP method to use. Should be GET or POST.

Returns:

  • (String)

    The HTTP method to use. Should be GET or POST.



25
26
27
# File 'lib/bisques/aws_request.rb', line 25

def method
  @method
end

#pathString

Returns The path to the API call. This shouldn’t be the full URL as the host part is built from the region and service.

Returns:

  • (String)

    The path to the API call. This shouldn’t be the full URL as the host part is built from the region and service.



35
36
37
# File 'lib/bisques/aws_request.rb', line 35

def path
  @path
end

#queryHash

Returns A hash describing the query params to send.

Returns:

  • (Hash)

    A hash describing the query params to send.



27
28
29
# File 'lib/bisques/aws_request.rb', line 27

def query
  @query
end

#regionString

Returns The AWS region. Ex: us-east-1.

Returns:

  • (String)

    The AWS region. Ex: us-east-1



37
38
39
# File 'lib/bisques/aws_request.rb', line 37

def region
  @region
end

#responseAwsResponse (readonly)

Returns An AwsResponse object describing the response. Returns nil until #make_request has been called.

Returns:



45
46
47
# File 'lib/bisques/aws_request.rb', line 45

def response
  @response
end

#serviceString

Returns The AWS service. Ex: sqs.

Returns:

  • (String)

    The AWS service. Ex: sqs



39
40
41
# File 'lib/bisques/aws_request.rb', line 39

def service
  @service
end

Class Method Details

.aws_encode(value) ⇒ String

AWS has some particular rules about how it likes it’s form params encoded.

Parameters:

  • value (String)

Returns:

  • (String)

    encoded value



54
55
56
57
58
# File 'lib/bisques/aws_request.rb', line 54

def self.aws_encode(value)
  value.to_s.gsub(/([^a-zA-Z0-9._~-]+)/n) do
    '%' + $1.unpack('H2' * $1.size).join('%').upcase
  end
end

Instance Method Details

#make_requestObject

Send the HTTP request and get a response. Returns an AwsResponse object. The instance is frozen once this method is called and cannot be used again.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/bisques/aws_request.rb', line 77

def make_request
  create_authorization

  options = {}
  options[:header] = authorization.headers.merge(
    'Authorization' => authorization.authorization_header
  )
  options[:query] = query if query.any?
  options[:body] = form_body if body

  http_response = @httpclient.request(method, url, options)
  @response = AwsResponse.new(self, http_response)

  freeze

  @response
end

#urlString

The full URL to the API endpoint the request will call.

Returns:

  • (String)


70
71
72
# File 'lib/bisques/aws_request.rb', line 70

def url
  File.join("https://#{service}.#{region}.amazonaws.com", path)
end