Skip to content
Merged
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
12 changes: 9 additions & 3 deletions lib/devin_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"
Expand Down
8 changes: 2 additions & 6 deletions lib/devin_api/endpoints/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions lib/devin_api/resources/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions spec/core/endpoints/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 9 additions & 9 deletions spec/core/resources/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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