Module: Miasma::Contrib::AwsApiCore::ApiCommon

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/miasma/contrib/aws.rb', line 327

def self.included(klass)
  klass.class_eval do
    attribute :aws_access_key_id, String, :required => true
    attribute :aws_secret_access_key, String, :required => true
    attribute :aws_region, String, :required => true
    attribute :aws_host, String
    attribute :aws_bucket_region, String

    # @return [Contrib::AwsApiCore::SignatureV4]
    attr_reader :signer
  end
end

Instance Method Details

#api_for(type) ⇒ Api

Build new API for specified type using current provider / creds

Parameters:

  • type (Symbol)

    api type

Returns:

  • (Api)


344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/miasma/contrib/aws.rb', line 344

def api_for(type)
  memoize(type) do
    creds = attributes.dup
    creds.delete(:aws_host)
    Miasma.api(
      Smash.new(
        :type => type,
        :provider => provider,
        :credentials => creds
      )
    )
  end
end

#connectObject

Setup for API connections



359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/miasma/contrib/aws.rb', line 359

def connect
  unless(aws_host)
    self.aws_host = [
      self.class::API_SERVICE.downcase,
      aws_region,
      'amazonaws.com'
    ].join('.')
  end
  @signer = Contrib::AwsApiCore::SignatureV4.new(
    aws_access_key_id, aws_secret_access_key, aws_region, self.class::API_SERVICE
  )
end

#connectionHTTP

Returns connection for requests (forces headers).

Returns:

  • (HTTP)

    connection for requests (forces headers)



378
379
380
381
382
383
# File 'lib/miasma/contrib/aws.rb', line 378

def connection
  super.with_headers(
    'Host' => aws_host,
    'X-Amz-Date' => Contrib::AwsApiCore.time_iso8601
  )
end

#endpointString

Returns endpoint for request.

Returns:

  • (String)

    endpoint for request



386
387
388
# File 'lib/miasma/contrib/aws.rb', line 386

def endpoint
  "https://#{aws_host}"
end

#make_request(connection, http_method, request_args) ⇒ HTTP::Response

Note:

if http_method is :post, params will be automatically removed and placed into :form

Override to inject signature

Parameters:

  • connection (HTTP)
  • http_method (Symbol)
  • request_args (Array)

Returns:

  • (HTTP::Response)


398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/miasma/contrib/aws.rb', line 398

def make_request(connection, http_method, request_args)
  dest, options = request_args
  path = URI.parse(dest).path
  options = options ? options.to_smash : Smash.new
  options[:params] = options.fetch(:params, Smash.new).to_smash.deep_merge('Version' => self.class::API_VERSION)
  if(http_method.to_sym == :post)
    if(options[:form])
      options[:form].merge(options.delete(:params))
    else
      options[:form] = options.delete(:params)
    end
  end
  update_request(connection, options)
  signature = signer.generate(
    http_method, path, options.merge(
      Smash.new(
        :headers => Smash[
          connection.default_headers.to_a
        ]
      )
    )
  )
  options = Hash[options.map{|k,v|[k.to_sym,v]}]
  connection.auth(signature).send(http_method, dest, options)
end

#update_request(con, opts) ⇒ TrueClass

Simple callback to allow request option adjustments prior to signature calculation

Parameters:

  • opts (Smash)

    request options

Returns:

  • (TrueClass)


429
430
431
# File 'lib/miasma/contrib/aws.rb', line 429

def update_request(con, opts)
  true
end

#uri_escape(string) ⇒ String

Returns custom escape for aws compat.

Returns:

  • (String)

    custom escape for aws compat



373
374
375
# File 'lib/miasma/contrib/aws.rb', line 373

def uri_escape(string)
  signer.safe_escape(string)
end