Skip to content

Commit 2220c1a

Browse files
committed
file upload v2
1 parent c75d8e4 commit 2220c1a

File tree

4 files changed

+100
-2
lines changed

4 files changed

+100
-2
lines changed

lib/slack/web/api/endpoints/files.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,71 @@ def files_sharedPublicURL(options = {})
183183
def files_upload(options = {})
184184
post('files.upload', options)
185185
end
186+
187+
#
188+
# Uploads or creates a file (V2).
189+
#
190+
# @option options [string] :content
191+
# File contents via a POST variable. If omitting this parameter, you must provide a file.
192+
# @option options [file] :file
193+
# File contents via multipart/form-data. If omitting this parameter, you must submit content.
194+
#
195+
# params for files_getUploadURLExternal:
196+
#
197+
# @option options [string] :filename
198+
# Name of the file being uploaded.
199+
# @option options [string] :alt_txt
200+
# Description of image for screen-reader.
201+
# @option options [string] :snippet_type
202+
# Syntax type of the snippet being uploaded.
203+
#
204+
# params for files_completeUploadExternal:
205+
#
206+
# @option options [Object] :channel_id
207+
# Channel ID where the file will be shared. If not specified the file will be private.
208+
# @option options [string] :initial_comment
209+
# The message text introducing the file in specified channels.
210+
# @option options [string] :thread_ts
211+
# Provide another message's ts value to upload this file as a reply. Never use a reply's ts value; use its parent instead.
212+
#
213+
# @see https://api.slack.com/messaging/files#uploading_files
214+
# @see https://github.com/slackapi/node-slack-sdk/blob/96d846a79878f0d33893d14a9942ed9c4f678f2d/packages/web-api/src/WebClient.ts#L495
215+
# @see https://github.com/slackapi/python-slack-sdk/blob/a7223d9852de8a4ef9156552d7c50a92ec92669e/slack_sdk/web/client.py#L3737
216+
def files_upload_v2(options = {})
217+
file = ::Faraday::UploadIO.new(StringIO.new(options[:content]), 'text/plain') if options[:content]
218+
file ||= options[:file]
219+
if file.nil?
220+
raise(
221+
ArgumentError, 'Either a file or content field is required for valid file upload. You must supply one'
222+
)
223+
end
224+
# step 1: get upload url
225+
response = files_getUploadURLExternal(
226+
length: file.size, **options.slice(:filename, :length, :alt_txt, :snippet_type)
227+
)
228+
upload_url = response[:upload_url]
229+
file_id = response[:file_id]
230+
231+
file = ::Faraday::UploadIO.new(StringIO.new(options[:content]), 'text/plain') if options[:content]
232+
file ||= options[:file]
233+
if file.nil?
234+
raise(
235+
ArgumentError, 'Either a file or content field is required for valid file upload. You must supply one'
236+
)
237+
end
238+
239+
# step 2: upload file
240+
post(
241+
upload_url,
242+
{ file: file }
243+
)
244+
245+
# step 3: complete upload
246+
files_completeUploadExternal(
247+
files: [{ id: file_id, title: options[:filename] }].to_json,
248+
**options.slice(:channel_id, :initial_comment, :thread_ts)
249+
)
250+
end
186251
end
187252
end
188253
end

lib/slack/web/faraday/connection.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def connection
99
@connection ||=
1010
begin
1111
options = {
12-
headers: { 'Accept' => 'application/json; charset=utf-8' }
12+
headers: { 'Accept' => 'application/json, text/plain; charset=utf-8' }
1313
}
1414

1515
options[:headers]['User-Agent'] = user_agent if user_agent
@@ -26,7 +26,7 @@ def connection
2626
connection.request :url_encoded
2727
connection.use ::Slack::Web::Faraday::Response::RaiseError
2828
connection.response :mashify, mash_class: Slack::Messages::Message
29-
connection.response :json, content_type: /\b*$/
29+
connection.response :json, content_type: /\b*json$/
3030
connection.use ::Slack::Web::Faraday::Response::WrapError
3131
connection.response :logger, logger if logger
3232
connection.adapter adapter

lib/slack/web/faraday/response/raise_error.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ class RaiseError < ::Faraday::Middleware
77
def on_complete(env)
88
raise Slack::Web::Api::Errors::TooManyRequestsError, env.response if env.status == 429
99

10+
if env.body.is_a?(String)
11+
handle_non_json_response(env)
12+
else
13+
handle_json_response(env)
14+
end
15+
end
16+
17+
private
18+
19+
def handle_non_json_response(env)
20+
raise Slack::Web::Api::Errors::ServerError.new('request failed!', env.response) unless env.success?
21+
end
22+
23+
def handle_json_response(env)
1024
return unless env.success?
1125

1226
body = env.body

spec/slack/web/faraday/response/raise_error_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,24 @@
8282
)
8383
end
8484
end
85+
86+
context 'with a non-JSON 200 response' do
87+
let(:body) { 'OK - 9' }
88+
89+
it 'raises a ServerError' do
90+
expect(raise_error_obj.on_complete(env)).to be_nil
91+
end
92+
end
93+
94+
context 'with a non-JSON failed response' do
95+
let(:body) { 'not json' }
96+
let(:env) { double status: status, response: response, body: body, success?: false }
97+
98+
it 'raises a ServerError' do
99+
expect { raise_error_obj.on_complete(env) }.to(
100+
raise_error(Slack::Web::Api::Errors::ServerError, 'request failed!')
101+
)
102+
end
103+
end
85104
end
86105
end

0 commit comments

Comments
 (0)