diff --git a/README.md b/README.md index e6ff597..de3fb07 100644 --- a/README.md +++ b/README.md @@ -111,9 +111,9 @@ session = client.session('session_id') # Send a message to the session response = session.send_message('How do I start?') -# Upload files to the session -files = [File.open('path/to/file.txt')] -upload_response = session.upload_files(files) +# Upload file +file = File.open('path/to/file.txt') +upload_response = client.attachment.upload_file(file) # Update session tags session.update_tags(['web', 'app']) diff --git a/lib/devin_api/client.rb b/lib/devin_api/client.rb index 4bc03f0..4d2f917 100644 --- a/lib/devin_api/client.rb +++ b/lib/devin_api/client.rb @@ -53,10 +53,16 @@ def get(path, params = {}) # Executes a POST request # @param [String] path The path to request # @param [Hash] params Body parameters - # @return [Hash] Response body + # @return [Hash, String] Response body def post(path, params = {}) response = connection.post(path) do |req| - req.body = params.to_json + if params.values.any? { |v| v.is_a?(Faraday::UploadIO) } + req.headers['Content-Type'] = 'multipart/form-data' + req.body = params + else + req.headers['Content-Type'] = 'application/json' + req.body = params.to_json + end end parse_response(response) end @@ -93,8 +99,8 @@ def build_connection conn.response :json, content_type: /\bjson$/ # Request middlewares - conn.request :json conn.request :multipart + conn.request :json # Authentication conn.headers['Authorization'] = "Bearer #{config.access_token}" diff --git a/lib/devin_api/endpoints/attachment.rb b/lib/devin_api/endpoints/attachment.rb index 0a603c2..76adc5f 100644 --- a/lib/devin_api/endpoints/attachment.rb +++ b/lib/devin_api/endpoints/attachment.rb @@ -8,13 +8,9 @@ module Attachment # @see https://docs.devin.ai/api-reference/attachments/upload-files-for-devin-to-work-with # # @param [File] file File object to upload - # @return [Hash] Response body + # @return [String] URL where the uploaded file can be accessed def upload_file(file) - payload = Faraday::UploadIO.new( - file.path, - file.content_type || 'application/octet-stream', - File.basename(file.path) - ) + payload = { file: Faraday::UploadIO.new(file.path, 'application/octet-stream', File.basename(file.path)) } connection.post('/v1/attachments', payload).body end diff --git a/lib/devin_api/resources/attachment.rb b/lib/devin_api/resources/attachment.rb index 89d3a42..30b7daa 100644 --- a/lib/devin_api/resources/attachment.rb +++ b/lib/devin_api/resources/attachment.rb @@ -10,12 +10,13 @@ def path '/v1/attachments' end + # Upload a file for Devin to work with during sessions + # @see https://docs.devin.ai/api-reference/attachments/upload-files-for-devin-to-work-with + # + # @param [File] file File object to upload + # @return [String] URL where the uploaded file can be accessed def upload_file(file) - payload = Faraday::UploadIO.new( - file.path, - file.content_type || 'application/octet-stream', - File.basename(file.path) - ) + payload = { file: Faraday::UploadIO.new(file.path, 'application/octet-stream', File.basename(file.path)) } client.connection.post(path, payload).body end diff --git a/spec/core/endpoints/attachment_spec.rb b/spec/core/endpoints/attachment_spec.rb index d20b252..0709cd1 100644 --- a/spec/core/endpoints/attachment_spec.rb +++ b/spec/core/endpoints/attachment_spec.rb @@ -6,23 +6,23 @@ subject { client } describe '#upload_file' do - let(:file) { double('File', path: '/tmp/test.txt', content_type: 'text/plain') } + let(:file) { double('File', path: '/tmp/test.txt') } + let(:upload_io) { double('Faraday::UploadIO') } before do allow(File).to receive(:basename).with('/tmp/test.txt').and_return('test.txt') - allow(Faraday::UploadIO).to receive(:new).with('/tmp/test.txt', 'text/plain', 'test.txt') - .and_return('file_content') + allow(Faraday::UploadIO).to receive(:new) + .with('/tmp/test.txt', 'application/octet-stream', 'test.txt') + .and_return(upload_io) - allow(subject.connection).to receive(:post).with('/v1/attachments', 'file_content').and_return( - double('Response', body: { 'file_id' => 'file-123', 'filename' => 'test.txt' }) - ) + allow(subject.connection).to receive(:post) + .with('/v1/attachments', { file: upload_io }) + .and_return(double('Response', body: 'https://example.com/file-123')) end it 'should upload a file' do response = subject.upload_file(file) - expect(response).to be_a(Hash) - expect(response['file_id']).to eq('file-123') - expect(response['filename']).to eq('test.txt') + expect(response).to eq('https://example.com/file-123') end end end diff --git a/spec/core/resources/attachment_spec.rb b/spec/core/resources/attachment_spec.rb index a58ee37..113a346 100644 --- a/spec/core/resources/attachment_spec.rb +++ b/spec/core/resources/attachment_spec.rb @@ -24,23 +24,23 @@ end describe '#upload_file' do - let(:file) { double('File', path: '/tmp/test.txt', content_type: 'text/plain') } + let(:file) { double('File', path: '/tmp/test.txt') } + let(:upload_io) { double('Faraday::UploadIO') } before do allow(File).to receive(:basename).with('/tmp/test.txt').and_return('test.txt') - allow(Faraday::UploadIO).to receive(:new).with('/tmp/test.txt', 'text/plain', 'test.txt') - .and_return('file_content') + allow(Faraday::UploadIO).to receive(:new) + .with('/tmp/test.txt', 'application/octet-stream', 'test.txt') + .and_return(upload_io) - allow(client.connection).to receive(:post).with('/v1/attachments', 'file_content').and_return( - double('Response', body: { 'id' => 'file-123', 'filename' => 'test.txt' }) - ) + allow(client.connection).to receive(:post) + .with('/v1/attachments', { file: upload_io }) + .and_return(double('Response', body: 'https://example.com/file-123')) end it 'should upload a file' do response = subject.upload_file(file) - expect(response).to be_a(Hash) - expect(response['id']).to eq('file-123') - expect(response['filename']).to eq('test.txt') + expect(response).to eq('https://example.com/file-123') end end end