-
Notifications
You must be signed in to change notification settings - Fork 35
Description
I'm trying to use faraday-http-cache with Hyperclient, but it isn't proving easy.
faraday-http-cache expects to sit before :net_http
in the middleware stack. A working example from their docs looks like this:
client = Faraday.new('https://api.github.com') do |stack|
stack.response :json, content_type: /\bjson$/
stack.use :http_cache, logger: ActiveSupport::Logger.new(STDOUT)
stack.adapter Faraday.default_adapter
end
# which has this middleware:
[FaradayMiddleware::ParseJson, Faraday::HttpCache, Faraday::Adapter::NetHttp]
However, trying to achieve the same thing in Hyperclient doesn't work as expected. The following setup, which I would expect to work, results in duplicate :net_http
in the middleware.
client = Hyperclient.new('https://api.github.com')
client.connection.use :http_cache, logger: ActiveSupport::Logger.new(STDOUT)
client.connection.adapter Faraday.default_adapter
# which has this middleware (see the duplicate NetHttp):
[FaradayMiddleware::EncodeJson, FaradayMiddleware::ParseJson, Faraday::Adapter::NetHttp, Faraday::HttpCache, Faraday::Adapter::NetHttp]
This is because of the way Hyperclient initialises the Faraday connection (see default_faraday_block) with the :net_http
adapter when it creates the connection.
The only way I've got this working was by monkey patching or subclassing EntryPoint and overriding the default_faraday_block
so the :http_cache
middleware is inserted before :net_http
.
class Hyperclient::EntryPoint
def default_faraday_block
lambda do |faraday|
faraday.request :json
faraday.response :json, content_type: /\bjson$/
faraday.use :http_cache, logger: ActiveSupport::Logger.new(STDOUT)
faraday.adapter :net_http
end
end
end
Advice? This is less than ideal.