ํจ๋ฌ๋ฐ์ด 0.8.X๋ ์ฅํฐํธ์ค ํด๋ผ์ด์ธํธ๋ฅผ ์ง์ํฉ๋๊น?
์ง์ OK ;)
ํ ๋ด๊ฐ ์ค์ :
conn = Faraday.new(url, ssl: {verify: false}) do |builder|
builder.response :follow_redirects, :limit => 3, :standards_compliant => false, :cookie => :all
builder.adapter : ์ฅํฐํธ์ค
๋
์์ฒญ์ ๋ณด๋์ง๋ง net_http !
em_http์ ์ฅํฐํธ์ค๋ก ์ ํํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผ ํฉ๋๊น?
0.7.X์์๋ ์ฅํฐํธ์ค๊ฐ ์ ์๋ํ์ต๋๋ค.
๋ด ์คํ ์ถ์ :
https://gist.github.com/3743068
typhoeus ๋๋ em_http ์ด๋ํฐ๋ฅผ ์ค์ ํ์ง๋ง ๋ก๊ทธ์์:
/home/johnbat26/.rvm/gems/ruby-1.9. 3-p194@webex/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb :54:in new'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb:54:in
create_request'
/home/johnbat26/.rvm/gems/ruby-1.9. 3-p194@webex/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb :74:in perform_request'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb:37:in
ํธ์ถ'
์๋ฒ๋ ์ํ 503์ ์๋ต์ ๋ฐ์ง๋ง net_http๋ ์ค๋ฅ๋ฅผ ์ป์ต๋๋ค.
์ค๋ฅ
nil:NilClass์ ๋ํ ์ ์๋์ง ์์ ๋ฉ์๋ `strip'
๋ค์๊ณผ ๊ฐ์ด ํจ๋ฌ๋ฐ์ด ์ฐ๊ฒฐ์ ์ด๊ธฐํํฉ๋๋ค.
conn = Faraday.new(url, ssl: {verify: false}) do |builder|
builder.response :follow_redirects, :limit => 3, :standards_compliant => false, :cookie => :all
builder.adapter :typhoeus
end
๊ด์ฐฎ์ ๋ณด์ธ๋ค. ๊ทธ๋ฌ๋ ๋์ผํ ์ฐ๊ฒฐ์ ์ฌ์ฉํ์ฌ ์์ฒญ์ ํ๊ณ ์์ต๋๊น? Faraday๋ฅผ ์ฌ์ฉํ์ฌ ์์ฒญ์ ์ํํ๋ ์ฝ๋๋ฅผ ๋ณด์ฌ์ฃผ์ง ์์์ต๋๋ค. ๋ ๊ฐ์ ์๋ก ๋ค๋ฅธ ํจ๋ฌ๋ฐ์ด ์ฐ๊ฒฐ์ ์ฌ์ฉ ์ค์ผ ์ ์์ต๋๋ค.
ํ: ๊ธฐ๋ณธ ์ต์ ๊ณผ ํจ๊ป follow_redirects๋ฅผ ์ฌ์ฉํด ๋ณด์ญ์์ค.
builder.response :follow_redirects
๋๋ฒ๊ทธ ๋ชจ๋์์ ๊ฐ์ฒด conn์ด ์ด๋ํฐ ์ฅํฐํธ์ค๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋ณผ ์ ์์ง๋ง ๋ก๊ทธ์๋ net_http๊ฐ ํ์๋ฉ๋๋ค?! ์์?
๋ค์ ์ฝ๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
require 'faraday'
require 'faraday_middleware'
require 'em-http'
require 'typhoeus'
module MediationHelper
# Parameters:
# url = args[:url]
# method = args[:method] || :post
# adapter = args[:adapter] || :net_http
# body = args[:body] || ""
# params = args[:params]
# authentication = args[:auth]
# headers = args[:headers] || {'Content-Type' => 'text/xml; charset=utf-8'}
# timeout = args[:timeout] || 60
# open_timeout = args[:open_timeout] || 60
#
def self.send_http_request(args)
url = args[:url]
method = args[:method] || :post
adapter = args[:adapter] || :net_http
params = args[:params]
body = args[:body] || ""
authentication = args[:auth]
headers = args[:headers] || {'Content-Type' => 'text/xml; charset=utf-8'}
timeout = args[:timeout] || 60
open_timeout = args[:open_timeout] || 60
conn = Faraday.new(url, ssl: {verify: false}) do |conn|
conn.response :follow_redirects
conn.adapter adapter
end
conn.basic_auth(args[:username], args[:password]) if authentication
response = conn.send(method) do |req|
req.headers.update headers
req.body = body
req.params = params if params
req.options = {
:timeout => timeout, # open/read timeout Integer in seconds
:open_timeout => open_timeout, # read timeout Integer in seconds
}
end
response
end
end
๋ค์์ผ๋ก ์ด๊ฒ์ ํธ์ถํฉ๋๋ค.
response_for_login_request = MediationHelper.send_http_request(:url => url,
:body => login_request_xml,
:adapter => :typhoeus
:headers => {'Content-Type' => "text/xml; charset=utf-8",
'Cookie' => my__cookie,
'SOAPAction' => '""'})
๋๋ ์ค์ :
conn.response:follow_redirects
์ฌ์ ํ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
์์ธ
๋ฉ์๋ ์ค๋ฅ ์์
์ค๋ฅ
nil:NilClass์ ๋ํ ์ ์๋์ง ์์ ๋ฉ์๋ `strip'
/home/johnbat26/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1435:in `block in initialize_http_header'
/home/johnbat26/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1433:in `each'
/home/johnbat26/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1433:in `initialize_http_header'
/home/johnbat26/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1862:in `initialize'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb:54:in `new'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb:54:in `create_request'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb:74:in `perform_request'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb:37:in `call'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday_middleware-0.8.8/lib/faraday_middleware/response/follow_redirects.rb:76:in `perform_with_redirection'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday_middleware-0.8.8/lib/faraday_middleware/response/follow_redirects.rb:65:in `call'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday-0.8.4/lib/faraday/connection.rb:226:in `run_request'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday-0.8.4/lib/faraday/connection.rb:99:in `post'
/home/coding/projects/idea_projects/tsp-bwks-mediation/app/helpers/mediation_helper.rb:50:in `send_http_request'
/home/coding/projects/idea_projects/tsp-bwks-mediation/app/helpers/broadworks_helper.rb:81:in `authenticate'
/home/coding/projects/idea_projects/tsp-bwks-mediation/app/helpers/broadworks_helper.rb:121:in `init_broadworks_session'
/home/coding/projects/idea_projects/tsp-bwks-mediation/app/jobs/webex_requests_job.rb:24:in `block in perform'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/nokogiri-1.5.5/lib/nokogiri/xml/node_set.rb:239:in `block in each'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/nokogiri-1.5.5/lib/nokogiri/xml/node_set.rb:238:in `upto'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/nokogiri-1.5.5/lib/nokogiri/xml/node_set.rb:238:in `each'
/home/coding/projects/idea_projects/tsp-bwks-mediation/app/jobs/webex_requests_job.rb:18:in `perform'
์ฃ์กํฉ๋๋ค. ๋ด ์ค์์ผ.
2๊ฐ์ ์์ฒญ์ ์ํํ๊ณ ๊ธฐ๋ณธ ์ด๋ํฐ๋ก ๋ ๋ฒ์งธ ์์ฒญ์ ์์ด๋ฒ๋ ธ์ต๋๋ค.
์ฅํฐํธ์ค๋ฅผ ์ฌ์ฉํ๋ฉด ์ด ์ค๋ฅ๊ฐ ๋ํ๋์ง ์์ต๋๋ค.
๊ทธ๋ฌ๋ net_http ์ด๋ํฐ๋ ์ด๋ป๊ฒ ํฉ๋๊น? ์ค๋ฅ: nil:NilClass์ ๋ํ ์ ์๋์ง ์์ ๋ฉ์๋ `strip' ?
์ฃ์กํฉ๋๋ค. ๋ต๋ณ์ ์์์ต๋๋ค. ์๋ง๋ nil์ธ ํค๋ ๊ฐ์ ์ ๋ฌํ๊ณ net_http๋ ํค๋ ๊ฐ์ ๋ฌธ์์ด๋ก ์ฒ๋ฆฌํ๋ ค๊ณ ํ์ต๋๋ค.
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ค๋ฉด ์ ๋ฌํ๋ ๋ชจ๋ ํค๋์ ์ค์ ๋ก ๋ฌธ์์ด ๊ฐ์ด ์๋์ง ํ์ธํ์ญ์์ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ฃ์กํฉ๋๋ค. ๋ต๋ณ์ ์์์ต๋๋ค. ์๋ง๋ nil์ธ ํค๋ ๊ฐ์ ์ ๋ฌํ๊ณ net_http๋ ํค๋ ๊ฐ์ ๋ฌธ์์ด๋ก ์ฒ๋ฆฌํ๋ ค๊ณ ํ์ต๋๋ค.
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ค๋ฉด ์ ๋ฌํ๋ ๋ชจ๋ ํค๋์ ์ค์ ๋ก ๋ฌธ์์ด ๊ฐ์ด ์๋์ง ํ์ธํ์ญ์์ค.