class AGCOD::Request

Constants

MOCK_REQUEST_IDS
TIME_FORMAT

Attributes

response[R]

Public Class Methods

new(action, params) click to toggle source
# File lib/aws_agcod/request.rb, line 13
def initialize(action, params)
  @action = action
  @params = sanitized_params(params)

  @response = Response.new(HTTParty.post(uri, body: body, headers: signed_headers, timeout: AGCOD.config.timeout).body)
end

Private Instance Methods

body() click to toggle source
# File lib/aws_agcod/request.rb, line 41
def body
  @body ||= @params.merge(
    "partnerId" => partner_id
  ).to_json
end
partner_id() click to toggle source
# File lib/aws_agcod/request.rb, line 64
def partner_id
  @partner_id ||= AGCOD.config.partner_id
end
sanitized_params(params) click to toggle source
# File lib/aws_agcod/request.rb, line 47
def sanitized_params(params)
  # Prefix partner_id in creationRequestId when it's not given as part of request_id, and it's not a mocked request_id.
  if params["creationRequestId"] &&
    !(params["creationRequestId"] =~ /#{partner_id}/) &&
    !(MOCK_REQUEST_IDS.member?(params["creationRequestId"]))

    params["creationRequestId"] = "#{partner_id}#{params["creationRequestId"]}"
  end

  # Remove partner_id when it's prefixed in requestId
  if params["requestId"] && !!(params["requestId"] =~ /^#{partner_id}/)
    params["requestId"].sub!(/^#{partner_id}/, "")
  end

  params
end
signed_headers() click to toggle source
# File lib/aws_agcod/request.rb, line 22
def signed_headers
  time = Time.now.utc

  headers = {
    "content-type" => "application/json",
    "x-amz-date" => time.strftime(TIME_FORMAT),
    "accept" => "application/json",
    "host" => uri.host,
    "x-amz-target" => "com.amazonaws.agcod.AGCODService.#{@action}",
    "date" => time.to_s
  }

  Signature.new(AGCOD.config).sign(uri, headers, body)
end
uri() click to toggle source
# File lib/aws_agcod/request.rb, line 37
def uri
  @uri ||= URI("#{AGCOD.config.uri}/#{@action}")
end