mirror of
https://github.com/lostisland/faraday.git
synced 2025-08-29 00:03:58 -04:00
refactor common Faraday adapter methods into a Faraday::Adapter middleware
This commit is contained in:
parent
c1fdbe3e02
commit
51a004ebc2
@ -49,30 +49,13 @@ module Faraday
|
||||
extend AutoloadHelper
|
||||
|
||||
autoload_all 'faraday',
|
||||
:Adapter => 'adapter',
|
||||
:Connection => 'connection',
|
||||
:Middleware => 'middleware',
|
||||
:Builder => 'builder',
|
||||
:Request => 'request',
|
||||
:Response => 'response',
|
||||
:Error => 'error'
|
||||
|
||||
module Adapter
|
||||
extend AutoloadHelper
|
||||
autoload_all 'faraday/adapter',
|
||||
:ActionDispatch => 'action_dispatch',
|
||||
:NetHttp => 'net_http',
|
||||
:Typhoeus => 'typhoeus',
|
||||
:Patron => 'patron',
|
||||
:Test => 'test'
|
||||
|
||||
register_lookup_modules \
|
||||
:action_dispatch => :ActionDispatch,
|
||||
:test => :Test,
|
||||
:net_http => :NetHttp,
|
||||
:typhoeus => :Typhoeus,
|
||||
:patron => :Patron,
|
||||
:net_http => :NetHttp
|
||||
end
|
||||
end
|
||||
|
||||
# not pulling in active-support JUST for this method.
|
||||
|
52
lib/faraday/adapter.rb
Normal file
52
lib/faraday/adapter.rb
Normal file
@ -0,0 +1,52 @@
|
||||
module Faraday
|
||||
class Adapter < Middleware
|
||||
extend AutoloadHelper
|
||||
autoload_all 'faraday/adapter',
|
||||
:ActionDispatch => 'action_dispatch',
|
||||
:NetHttp => 'net_http',
|
||||
:Typhoeus => 'typhoeus',
|
||||
:Patron => 'patron',
|
||||
:Test => 'test'
|
||||
|
||||
register_lookup_modules \
|
||||
:action_dispatch => :ActionDispatch,
|
||||
:test => :Test,
|
||||
:net_http => :NetHttp,
|
||||
:typhoeus => :Typhoeus,
|
||||
:patron => :Patron,
|
||||
:net_http => :NetHttp
|
||||
|
||||
def call(env)
|
||||
process_body_for_request(env)
|
||||
end
|
||||
|
||||
def process_body_for_request(env)
|
||||
return if env[:body].nil? || env[:body].empty? || !env[:body].respond_to?(:each_key)
|
||||
env[:request_headers]['Content-Type'] ||= 'application/x-www-form-urlencoded'
|
||||
env[:body] = create_form_params(env[:body])
|
||||
end
|
||||
|
||||
def create_form_params(params, base = nil)
|
||||
[].tap do |result|
|
||||
params.each_key do |key|
|
||||
key_str = base ? "#{base}[#{key}]" : key
|
||||
value = params[key]
|
||||
wee = (value.kind_of?(Hash) ? create_form_params(value, key_str) : "#{key_str}=#{escape(value.to_s)}")
|
||||
result << wee
|
||||
end
|
||||
end.join("&")
|
||||
end
|
||||
|
||||
# assume that query and fragment are already encoded properly
|
||||
def full_path_for(path, query = nil, fragment = nil)
|
||||
full_path = path.dup
|
||||
if query && !query.empty?
|
||||
full_path << "?#{query}"
|
||||
end
|
||||
if fragment && !fragment.empty?
|
||||
full_path << "##{fragment}"
|
||||
end
|
||||
full_path
|
||||
end
|
||||
end
|
||||
end
|
@ -1,6 +1,6 @@
|
||||
module Faraday
|
||||
module Adapter
|
||||
class ActionDispatch < Middleware
|
||||
class Adapter
|
||||
class ActionDispatch < Adapter
|
||||
attr_reader :session
|
||||
|
||||
# Initializes a new middleware instance for each request. Instead of
|
||||
@ -18,7 +18,7 @@ module Faraday
|
||||
end
|
||||
|
||||
def call(env)
|
||||
process_body_for_request(env)
|
||||
super
|
||||
full_path = full_path_for(env[:url].path, env[:url].query, env[:url].fragment)
|
||||
@session.__send__(env[:method], full_path, env[:body], env[:request_headers])
|
||||
resp = @session.response
|
||||
|
@ -6,10 +6,10 @@ rescue LoadError
|
||||
end
|
||||
|
||||
module Faraday
|
||||
module Adapter
|
||||
class NetHttp < Middleware
|
||||
class Adapter
|
||||
class NetHttp < Adapter
|
||||
def call(env)
|
||||
process_body_for_request(env)
|
||||
super
|
||||
|
||||
is_ssl = env[:url].scheme == 'https'
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
module Faraday
|
||||
module Adapter
|
||||
class Patron < Middleware
|
||||
class Adapter
|
||||
class Patron < Adapter
|
||||
begin
|
||||
require 'patron'
|
||||
rescue LoadError, NameError => e
|
||||
@ -8,7 +8,7 @@ module Faraday
|
||||
end
|
||||
|
||||
def call(env)
|
||||
process_body_for_request(env)
|
||||
super
|
||||
|
||||
sess = ::Patron::Session.new
|
||||
args = [env[:method], env[:url].to_s, env[:request_headers]]
|
||||
|
@ -1,5 +1,5 @@
|
||||
module Faraday
|
||||
module Adapter
|
||||
class Adapter
|
||||
# test = Faraday::Connection.new do
|
||||
# use Faraday::Adapter::Test do |stub|
|
||||
# stub.get '/nigiri/sake.json' do
|
||||
|
@ -1,6 +1,6 @@
|
||||
module Faraday
|
||||
module Adapter
|
||||
class Typhoeus < Middleware
|
||||
class Adapter
|
||||
class Typhoeus < Adapter
|
||||
self.supports_parallel_requests = true
|
||||
|
||||
def self.setup_parallel_manager(options = {})
|
||||
@ -14,7 +14,7 @@ module Faraday
|
||||
end
|
||||
|
||||
def call(env)
|
||||
process_body_for_request(env)
|
||||
super
|
||||
|
||||
hydra = env[:parallel_manager] || self.class.setup_parallel_manager
|
||||
req = ::Typhoeus::Request.new env[:url].to_s,
|
||||
|
@ -20,35 +20,5 @@ module Faraday
|
||||
def initialize(app = nil)
|
||||
@app = app
|
||||
end
|
||||
|
||||
# assume that query and fragment are already encoded properly
|
||||
def full_path_for(path, query = nil, fragment = nil)
|
||||
full_path = path.dup
|
||||
if query && !query.empty?
|
||||
full_path << "?#{query}"
|
||||
end
|
||||
if fragment && !fragment.empty?
|
||||
full_path << "##{fragment}"
|
||||
end
|
||||
full_path
|
||||
end
|
||||
|
||||
def process_body_for_request(env)
|
||||
# if it's a string, pass it through
|
||||
return if env[:body].nil? || env[:body].empty? || !env[:body].respond_to?(:each_key)
|
||||
env[:request_headers]['Content-Type'] ||= 'application/x-www-form-urlencoded'
|
||||
env[:body] = create_form_params(env[:body])
|
||||
end
|
||||
|
||||
def create_form_params(params, base = nil)
|
||||
[].tap do |result|
|
||||
params.each_key do |key|
|
||||
key_str = base ? "#{base}[#{key}]" : key
|
||||
value = params[key]
|
||||
wee = (value.kind_of?(Hash) ? create_form_params(value, key_str) : "#{key_str}=#{escape(value.to_s)}")
|
||||
result << wee
|
||||
end
|
||||
end.join("&")
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user