Jesus / dropbox_api

Ruby client library for Dropbox API v2
MIT License
171 stars 113 forks source link

Allow for custom Faraday middleware #32

Closed Aupajo closed 7 years ago

Aupajo commented 7 years ago

Addresses #29.

Introduces a method called middleware on the Client, that can be used like this:

client = DropboxApi::Client.new

client.middleware.prepend do |connection|
  connection.request LoggingMiddleware
end

client.middleware.append do |connection|
  connection.use InstrumentationMiddleware
end

The order of middleware is important, so prepend will let you set up the connection before DropboxApi modifies it (via an endpoint's builder.build call – usually in an initializer) and append will let you alter the connection after DropboxApi modifies it.

As the adapter must be the last item of middleware, there's one extra method on the middleware object, adapter=:

# Use the Excon gem instead of Net::HTTP to make requests
client.middleware.adapter = :excon
Jesus commented 7 years ago

Thanks for the good work! I'll review and merge as soon as I have a chance.

Aupajo commented 7 years ago

@Jesus Thanks for your suggestion! I've updated this PR to move middleware as a method on ConnectionBuilder.