lib/nylas/resources/drafts.rb in nylas-6.0.2 vs lib/nylas/resources/drafts.rb in nylas-6.0.3

- old
+ new

@@ -41,14 +41,24 @@ # @param request_body [Hash] The values to create the message with. # If you're attaching files, you must pass an array of [File] objects, or # 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) response @@ -61,14 +71,23 @@ # @param request_body [Hash] The values to create the message with. # If you're attaching files, you must pass an array of [File] objects, or # 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) response