Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for setting headers and overriding or extending the default Faraday connection block before a connection is constructed. #75

Merged
merged 1 commit into from
Oct 16, 2014

Conversation

dblock
Copy link
Collaborator

@dblock dblock commented Oct 16, 2014

From the updated README:

By default, Hyperclient adds application/json as Content-Type and Accept headers. It will also send requests as JSON and parse JSON responses. Specify additional headers or authentication if necessary.

api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api') do |client|
  client.headers['Access-Token'] = 'token' # this is new, you can access headers before the connection is built
end

By default, Hyperclient constructs a connection using typical Faraday middleware for handling JSON requests and responses. You can specify additional Faraday middleware if necessary.

api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api') do |client|
  client.connection do |conn|
    conn.use Faraday::Request::OAuth # this is new, you can add middleware to the default Faraday connection block
  end
end

You can build a new Faraday connection block without inheriting default middleware by specifying default: false in the connection block.

api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api') do |client|
  # this is new, you can just say default: false and not inherit anything, this also lets us specify other options in the future
  client.connection(default: false) do |conn|
    conn.request :json
    conn.response :json, content_type: /\bjson$/
    conn.adapter :net_http
  end
end

You can modify headers or specify authentication after a connection has been created. Hyperclient supports Basic, Token or Digest auth as well as many other Faraday extensions.

api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api')
api.digest_auth('username', 'password')
api.headers.update('Accept-Encoding' => 'deflate, gzip')

You can access the Faraday connection directly after it has been created and add middleware to itpoint. As an example, you could use the faraday-http-cache-middleware.

api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api')
api.connection.use :http_cache

@dblock dblock force-pushed the faraday-block branch 4 times, most recently from 1a7384e to 6c62045 Compare October 16, 2014 13:17
…ault Faraday connection block before a connection is constructed.
dblock added a commit that referenced this pull request Oct 16, 2014
Added support for setting headers and overriding or extending the default Faraday connection block before a connection is constructed.
@dblock dblock merged commit 4350988 into codegram:master Oct 16, 2014
@dblock dblock deleted the faraday-block branch October 16, 2014 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant