0

I'm refactoring some code in a Rails app consisting of several microservices. The faraday_middleware gem is used for communication between services.

I managed to replace several calls to Faraday::new() in different helper files with one single call to Faraday::new() in a ServiceConnectionHelper module. All of these replaced calls had an url parameter: Faraday.new(url: url)

But there's two very similar pieces of code left that I'd like to get rid of. In these cases, there is no url parameter. This is the old (working) code:

# 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

This is the code I want to use instead. It's not working because of an error inside the create function. When I catch it and log it, e.inspect is just #<UploadError: Please specify a file>

# 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

What can I do to make the ServiceConnectionHelper work in this case?

Pida
  • 928
  • 9
  • 32

1 Answers1

1

Compared of your first example, the order of request changed:

conn.request :url_encoded
conn.request :multipart