From 9f77979ba20b08bedcf1773cea933508266464a8 Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Thu, 29 Feb 2024 18:10:45 -0500 Subject: [PATCH 1/3] add support for json attachments --- lib/nylas/resources/drafts.rb | 27 +++++++++++++++++++++++---- lib/nylas/resources/messages.rb | 13 +++++++++++-- lib/nylas/utils/file_utils.rb | 3 +++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/lib/nylas/resources/drafts.rb b/lib/nylas/resources/drafts.rb index 72693788..4be5a0a8 100644 --- a/lib/nylas/resources/drafts.rb +++ b/lib/nylas/resources/drafts.rb @@ -43,10 +43,20 @@ def find(identifier:, draft_id:) # you can use {FileUtils::attach_file_request_builder} to build each object attach. # @return [Array(Hash, String)] The created draft and API Request ID. def create(identifier:, request_body:) - form_body, opened_files = FileUtils.build_form_request(request_body) + payload = request_body + opened_files = [] + + # Use form data only if the attachment size is greater than 3mb + attachments = request_body[:attachments] || request_body["attachments"] || [] + attachment_size = attachments&.sum { |attachment| attachment[:size] || 0 } || 0 + + if attachment_size >= FileUtils::FORM_DATA_ATTACHMENT_SIZE + payload, opened_files = FileUtils.build_form_request(request_body) + end + response = post( path: "#{api_uri}/v3/grants/#{identifier}/drafts", - request_body: form_body + request_body: payload ) opened_files.each(&:close) @@ -63,11 +73,20 @@ def create(identifier:, request_body:) # you can use {FileUtils::attach_file_request_builder} to build each object attach. # @return [Array(Hash, String)] The updated draft and API Request ID. def update(identifier:, draft_id:, request_body:) - form_body, opened_files = FileUtils.build_form_request(request_body) + payload = request_body + opened_files = [] + + # Use form data only if the attachment size is greater than 3mb + attachments = request_body[:attachments] || request_body["attachments"] || [] + attachment_size = attachments&.sum { |attachment| attachment[:size] || 0 } || 0 + + if attachment_size >= FileUtils::FORM_DATA_ATTACHMENT_SIZE + payload, opened_files = FileUtils.build_form_request(request_body) + end response = put( path: "#{api_uri}/v3/grants/#{identifier}/drafts/#{draft_id}", - request_body: form_body + request_body: payload ) opened_files.each(&:close) diff --git a/lib/nylas/resources/messages.rb b/lib/nylas/resources/messages.rb index 9530c6e0..4465e70b 100644 --- a/lib/nylas/resources/messages.rb +++ b/lib/nylas/resources/messages.rb @@ -79,11 +79,20 @@ def destroy(identifier:, message_id:) # you can use {FileUtils::attach_file_request_builder} to build each object attach. # @return [Array(Hash, String)] The sent message and the API Request ID. def send(identifier:, request_body:) - form_body, opened_files = FileUtils.build_form_request(request_body) + payload = request_body + opened_files = [] + + # Use form data only if the attachment size is greater than 3mb + attachments = request_body[:attachments] || request_body["attachments"] || [] + attachment_size = attachments&.sum { |attachment| attachment[:size] || 0 } || 0 + + if attachment_size >= FileUtils::FORM_DATA_ATTACHMENT_SIZE + payload, opened_files = FileUtils.build_form_request(request_body) + end response = post( path: "#{api_uri}/v3/grants/#{identifier}/messages/send", - request_body: form_body + request_body: payload ) opened_files.each(&:close) diff --git a/lib/nylas/utils/file_utils.rb b/lib/nylas/utils/file_utils.rb index 64b22a8a..0c0f96f8 100644 --- a/lib/nylas/utils/file_utils.rb +++ b/lib/nylas/utils/file_utils.rb @@ -5,6 +5,9 @@ module Nylas # A collection of file-related utilities. module FileUtils + # The maximum size of an attachment that can be sent using json + FORM_DATA_ATTACHMENT_SIZE = 3 * 1024 * 1024 + # Build a form request for the API. # @param request_body The values to create the message with. # @return The form data to send to the API and the opened files. From e2ef5ca97412d558ddd309f63fb1effa5cb99ad9 Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Thu, 29 Feb 2024 18:10:50 -0500 Subject: [PATCH 2/3] add test support --- spec/nylas/resources/drafts_spec.rb | 59 +++++++++++++++++++++++++-- spec/nylas/resources/messages_spec.rb | 28 ++++++++++++- 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/spec/nylas/resources/drafts_spec.rb b/spec/nylas/resources/drafts_spec.rb index f2b7eb80..9981caf4 100644 --- a/spec/nylas/resources/drafts_spec.rb +++ b/spec/nylas/resources/drafts_spec.rb @@ -97,7 +97,33 @@ expect(draft_response).to eq(response) end - it "calls the post method with the correct parameters and attachments" do + it "calls the post method with the correct parameters for small attachments" do + identifier = "abc-123-grant-id" + mock_file = instance_double("file") + request_body = { + subject: "Hello from Nylas!", + to: [{ name: "Jon Snow", email: "jsnow@gmail.com" }], + cc: [{ name: "Arya Stark", email: "astark@gmail.com" }], + body: "This is the body of my draft message.", + attachments: [{ + filename: "file.txt", + content_type: "text/plain", + size: 100, + content: mock_file + }] + } + path = "#{api_uri}/v3/grants/#{identifier}/drafts" + + allow(drafts).to receive(:post) + .with(path: path, request_body: request_body) + .and_return(response) + + draft_response = drafts.create(identifier: identifier, request_body: request_body) + + expect(draft_response).to eq(response) + end + + it "calls the post method with the correct parameters for large attachments" do identifier = "abc-123-grant-id" mock_file = instance_double("file") request_body = { @@ -109,7 +135,7 @@ attachment = { filename: "file.txt", content_type: "text/plain", - size: 100, + size: 3 * 1024 * 1024, content: mock_file } expected_compiled_request = { @@ -154,6 +180,33 @@ end it "calls the put method with the correct parameters and attachments" do + identifier = "abc-123-grant-id" + draft_id = "5d3qmne77v32r8l4phyuksl2x" + mock_file = instance_double("file") + request_body = { + subject: "Hello from Nylas!", + to: [{ name: "Jon Snow", email: "jsnow@gmail.com" }], + cc: [{ name: "Arya Stark", email: "astark@gmail.com" }], + body: "This is the body of my draft message.", + attachments: [{ + filename: "file.txt", + content_type: "text/plain", + size: 100, + content: mock_file + }] + } + path = "#{api_uri}/v3/grants/#{identifier}/drafts/#{draft_id}" + allow(drafts).to receive(:put) + .with(path: path, request_body: request_body) + .and_return(response) + + draft_response = drafts.update(identifier: identifier, draft_id: draft_id, + request_body: request_body) + + expect(draft_response).to eq(response) + end + + it "calls the put method with the correct parameters for large attachments" do identifier = "abc-123-grant-id" draft_id = "5d3qmne77v32r8l4phyuksl2x" mock_file = instance_double("file") @@ -166,7 +219,7 @@ attachment = { filename: "file.txt", content_type: "text/plain", - size: 100, + size: 3 * 1024 * 1024, content: mock_file } expected_compiled_request = { diff --git a/spec/nylas/resources/messages_spec.rb b/spec/nylas/resources/messages_spec.rb index b0aefa13..b1dddf35 100644 --- a/spec/nylas/resources/messages_spec.rb +++ b/spec/nylas/resources/messages_spec.rb @@ -135,6 +135,32 @@ end it "calls the post method with the correct parameters and attachments" do + identifier = "abc-123-grant-id" + mock_file = instance_double("file") + request_body = { + subject: "Hello from Nylas!", + to: [{ name: "Jon Snow", email: "jsnow@gmail.com" }], + cc: [{ name: "Arya Stark", email: "astark@gmail.com" }], + body: "This is the body of my message message.", + attachments: [{ + filename: "file.txt", + content_type: "text/plain", + size: 100, + content: mock_file + }] + } + path = "#{api_uri}/v3/grants/#{identifier}/messages/send" + + allow(messages).to receive(:post) + .with(path: path, request_body: request_body) + .and_return(response) + + message_response = messages.send(identifier: identifier, request_body: request_body) + + expect(message_response).to eq(response) + end + + it "calls the post method with the correct parameters and large attachments" do identifier = "abc-123-grant-id" mock_file = instance_double("file") request_body = { @@ -146,7 +172,7 @@ attachment = { filename: "file.txt", content_type: "text/plain", - size: 100, + size: 3 * 1024 * 1024, content: mock_file } expected_compiled_request = { From 599d1c8d1800a56b82cbd013d18b8f2a9150bcdf Mon Sep 17 00:00:00 2001 From: Mostafa Rashed <17770919+mrashed-dev@users.noreply.github.com> Date: Thu, 29 Feb 2024 18:10:53 -0500 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6aedbee7..9ad5c65f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +### Unreleased +* Improved message sending and draft create/update performance + ### 6.0.2 / 2024-02-27 * Fixed the HTTP operation of updating grants * Fixed endpoint URL of rotating webhooks