Faraday: Demandes de paralysie du middleware

Créé le 10 janv. 2015  ·  3Commentaires  ·  Source: lostisland/faraday

Sans middleware

Tout fonctionne comme prévu

[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=[]>

Avec le middleware Logger :

Ne fait probablement aucune réponse.

[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'

Avec le middleware de requête JSON

Ne fait aucune demande.

[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=[]>

Avec middleware de réponse JSON

[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'

Tous les 3 commentaires

Désolé pour la confusion. Lorsque vous passez un bloc ruby ​​pour configurer le middleware, vous devez également configurer explicitement l'adaptateur, sinon rien ne fonctionnera. https://github.com/lostisland/faraday/issues/325#issuecomment -30370061

@mislav Une bonne idée serait probablement de

@mislav une autre option consiste à lever une exception descriptive s'il n'y a pas d'adaptateur défini dans le bloc

Je suis d'accord que le comportement actuel est assez déroutant.

Cette page vous a été utile?
0 / 5 - 0 notes

Questions connexes

olleolleolle picture olleolleolle  ·  5Commentaires

jedeleh picture jedeleh  ·  3Commentaires

amrrbakry picture amrrbakry  ·  4Commentaires

jeffb-stell picture jeffb-stell  ·  5Commentaires

jordansissel picture jordansissel  ·  5Commentaires