module Newgistics
  class Api
    def get(request, response_handler)
      response = connection.get(request.path, request.body)
      response_handler.handle(response)
    end

    def post(request, response_handler)
      response = connection.post(request.path, request.body)
      response_handler.handle(response)
    end

    private

    def connection
      @connection ||= Faraday.new(url: host_url) do |faraday|
        faraday.response :logger, Newgistics.logger, bodies: true
      end
    end

    def host_url
      Newgistics.configuration.host_url
    end
  end
end

module Newgistics
  class Api
    def get(request, response_handler)
      response = connection.get(request.path, request.body)
      response_handler.handle(response)
    end

    def post(request, response_handler)
      response = connection.post(request.path, request.body)
      response_handler.handle(response)
    end

    private

    def connection
      @connection ||= Faraday.new(url: host_url) do |faraday|
        faraday.response :logger, Newgistics.logger, bodies: true
        faraday.adapter Faraday.default_adapter
      end
    end

    def host_url
      Newgistics.configuration.host_url
    end
  end
end

Make sure we use an adapter for our faraday connection
Make sure we use an adapter for our faraday connection

When adding middleware to the faraday connection we need to
be explicit about which adapter we'll use, otherwise no HTTP
requests are actually made.

Sadly, Faraday doesn't raise an exception if the adapter is
missing and you end up debugging cryptic error messages raised
from the middleware :panda_face:

For more information see:
https://github.com/lostisland/faraday/issues/317
