Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware cripple requests #455

Closed
t3hk0d3 opened this issue Jan 10, 2015 · 3 comments
Closed

Middleware cripple requests #455

t3hk0d3 opened this issue Jan 10, 2015 · 3 comments

Comments

@t3hk0d3
Copy link

t3hk0d3 commented Jan 10, 2015

Without any middleware

Everything working as expected

[37] pry(main)>  p = Faraday.new("http://192.168.5.4:8112")
=> #<Faraday::Connection:0x007fd461259c88
 @builder=#<Faraday::RackBuilder:0x007fd4612598f0 @handlers=[Faraday::Request::UrlEncoded, Faraday::Adapter::NetHttp]>,
 @default_parallel_manager=nil,
 @headers={"User-Agent"=>"Faraday v0.9.1"},
 @options=#<struct Faraday::RequestOptions params_encoder=nil, proxy=nil, bind=nil, timeout=nil, open_timeout=nil, boundary=nil, oauth=nil>,
 @parallel_manager=nil,
 @params={},
 @proxy=nil,
 @ssl=
  #<struct Faraday::SSLOptions
   verify=nil,
   ca_file=nil,
   ca_path=nil,
   verify_mode=nil,
   cert_store=nil,
   client_cert=nil,
   client_key=nil,
   certificate=nil,
   private_key=nil,
   verify_depth=nil,
   version=nil>,
 @url_prefix=#<URI::HTTP:0x007fd461258c48 URL:http://192.168.5.4:8112/>>
[38] pry(main)> p.post '/json', "{\"id\": 0, \"method\":\"auth.login\", \"params\":[\"password\"]}"
=> #<Faraday::Response:0x007fd4611d3bd8
 @env=
  #<struct Faraday::Env
   method=:post,
   body="{\"id\": 0, \"result\": false, \"error\": null}",
   url=#<URI::HTTP:0x007fd4611e21d8 URL:http://192.168.5.4:8112/json>,
   request=
    #<struct Faraday::RequestOptions params_encoder=nil, proxy=nil, bind=nil, timeout=nil, open_timeout=nil, boundary=nil, oauth=nil>,
   request_headers={"User-Agent"=>"Faraday v0.9.1", "Content-Type"=>"application/x-www-form-urlencoded"},
   ssl=
    #<struct Faraday::SSLOptions
     verify=nil,
     ca_file=nil,
     ca_path=nil,
     verify_mode=nil,
     cert_store=nil,
     client_cert=nil,
     client_key=nil,
     certificate=nil,
     private_key=nil,
     verify_depth=nil,
     version=nil>,
   parallel_manager=nil,
   params=nil,
   response=nil,
   response_headers=
    {"transfer-encoding"=>"chunked",
     "date"=>"Sat, 10 Jan 2015 01:47:43 GMT",
     "connection"=>"close",
     "content-type"=>"application/x-json",
     "server"=>"TwistedWeb/12.0.0"},
   status=200>,
 @on_complete_callbacks=[]>

With Logger middleware:

Probably does not make any reponse. Crash on dumping headers (they are nil)

[39] pry(main)> p = Faraday.new("http://192.168.5.4:8112") { |f| f.response :logger }
=> #<Faraday::Connection:0x007fd460a44f98
 @builder=#<Faraday::RackBuilder:0x007fd460a44930 @handlers=[Faraday::Response::Logger]>,
 @default_parallel_manager=nil,
 @headers={"User-Agent"=>"Faraday v0.9.1"},
 @options=#<struct Faraday::RequestOptions params_encoder=nil, proxy=nil, bind=nil, timeout=nil, open_timeout=nil, boundary=nil, oauth=nil>,
 @parallel_manager=nil,
 @params={},
 @proxy=nil,
 @ssl=
  #<struct Faraday::SSLOptions
   verify=nil,
   ca_file=nil,
   ca_path=nil,
   verify_mode=nil,
   cert_store=nil,
   client_cert=nil,
   client_key=nil,
   certificate=nil,
   private_key=nil,
   verify_depth=nil,
   version=nil>,
 @url_prefix=#<URI::HTTP:0x007fd460a44520 URL:http://192.168.5.4:8112/>>
[40] pry(main)> p.post '/json', "{\"id\": 0, \"method\":\"auth.login\", \"params\":[\"password\"]}"
I, [2015-01-10T04:48:16.268374 #77388]  INFO -- : post http://192.168.5.4:8112/json
D, [2015-01-10T04:48:16.268467 #77388] DEBUG -- request: User-Agent: "Faraday v0.9.1"
I, [2015-01-10T04:48:16.268619 #77388]  INFO -- Status: 
NoMethodError: undefined method `map' for nil:NilClass
from /Users/tehkode/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/faraday-0.9.1/lib/faraday/response/logger.rb:36:in `dump_headers'

With JSON request middleware

Does not make any request. Return request as response

[41] pry(main)> p = Faraday.new("http://192.168.5.4:8112") { |f| f.request :json }
=> #<Faraday::Connection:0x007fd460c68e78
 @builder=#<Faraday::RackBuilder:0x007fd460c68a40 @handlers=[FaradayMiddleware::EncodeJson]>,
 @default_parallel_manager=nil,
 @headers={"User-Agent"=>"Faraday v0.9.1"},
 @options=#<struct Faraday::RequestOptions params_encoder=nil, proxy=nil, bind=nil, timeout=nil, open_timeout=nil, boundary=nil, oauth=nil>,
 @parallel_manager=nil,
 @params={},
 @proxy=nil,
 @ssl=
  #<struct Faraday::SSLOptions
   verify=nil,
   ca_file=nil,
   ca_path=nil,
   verify_mode=nil,
   cert_store=nil,
   client_cert=nil,
   client_key=nil,
   certificate=nil,
   private_key=nil,
   verify_depth=nil,
   version=nil>,
 @url_prefix=#<URI::HTTP:0x007fd460c68770 URL:http://192.168.5.4:8112/>>
[42] pry(main)> p.post '/json', "{\"id\": 0, \"method\":\"auth.login\", \"params\":[\"password\"]}"
=> #<Faraday::Response:0x007fd460b922b0
 @env=
  #<struct Faraday::Env
   method=:post,
   body="{\"id\": 0, \"method\":\"auth.login\", \"params\":[\"torrent\"]}",
   url=#<URI::HTTP:0x007fd460b92c38 URL:http://192.168.5.4:8112/json>,
   request=
    #<struct Faraday::RequestOptions params_encoder=nil, proxy=nil, bind=nil, timeout=nil, open_timeout=nil, boundary=nil, oauth=nil>,
   request_headers={"User-Agent"=>"Faraday v0.9.1", "Content-Type"=>"application/json"},
   ssl=
    #<struct Faraday::SSLOptions
     verify=nil,
     ca_file=nil,
     ca_path=nil,
     verify_mode=nil,
     cert_store=nil,
     client_cert=nil,
     client_key=nil,
     certificate=nil,
     private_key=nil,
     verify_depth=nil,
     version=nil>,
   parallel_manager=nil,
   params=nil,
   response=nil,
   response_headers=nil,
   status=nil>,
 @on_complete_callbacks=[]>

With JSON response middleware

[43] pry(main)> p = Faraday.new("http://192.168.5.4:8112") { |f| f.response :json }
=> #<Faraday::Connection:0x007fd4611eaae0
 @builder=#<Faraday::RackBuilder:0x007fd4611ea680 @handlers=[FaradayMiddleware::ParseJson]>,
 @default_parallel_manager=nil,
 @headers={"User-Agent"=>"Faraday v0.9.1"},
 @options=#<struct Faraday::RequestOptions params_encoder=nil, proxy=nil, bind=nil, timeout=nil, open_timeout=nil, boundary=nil, oauth=nil>,
 @parallel_manager=nil,
 @params={},
 @proxy=nil,
 @ssl=
  #<struct Faraday::SSLOptions
   verify=nil,
   ca_file=nil,
   ca_path=nil,
   verify_mode=nil,
   cert_store=nil,
   client_cert=nil,
   client_key=nil,
   certificate=nil,
   private_key=nil,
   verify_depth=nil,
   version=nil>,
 @url_prefix=#<URI::HTTP:0x007fd4611ea360 URL:http://192.168.5.4:8112/>>
[44] pry(main)> p.post '/json', "{\"id\": 0, \"method\":\"auth.login\", \"params\":[\"torrent\"]}"
NoMethodError: undefined method `[]' for nil:NilClass
from /Users/tehkode/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/faraday_middleware-0.9.1/lib/faraday_middleware/response_middleware.rb:59:in `response_type'
@mislav
Copy link
Contributor

mislav commented Jan 11, 2015

Sorry for the confustion. When you pass a ruby block to configure middleware, you have to explicitly also configure the adapter as well otherwise nothing will work. #325 (comment)

@mislav mislav closed this as completed Jan 11, 2015
@t3hk0d3
Copy link
Author

t3hk0d3 commented Jan 11, 2015

@mislav Probably good idea would be to fallback to default adapter if its not specified explicitly. Current behavior is very confusing.

@mraaroncruz
Copy link

@mislav another option is to raise a descriptive exception if there is no adapter set in the block

I agree that the current behavior is pretty confusing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants