我在我的rails应用程序中使用Balanced ruby gem进行付款集成.
我正在提交卡信息并获得有效回复.然后我将卡信息发送给我的控制器,并且我正在创建买家card_uri.
buyer = Balanced::Marketplace.my_marketplace.create_buyer(@member.email, card_uri)
Run Code Online (Sandbox Code Playgroud)
但是,我收到了这个错误:
Faraday::Error::TimeoutError (execution expired):
Run Code Online (Sandbox Code Playgroud)
谁能告诉我什么是错的?提前致谢.
我有一个看起来像这样的模型:
class Gist
def self.create(options)
post_response = Faraday.post do |request|
request.url 'https://api.github.com/gists'
request.headers['Authorization'] = "Basic " + Base64.encode64("#{GITHUB_USERNAME}:#{GITHUB_PASSWORD}")
request.body = options.to_json
end
end
end
Run Code Online (Sandbox Code Playgroud)
和一个看起来像这样的测试:
require 'spec_helper'
describe Gist do
context '.create' do
it 'POSTs a new Gist to the user\'s account' do
Faraday.should_receive(:post)
Gist.create({:public => 'true',
:description => 'a test gist',
'files' => {'test_file.rb' => 'puts "hello world!"'}})
end
end
end
Run Code Online (Sandbox Code Playgroud)
然而,这个测试并不能让我满意,因为我正在测试的是我正在使用法拉第进行一些POST,但我实际上无法测试URL,标题或正文,因为它们已被传入一个街区.我尝试使用法拉第测试适配器,但我也没有看到任何测试URL,标题或正文的方法.
有没有更好的方法来编写我的Rspec存根?或者我能以某种方式使用法拉第测试适配器我无法理解?
谢谢!
我正在使用Oauth2 Gem连接到服务。
我可以收到授权代码,但是当我使用该代码检索用户令牌时,会得到Faraday :: ConnectionFailed(到达文件末尾):
其他大多数问题都归因于过时的证书。但是,当我部署到Heroku时,此错误仍然存在。
client = OAuth2::Client.new(client_id, client_secret, :site => 'https://auth.mxit.com', :authorize_url => '/authorize', :token_url => '/token')
auth_code1 = client.auth_code.authorize_url(:redirect_uri => root_url+'oauth2/callback', :scope => 'message/send')
auth_code1 =params[:code]
base_code = Base64.encode64(client_id+' : '+client_secret)
token = client.auth_code.get_token(auth_code1, :redirect_uri => root_url+'oauth2', :grant_type =>'authorization_code', :headers => {'Content-Type' => 'application/x-www-form-urlencoded','Authorization' => 'Basic'+base_code })
Run Code Online (Sandbox Code Playgroud) #<Faraday::ConnectionFailed> Connection refused - connect(2)我在使用复选框选择项目users/edit.html.erb并按下保存后收到消息。我选择的项目位于名为 的数组中amenities_list。这些项目是布尔值。尝试保存我收到的更改后#<Faraday::ConnectionFailed>。
用户/edit.html.erb
<div class="tab-content group">
<% amenities_list = User.amenities_list %>
<ul>
<% @user.boolean_array_from_amenities_integer.each_with_index do |amenity_available,index| %>
<% if amenity_available %>
<li class="available-amenity">
<% else %>
<li class="unavailable-amenity">
<% end %>
<%= amenities_list[index] %>
</li>
<% end %>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
用户控制器.rb
def new
@user = User.new
end
def create
@user = User.new(params[:user])
@user.id = current_user.id
@user.set_amenities_from_options_list!(params[:user_amenities_indicies])
if @user.save
flash[:success] = "Thank you for signing up! A confirmation email has …Run Code Online (Sandbox Code Playgroud) 我用来faraday处理一些对内部 API 的请求。API 端点使用 CSRF 令牌,因此我也使用faraday-cookie_jar.
对于一些 API 端点,它们需要一个:multipart请求。其他人则不然。
有没有办法利用同一个Connection对象,但无论你是否正在执行一个:multipart或一个:url_encoded请求,都可以切换?
目前,我必须使用两个连接,具体取决于我发出的请求类型。在连接完成至少 1 个请求后,您似乎无法更改连接的请求方法。
@connection = Faraday.new(url: 'http://localhost:3000') do |faraday|
faraday.request :url_encoded
faraday.use :cookie_jar
faraday.adapter Faraday.default_adapter
end
@connection.get '/something'
# try to change to :multipart
@connection.request :multipart # => Faraday::RackBuilder::StackLocked: can't modify middleware stack after making a request
Run Code Online (Sandbox Code Playgroud)
它似乎不允许您在提出请求后进行切换。我知道您可以通过传递一个块来对每个请求本身的请求进行一些修改,但我似乎找不到:multipart在其中修改以切换到的位置。
提前致谢。
我使用带有net-http-persistent适配器的Faraday来发出 HTTP 请求。
我想通过使请求异步执行来优化它们,但由于我需要持久连接,所以我不断收到错误,例如too many connections reset我认为这是由于我有多个线程创建新连接的事实。
我尝试将适配器更改为tyhoeus,但由于连接不是持久的,因此执行所有请求的最终结果并不符合预期。
我的目标是通过发出此 HTTP 请求将商品添加到购物篮中。如果没有持久连接,项目不会添加到购物篮中。
所以,我的问题是:
这是我的一段代码:
创建连接:
Faraday.new do |c|
c.use :cookie_jar, jar: cookie_jar
c.options.open_timeout = 5
c.options.timeout = 10
c.request :url_encoded
c.response :logger, logger
c.adapter :net_http_persistent do |http| # yields Net::HTTP::Persistent
http.idle_timeout = 2
end
end
Run Code Online (Sandbox Code Playgroud)
创建线程并获取每个线程的结果
result = []
threads = []
total_items = items.size
items.each_slice(5) do |sliced_items|
# Create a thread for a batch of 5 items and …Run Code Online (Sandbox Code Playgroud) 我想向 API 发送 POST 请求。这是我的代码:
conn = Faraday.new(url: BASE) do |faraday|
faraday.response :logger
faraday.request :url_encoded
faraday.headers['Content-Type'] = 'application/json'
faraday.headers["Authorization"] = "bearer #{AppConfig.instance.access_token}"
faraday.adapter Faraday.default_adapter
end
form_data = {a: 1, b: 2}
conn.post("/api/test", form_data)
Run Code Online (Sandbox Code Playgroud)
但我收到这个错误:
conn = Faraday.new(url: BASE) do |faraday|
faraday.response :logger
faraday.request :url_encoded
faraday.headers['Content-Type'] = 'application/json'
faraday.headers["Authorization"] = "bearer #{AppConfig.instance.access_token}"
faraday.adapter Faraday.default_adapter
end
form_data = {a: 1, b: 2}
conn.post("/api/test", form_data)
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
conn.post("/api/test", form_data.to_json)
Run Code Online (Sandbox Code Playgroud)
但它也不起作用。
我采用了一些代码,其中内嵌了 5 或 6 个法拉第请求,这是我第一次使用它。我正在抽象为一个模块。这是我第一次使用这个库,目前我有:
module CoreService
def self.net_connection param_url
conn = Faraday.new(:url => param_url ) do |c|
c.use Faraday::Request::UrlEncoded # encode request params as "www-form-urlencoded"
c.use Faraday::Response::Logger # log request & response to STDOUT
c.use Faraday::Adapter::NetHttp # perform requests with Net::HTTP
end
return conn
end
def self.success? arg
if [200,302].include? arg.status
return true
else
return false
end
end
end
Run Code Online (Sandbox Code Playgroud)
并会使用类似:
conn=CoreService.net_connection SOME::URL
url_val = "/other/things"
response = conn.get url_val
if CoreService.success? response
puts "THAT WAS A SUCCESS"
else
puts …Run Code Online (Sandbox Code Playgroud) 我有一个这样的方法:
def make_request(path, params, body)
raise ArgumentError.new('Endpoint not set!') if url.nil?
conditions = {url: url}
conditions[:params] = params unless params.blank?
connection = Faraday::Connection.new(conditions)
connection.run_request(:get, path, body, {'Content-Type' => 'application/json'})
end
Run Code Online (Sandbox Code Playgroud)
那我该如何添加 keep-alive 呢?另外,由于我每次调用此方法时都会实例化一个连接对象(url 可能不同),所以 keep-alive 参数仍然有效吗?
我正在使用 Circle-Ci 和 Firebase App Distribution 进行持续分发直到今天,每件事都运行良好,我收到以下错误:
[10:52:11]: --- Step: firebase_app_distribution ---
[10:52:11]: ---------------------------------------
[10:52:11]: Authenticating with FIREBASE_TOKEN environment variable
[10:52:12]: Authenticated successfully.
[10:52:12]: ? Uploading the IPA.
[10:52:13]: An error occurred while executing the `error` block:
[10:52:19]: undefined method `error_info' for #<Faraday::ForbiddenError:0x00007fe597865d38>
bundler: failed to load command: fastlane (/Users/distiller/project/vendor/bundle/ruby/2.7.0/bin/fastlane)
Faraday::ForbiddenError: [!] the server responded with status 403
/Users/distiller/project/vendor/bundle/ruby/2.7.0/gems/faraday-1.3.0/lib/faraday/response/raise_error.rb:20:in `on_complete'
/Users/distiller/project/vendor/bundle/ruby/2.7.0/gems/faraday-1.3.0/lib/faraday/middleware.rb:19:in `block in call'
/Users/distiller/project/vendor/bundle/ruby/2.7.0/gems/faraday-1.3.0/lib/faraday/response.rb:59:in `on_complete'
/Users/distiller/project/vendor/bundle/ruby/2.7.0/gems/faraday-1.3.0/lib/faraday/middleware.rb:18:in `call'
/Users/distiller/project/vendor/bundle/ruby/2.7.0/gems/faraday_middleware-1.0.0/lib/faraday_middleware/response_middleware.rb:36:in `call'
/Users/distiller/project/vendor/bundle/ruby/2.7.0/gems/faraday-1.3.0/lib/faraday/rack_builder.rb:154:in `build_response'
/Users/distiller/project/vendor/bundle/ruby/2.7.0/gems/faraday-1.3.0/lib/faraday/connection.rb:492:in `run_request'
/Users/distiller/project/vendor/bundle/ruby/2.7.0/gems/faraday-1.3.0/lib/faraday/connection.rb:279:in `post'
/Users/distiller/project/vendor/bundle/ruby/2.7.0/gems/fastlane-plugin-firebase_app_distribution-0.2.5/lib/fastlane/plugin/firebase_app_distribution/client/firebase_app_distribution_api_client.rb:123:in `upload_binary'
/Users/distiller/project/vendor/bundle/ruby/2.7.0/gems/fastlane-plugin-firebase_app_distribution-0.2.5/lib/fastlane/plugin/firebase_app_distribution/client/firebase_app_distribution_api_client.rb:153:in `upload' …Run Code Online (Sandbox Code Playgroud)