Sha256: 69cdce6b79b27820d77969314217ccbee8382f9c988f5d3703db00c46f017145

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

require 'faraday_middleware/ext/uri_ext'
require 'faraday_middleware/aws_signers_v4_ext'

class FaradayMiddleware::AwsSignersV4 < Faraday::Middleware
  class Request
    def initialize(env)
      @env = env
    end

    def headers
      @env.request_headers
    end

    def body
      @env.body || ''
    end

    def endpoint
      url = @env.url.dup

      # Escape the query string or the request won't sign correctly
      if url and url.query
        re_escape_query!(url)
      end

      url
    end

    def http_method
      @env.method.to_s.upcase
    end

    private

    def re_escape_query!(url)
      params = URI.decode_www_form(url.query)

      if params.any? {|k, v| v =~ / / }
        url.query = URI.seahorse_encode_www_form(params)
      end
    end
  end # of class Request

  def initialize(app, options = nil)
    super(app)

    @credentials = options.fetch(:credentials)
    @service_name = options.fetch(:service_name)
    @region = options.fetch(:region)
  end

  def call(env)
    req = Request.new(env)
    Aws::Signers::V4.new(@credentials, @service_name, @region).sign(req)
    @app.call(env)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
faraday_middleware-aws-signers-v4-0.1.9 lib/faraday_middleware/request/aws_signers_v4.rb