Class: SparkApi::Authentication::OAuth2Impl::FaradayMiddleware
- Inherits:
-
Faraday::Response::Middleware
- Object
- Faraday::Response::Middleware
- SparkApi::Authentication::OAuth2Impl::FaradayMiddleware
- Defined in:
- lib/spark_api/authentication/oauth2_impl/faraday_middleware.rb
Overview
OAuth2 Faraday response middleware
HTTP Response after filter to package oauth2 responses and bubble up basic api errors.
Instance Method Summary collapse
-
#initialize(app) ⇒ FaradayMiddleware
constructor
A new instance of FaradayMiddleware.
- #on_complete(env) ⇒ Object
Constructor Details
#initialize(app) ⇒ FaradayMiddleware
Returns a new instance of FaradayMiddleware.
11 12 13 |
# File 'lib/spark_api/authentication/oauth2_impl/faraday_middleware.rb', line 11 def initialize(app) super(app) end |
Instance Method Details
#on_complete(env) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/spark_api/authentication/oauth2_impl/faraday_middleware.rb', line 15 def on_complete(env) body = MultiJson.decode(env[:body]) SparkApi.logger.debug { "[oauth2] Response Body: #{body.inspect}" } unless body.is_a?(Hash) raise InvalidResponse, "The server response could not be understood" end case env[:status] when 200..299 SparkApi.logger.debug{ "[oauth2] Success!" } session = OAuthSession.new(body) else SparkApi.logger.warn { "[oauth2] failure #{body.inspect}" } # Handle the WWW-Authenticate Response Header Field if present. This can be returned by # OAuth2 implementations and wouldn't hurt to log. auth_header_error = env[:request_headers]["WWW-Authenticate"] SparkApi.logger.warn { "Authentication error #{auth_header_error}" } unless auth_header_error.nil? raise ClientError, {:message => body["error"], :code =>0, :status => env[:status], :request_path => env[:url]} end SparkApi.logger.debug { "[oauth2] Session=#{session.inspect}" } env[:body] = session end |