Class: Farscape::Agent::HTTPClient
- Inherits:
-
BaseClient
- Object
- BaseClient
- Farscape::Agent::HTTPClient
- Defined in:
- lib/farscape/client/http_client.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
The Faraday connection instance.
-
#Plugins ⇒ Object
readonly
Returns the value of attribute Plugins.
Instance Method Summary collapse
- #dispatch_error(response) ⇒ Object
-
#faraday_adapter ⇒ Object
Override this in a subclass to create clients with custom Faraday adapters.
-
#initialize(agent) ⇒ HTTPClient
constructor
A new instance of HTTPClient.
- #interface_methods ⇒ Object
-
#invoke(options = {}) ⇒ Faraday::Response
Makes a Faraday request given the specified options.
Methods inherited from BaseClient
#idempotent_method?, #safe_method?, #unsafe_method?
Constructor Details
#initialize(agent) ⇒ HTTPClient
Returns a new instance of HTTPClient.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/farscape/client/http_client.rb', line 15 def initialize(agent) @connection = Faraday.new do |builder| builder.request :json agent.middleware_stack.each do |middleware| if middleware.key?(:config) config = middleware[:config] if config.is_a?(Array) builder.use(middleware[:class], *config) else builder.use(middleware[:class], config) end else builder.use(middleware[:class]) end end builder.adapter faraday_adapter end end |
Instance Attribute Details
#connection ⇒ Object (readonly)
The Faraday connection instance.
12 13 14 |
# File 'lib/farscape/client/http_client.rb', line 12 def connection @connection end |
#Plugins ⇒ Object (readonly)
Returns the value of attribute Plugins.
13 14 15 |
# File 'lib/farscape/client/http_client.rb', line 13 def Plugins @Plugins end |
Instance Method Details
#dispatch_error(response) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/farscape/client/http_client.rb', line 66 def dispatch_error(response) errors = Farscape::Exceptions http_code = { 400 => errors::BadRequest, 401 => errors::Unauthorized, 403 => errors::Forbidden, 404 => errors::NotFound, 405 => errors::MethodNotAllowed, 406 => errors::NotAcceptable, 407 => errors::ProxyAuthenticationRequired, 408 => errors::RequestTimeout, 409 => errors::Conflict, 410 => errors::Gone, 411 => errors::LengthRequired, 412 => errors::PreconditionFailed, 413 => errors::RequestEntityTooLarge, 414 => errors::RequestUriTooLong, 415 => errors::UnsupportedMediaType, 416 => errors::RequestedRangeNotSatisfiable, 417 => errors::ExpectationFailed, 418 => errors::ImaTeapot, 422 => errors::UnprocessableEntity, 500 => errors::InternalServerError, 501 => errors::NotImplemented, 502 => errors::BadGateway, 503 => errors::ServiceUnavailable, 504 => errors::GatewayTimeout, 505 => errors::ProtocolVersionNotSupported, } http_code[response.status] || errors::ProtocolException unless response.success? end |
#faraday_adapter ⇒ Object
Override this in a subclass to create clients with custom Faraday adapters
36 37 38 |
# File 'lib/farscape/client/http_client.rb', line 36 def faraday_adapter Faraday.default_adapter end |
#interface_methods ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/farscape/client/http_client.rb', line 58 def interface_methods { idempotent: ['PUT', 'DELETE'], unsafe: ['POST', 'PATCH'], # http://tools.ietf.org/html/rfc5789 safe: ['GET', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT'] } end |
#invoke(options = {}) ⇒ Faraday::Response
Makes a Faraday request given the specified options
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/farscape/client/http_client.rb', line 46 def invoke( = {}) defaults = { method: 'get'} = defaults.merge() connection.send([:method].to_s.downcase) do |req| req.url [:url] req.body = [:body] if .has_key?(:body) [:params].each { |k,v| req.params[k] = v } if .has_key?(:params) [:headers].each { |k,v| req.headers[k] = v } if .has_key?(:headers) end end |