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

Cache Faraday::Connection for persistent adapters #322

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 0.14.7 (Next)

* [#322](https://github.com/slack-ruby/slack-ruby-client/pull/322): Cache Faraday::Connection for persistent adapters - [@drbrain](https://github.com/drbrain).
* Your contribution here.

### 0.14.6 (2020/3/28)
Expand Down
43 changes: 23 additions & 20 deletions lib/slack/web/faraday/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,32 @@ module Connection
private

def connection
options = {
headers: { 'Accept' => 'application/json; charset=utf-8' }
}
@connection ||=
begin
options = {
headers: { 'Accept' => 'application/json; charset=utf-8' }
}

options[:headers]['User-Agent'] = user_agent if user_agent
options[:proxy] = proxy if proxy
options[:ssl] = { ca_path: ca_path, ca_file: ca_file } if ca_path || ca_file
options[:headers]['User-Agent'] = user_agent if user_agent
options[:proxy] = proxy if proxy
options[:ssl] = { ca_path: ca_path, ca_file: ca_file } if ca_path || ca_file

request_options = {}
request_options[:timeout] = timeout if timeout
request_options[:open_timeout] = open_timeout if open_timeout
options[:request] = request_options if request_options.any?
request_options = {}
request_options[:timeout] = timeout if timeout
request_options[:open_timeout] = open_timeout if open_timeout
options[:request] = request_options if request_options.any?

::Faraday::Connection.new(endpoint, options) do |connection|
connection.use ::Faraday::Request::Multipart
connection.use ::Faraday::Request::UrlEncoded
connection.use ::Faraday::Response::RaiseError
connection.use ::Slack::Web::Faraday::Response::RaiseError
connection.use ::FaradayMiddleware::Mashify, mash_class: Slack::Messages::Message
connection.use ::FaradayMiddleware::ParseJson
connection.response :logger, logger if logger
connection.adapter ::Faraday.default_adapter
end
::Faraday::Connection.new(endpoint, options) do |connection|
connection.use ::Faraday::Request::Multipart
connection.use ::Faraday::Request::UrlEncoded
connection.use ::Faraday::Response::RaiseError
connection.use ::Slack::Web::Faraday::Response::RaiseError
connection.use ::FaradayMiddleware::Mashify, mash_class: Slack::Messages::Message
connection.use ::FaradayMiddleware::ParseJson
connection.response :logger, logger if logger
connection.adapter ::Faraday.default_adapter
end
end
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/slack/web/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,16 @@
client.users_admin_setInactive(user: 'U092BDCLV')
end
end

context 'persistent capability' do
describe '#initialize' do
it 'caches the Farady connection to allow persistent adapters' do
first = client.send(:connection)
second = client.send(:connection)

expect(first).to equal second
end
end
end
end
end