-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed issue where attachments < 3mb were not being encoded correctly (#…
…227) # Description This PR fixes an issue where we were not encoding attachments less than 3mb properly. # License <!-- Your PR comment must contain the following line for us to merge the PR. --> I confirm that this contribution is made under the terms of the MIT license and that I have the authority necessary to make this contribution on behalf of its copyright owner.
- Loading branch information
1 parent
6d4f58d
commit 867ac11
Showing
9 changed files
with
100 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/kotlin/com/nylas/util/CreateAttachmentRequestAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.nylas.util | ||
|
||
import com.nylas.models.CreateAttachmentRequest | ||
import com.squareup.moshi.FromJson | ||
import com.squareup.moshi.JsonWriter | ||
import com.squareup.moshi.ToJson | ||
|
||
/** | ||
* This class is used to serialize and deserialize the CreateAttachmentRequest class. | ||
* @suppress Not for public use. | ||
*/ | ||
class CreateAttachmentRequestAdapter { | ||
@ToJson | ||
@Throws(UnsupportedOperationException::class) | ||
fun toJson(writer: JsonWriter, value: CreateAttachmentRequest?) { | ||
writer.beginObject() | ||
writer.name("filename").value(value?.filename) | ||
writer.name("content_type").value(value?.contentType) | ||
writer.name("size").value(value?.size) | ||
value?.isInline?.let { writer.name("is_inline").value(it) } | ||
value?.contentId?.let { writer.name("content_id").value(it) } | ||
value?.contentDisposition?.let { writer.name("content_disposition").value(it) } | ||
|
||
// Encode the file stream to base64 | ||
value?.content?.let { | ||
val base64Content = it.use { stream -> | ||
FileUtils.encodeStreamToBase64(stream) | ||
} | ||
writer.name("content").value(base64Content) | ||
} | ||
|
||
writer.endObject() | ||
} | ||
|
||
@FromJson | ||
@Throws(UnsupportedOperationException::class) | ||
fun fromJson(reader: com.squareup.moshi.JsonReader): CreateAttachmentRequest? { | ||
throw UnsupportedOperationException("Deserialization not supported") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -412,6 +412,7 @@ class MessagesTests { | |
fun `sending a message with a small attachment calls requests with the correct params`() { | ||
val adapter = JsonHelper.moshi().adapter(SendMessageRequest::class.java) | ||
val testInputStream = ByteArrayInputStream("test data".toByteArray()) | ||
val testInputStreamCopy = ByteArrayInputStream("test data".toByteArray()) | ||
val sendMessageRequest = | ||
SendMessageRequest( | ||
to = listOf(EmailName(email = "[email protected]", name = "Test")), | ||
|
@@ -433,6 +434,16 @@ class MessagesTests { | |
), | ||
customHeaders = listOf(CustomHeader(name = "header-name", value = "header-value")), | ||
) | ||
val expectedRequestBody = sendMessageRequest.copy( | ||
attachments = listOf( | ||
CreateAttachmentRequest( | ||
content = testInputStreamCopy, | ||
contentType = "text/plain", | ||
filename = "attachment.txt", | ||
size = 100, | ||
), | ||
), | ||
) | ||
|
||
messages.send(grantId, sendMessageRequest) | ||
|
||
|
@@ -451,7 +462,7 @@ class MessagesTests { | |
|
||
assertEquals("v3/grants/$grantId/messages/send", pathCaptor.firstValue) | ||
assertEquals(Types.newParameterizedType(Response::class.java, Message::class.java), typeCaptor.firstValue) | ||
assertEquals(adapter.toJson(sendMessageRequest), requestBodyCaptor.firstValue) | ||
assertEquals(adapter.toJson(expectedRequestBody), requestBodyCaptor.firstValue) | ||
assertNull(queryParamCaptor.firstValue) | ||
} | ||
|
||
|
@@ -480,6 +491,7 @@ class MessagesTests { | |
), | ||
customHeaders = listOf(CustomHeader(name = "header-name", value = "header-value")), | ||
) | ||
val attachmentLessRequest = sendMessageRequest.copy(attachments = null) | ||
|
||
messages.send(grantId, sendMessageRequest) | ||
|
||
|
@@ -508,7 +520,7 @@ class MessagesTests { | |
val fileBuffer = Buffer() | ||
multipart.part(0).body().writeTo(buffer) | ||
multipart.part(1).body().writeTo(fileBuffer) | ||
assertEquals(adapter.toJson(sendMessageRequest), buffer.readUtf8()) | ||
assertEquals(adapter.toJson(attachmentLessRequest), buffer.readUtf8()) | ||
assertEquals("test data", fileBuffer.readUtf8()) | ||
} | ||
} | ||
|