Ruby on rails 带Faraday的Rails应用程序:在没有url参数的情况下重构Faraday::new()时出现问题
我正在重构由几个微服务组成的Rails应用程序中的一些代码。faraday_中间件gem用于服务之间的通信 我设法用ServiceConnectionHelper模块中对Faraday::new的单个调用替换了不同帮助文件中对Faraday::new的几个调用。所有这些被替换的调用都有一个url参数:Faraday.newurl:url 但还有两段非常相似的代码我想去掉。在这些情况下,没有url参数。这是旧的工作代码:Ruby on rails 带Faraday的Rails应用程序:在没有url参数的情况下重构Faraday::new()时出现问题,ruby-on-rails,ruby,microservices,faraday,Ruby On Rails,Ruby,Microservices,Faraday,我正在重构由几个微服务组成的Rails应用程序中的一些代码。faraday_中间件gem用于服务之间的通信 我设法用ServiceConnectionHelper模块中对Faraday::new的单个调用替换了不同帮助文件中对Faraday::new的几个调用。所有这些被替换的调用都有一个url参数:Faraday.newurl:url 但还有两段非常相似的代码我想去掉。在这些情况下,没有url参数。这是旧的工作代码: # This code calls the connection funct
# This code calls the connection function below
def create(resource)
params = {
resource_id: resource.to_param,
version: resource.version,
file: Faraday::UploadIO.new(resource.file.path, resource.mime_type.to_s, resource.file.original_filename)
}
res = connection(resource.authorization).post(foobar_url, params)
return res.body['id'] if [200, 201].include?(res.status)
raise UploadError, res.body['error']
end
# connection function
def connection(authorization_header = nil)
Faraday.new do |conn|
conn.use FaradayMiddleware::FollowRedirects, limit: 5
conn.request :multipart
conn.request :url_encoded
conn.use FaradayMiddleware::ParseJson, content_type: 'application/json'
conn.adapter Faraday.default_adapter
conn.headers['Accept'] = 'application/json'
conn.headers['Authorization'] = authorization_header unless authorization_header.nil?
end
end
这就是我想使用的代码。由于create函数中的错误,它无法工作。当我捕捉并记录它时,e.inspect只是
在这种情况下,如何使ServiceConnectionHelper正常工作?与第一个示例相比,请求顺序发生了变化: 连接请求:url\u编码 控制请求:多部分
您可以尝试远程conn.request:url\u编码,或者像第一个示例中那样翻转顺序。远射,但似乎是唯一的区别。谢谢,成功了!当我将对Faraday.new的多个调用重构为单个调用时,我必须更改顺序。将其添加为应答,如果它解决了您的问题,您能接受吗。
# Small change only: Te other service's url is computed in the ServiceConnectionHelper module
def create(resource)
params = {
resource_id: resource.to_param,
version: resource.version,
file: Faraday::UploadIO.new(resource.file.path, resource.mime_type.to_s, resource.file.original_filename)
}
# This is were the error happens
res = connection(resource.authorization).post('/', params)
return res.body['id'] if [200, 201].include?(res.status)
raise UploadError, res.body['error']
end
# connection function calls the new helper module now
def connection(authorization_header = nil)
ServiceConnectionHelper.connection('foobar', authorization_header)
end
# the new module
module ServiceConnectionHelper
class << self
def connection(service, oauth_token = nil)
url = service_url(service)
Faraday.new(url: url) do |conn|
conn.use FaradayMiddleware::FollowRedirects, limit: 5
conn.request :url_encoded
conn.adapter Faraday.default_adapter
conn.request :multipart
conn.use FaradayMiddleware::ParseJson, content_type: 'application/json'
conn.headers['Accept'] = 'application/json'
conn.headers['Authorization'] = oauth_token if oauth_token
end
end
private
def service_url(service)
url = case service
when 'foobar' then 'foobar_url_as_a_string'
# the same for other services
end
url
end
end
end