Sha256: 2727bfdaf940fa7e80a0764e71103997aa32109444d4f8e0a853e22d9e699bd1

Contents?: true

Size: 1.83 KB

Versions: 6

Compression:

Stored size: 1.83 KB

Contents

require 'faraday'

class Faraday::Request::OSToken
  def initialize(app, token)
    @app = app
    @token = token
  end

  def call(env)
    if @token.expired?
      @token.reflesh(Yao.default_client.default)
    end

    env[:request_headers]['X-Auth-Token'] = @token.to_s
    @app.call(env)
  end
end
Faraday::Request.register_middleware os_token: -> { Faraday::Request::OSToken }

class Faraday::Response::OSDumper < Faraday::Response::Middleware
  def on_complete(env)
    require 'pp'
    params = [
      env.url.to_s,
      JSON.parse(env.body),
      env.request_headers,
      env.response_headers,
      env.method,
      env.status
    ].map(&:pretty_inspect)
    $stdout.puts(<<-FMT % params)
================================
 OpenStack response inspection:
================================
Requested To: %s

Body:
%s
Request Headers:
%s
Response Headers:
%s

Method: %s
Status Code: %s
================================

    FMT
  end
end
Faraday::Response.register_middleware os_dumper: -> { Faraday::Response::OSDumper }

class Faraday::Response::OSResponseRecorder < Faraday::Response::Middleware
  def on_complete(env)
    require 'pathname'
    root = Pathname.new(File.expand_path('../../../tmp', __FILE__))
    path = [env.method.to_s.upcase, env.url.path.gsub('/', '-')].join("-") + ".json"

    puts root.join(path)
    File.open(root.join(path), 'w') do |f|
      f.write env.body
    end
  end
end
Faraday::Response.register_middleware os_response_recorder: -> { Faraday::Response::OSResponseRecorder }

require 'yao/server_error'
class Faraday::Response::OSErrorDetector < Faraday::Response::Middleware
  # TODO: Better handling, respecting official doc
  def on_complete(env)
    return if env.success?

    raise Yao::ServerError.detect(env)
  end
end
Faraday::Response.register_middleware os_error_detector: -> { Faraday::Response::OSErrorDetector }

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
yao-0.1.0 lib/yao/faraday_middlewares.rb
yao-0.0.6 lib/yao/faraday_middlewares.rb
yao-0.0.5 lib/yao/faraday_middlewares.rb
yao-0.0.4 lib/yao/faraday_middlewares.rb
yao-0.0.3 lib/yao/faraday_middlewares.rb
yao-0.0.2 lib/yao/faraday_middlewares.rb