Skip to content

Commit 6792106

Browse files
committed
add a connection with no json response parsing
1 parent d635678 commit 6792106

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

lib/slack/web/faraday/connection.rb

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,44 @@ module Slack
33
module Web
44
module Faraday
55
module Connection
6-
private
6+
def default_options
7+
options = {
8+
headers: { 'Accept' => 'application/json; charset=utf-8' }
9+
}
10+
options[:headers]['User-Agent'] = user_agent if user_agent
11+
options[:proxy] = proxy if proxy
12+
options[:ssl] = { ca_path: ca_path, ca_file: ca_file } if ca_path || ca_file
713

8-
def connection
9-
@connection ||=
10-
begin
11-
options = {
12-
headers: { 'Accept' => 'application/json; charset=utf-8' }
13-
}
14+
request_options = {}
15+
request_options[:timeout] = timeout if timeout
16+
request_options[:open_timeout] = open_timeout if open_timeout
17+
options[:request] = request_options if request_options.any?
18+
options
19+
end
1420

15-
options[:headers]['User-Agent'] = user_agent if user_agent
16-
options[:proxy] = proxy if proxy
17-
options[:ssl] = { ca_path: ca_path, ca_file: ca_file } if ca_path || ca_file
21+
def create_connection(expect_json_response: true)
22+
options = default_options
23+
::Faraday::Connection.new(endpoint, options) do |connection|
24+
connection.request :multipart
25+
connection.request :url_encoded
26+
connection.use ::Slack::Web::Faraday::Response::RaiseError if expect_json_response
27+
connection.response :mashify, mash_class: Slack::Messages::Message
28+
connection.response :json, content_type: /\b*$/ if expect_json_response
29+
connection.use ::Slack::Web::Faraday::Response::WrapError
30+
connection.response :logger, logger if logger
31+
connection.adapter adapter
32+
end
33+
end
1834

19-
request_options = {}
20-
request_options[:timeout] = timeout if timeout
21-
request_options[:open_timeout] = open_timeout if open_timeout
22-
options[:request] = request_options if request_options.any?
35+
def connection
36+
@connection ||= create_connection
37+
end
2338

24-
::Faraday::Connection.new(endpoint, options) do |connection|
25-
connection.request :multipart
26-
connection.request :url_encoded
27-
connection.use ::Slack::Web::Faraday::Response::RaiseError
28-
connection.response :mashify, mash_class: Slack::Messages::Message
29-
connection.response :json, content_type: /\bjson$/
30-
connection.use ::Slack::Web::Faraday::Response::WrapError
31-
connection.response :logger, logger if logger
32-
connection.adapter adapter
33-
end
34-
end
39+
def connection_without_response_parsing
40+
@connection_without_response_parsing ||= create_connection(false)
3541
end
42+
43+
private :create_connection
3644
end
3745
end
3846
end

0 commit comments

Comments
 (0)