-
Notifications
You must be signed in to change notification settings - Fork 35
add faraday connection options without breaking api #80
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ def message | |
# client = Hyperclient::EntryPoint.new('http://my.api.org') | ||
# | ||
# client = Hyperclient::EntryPoint.new('http://my.api.org') do |entry_point| | ||
# entry_point.connection(default: true) do |conn| | ||
# entry_point.connection do |conn| | ||
# conn.use Faraday::Request::OAuth | ||
# end | ||
# entry_point.headers['Access-Token'] = 'token' | ||
|
@@ -48,22 +48,31 @@ def initialize(url, &_block) | |
# default - Set to true to reuse default Faraday connection options. | ||
# | ||
# Returns a Faraday::Connection. | ||
def connection(options = { default: true }, &block) | ||
def connection(options = {}, &block) | ||
@faraday_options ||= options.dup | ||
if block_given? | ||
fail ConnectionAlreadyInitializedError if @connection | ||
if options[:default] | ||
if @faraday_options.delete(:default) == false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you are going to |
||
@faraday_block = block | ||
else | ||
@faraday_block = lambda do |conn| | ||
default_faraday_block.call conn | ||
block.call conn | ||
end | ||
else | ||
@faraday_block = block | ||
end | ||
else | ||
@connection ||= Faraday.new(_url, { headers: headers }, &faraday_block) | ||
@connection ||= Faraday.new(_url, faraday_options, &faraday_block) | ||
end | ||
end | ||
|
||
# Public: Headers included with every API request. | ||
# | ||
# Returns a Hash. | ||
def headers | ||
return @connection.headers if @connection | ||
@headers ||= default_headers | ||
end | ||
|
||
# Public: Set headers. | ||
# | ||
# value - A Hash containing headers to include with every API request. | ||
|
@@ -72,12 +81,19 @@ def headers=(value) | |
@headers = value | ||
end | ||
|
||
# Public: Headers included with every API request. | ||
# Public: Options passed to Faraday | ||
# | ||
# Returns a Hash. | ||
def headers | ||
return @connection.headers if @connection | ||
@headers ||= default_headers | ||
def faraday_options | ||
(@faraday_options ||= {}).merge(headers: headers) | ||
end | ||
|
||
# Public: Set Faraday connection options. | ||
# | ||
# value - A Hash containing options to pass to Faraday | ||
def faraday_options=(value) | ||
fail ConnectionAlreadyInitializedError if @connection | ||
@faraday_options = value | ||
end | ||
|
||
# Public: Faraday block used with every API request. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, I usually just keep these things under Next. That's because there's no actual -pre release, so it may be confusing for someone reading it. It's no big deal.