-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved message sending and draft create/update performance (#217)
# Description This PR improves message send/draft create/update performance by always defaulting to application/json instead of multipart. Multipart will only be used for when a request contains a total attachments size of 3mb or higher. # 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
88da3b5
commit 6afc9ff
Showing
6 changed files
with
317 additions
and
24 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
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ import org.mockito.kotlin.any | |
import org.mockito.kotlin.argumentCaptor | ||
import org.mockito.kotlin.verify | ||
import org.mockito.kotlin.whenever | ||
import java.io.ByteArrayInputStream | ||
import java.lang.reflect.Type | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
@@ -227,6 +228,92 @@ class DraftsTests { | |
|
||
drafts.create(grantId, createDraftRequest) | ||
|
||
val pathCaptor = argumentCaptor<String>() | ||
val typeCaptor = argumentCaptor<Type>() | ||
val requestBodyCaptor = argumentCaptor<String>() | ||
val queryParamCaptor = argumentCaptor<IQueryParams>() | ||
verify(mockNylasClient).executePost<Response<Draft>>( | ||
pathCaptor.capture(), | ||
typeCaptor.capture(), | ||
requestBodyCaptor.capture(), | ||
queryParamCaptor.capture(), | ||
) | ||
|
||
assertEquals("v3/grants/$grantId/drafts", pathCaptor.firstValue) | ||
assertEquals(Types.newParameterizedType(Response::class.java, Draft::class.java), typeCaptor.firstValue) | ||
assertEquals(adapter.toJson(createDraftRequest), requestBodyCaptor.firstValue) | ||
assertNull(queryParamCaptor.firstValue) | ||
} | ||
|
||
@Test | ||
fun `creating a draft with small attachment calls requests with the correct params`() { | ||
val adapter = JsonHelper.moshi().adapter(CreateDraftRequest::class.java) | ||
val testInputStream = ByteArrayInputStream("test data".toByteArray()) | ||
val createDraftRequest = | ||
CreateDraftRequest( | ||
body = "Hello, I just sent a message using Nylas!", | ||
cc = listOf(EmailName(email = "[email protected]", name = "Test")), | ||
bcc = listOf(EmailName(email = "[email protected]", name = "BCC")), | ||
subject = "Hello from Nylas!", | ||
starred = true, | ||
sendAt = 1620000000, | ||
replyToMessageId = "reply-to-message-id", | ||
trackingOptions = TrackingOptions(label = "label", links = true, opens = true, threadReplies = true), | ||
attachments = listOf( | ||
CreateAttachmentRequest( | ||
content = testInputStream, | ||
contentType = "text/plain", | ||
filename = "attachment.txt", | ||
size = 100, | ||
), | ||
), | ||
) | ||
|
||
drafts.create(grantId, createDraftRequest) | ||
|
||
val pathCaptor = argumentCaptor<String>() | ||
val typeCaptor = argumentCaptor<Type>() | ||
val requestBodyCaptor = argumentCaptor<String>() | ||
val queryParamCaptor = argumentCaptor<IQueryParams>() | ||
verify(mockNylasClient).executePost<Response<Draft>>( | ||
pathCaptor.capture(), | ||
typeCaptor.capture(), | ||
requestBodyCaptor.capture(), | ||
queryParamCaptor.capture(), | ||
) | ||
|
||
assertEquals("v3/grants/$grantId/drafts", pathCaptor.firstValue) | ||
assertEquals(Types.newParameterizedType(Response::class.java, Draft::class.java), typeCaptor.firstValue) | ||
assertEquals(adapter.toJson(createDraftRequest), requestBodyCaptor.firstValue) | ||
assertNull(queryParamCaptor.firstValue) | ||
} | ||
|
||
@Test | ||
fun `creating a draft with large attachment calls requests with the correct params`() { | ||
val adapter = JsonHelper.moshi().adapter(CreateDraftRequest::class.java) | ||
val testInputStream = ByteArrayInputStream("test data".toByteArray()) | ||
val createDraftRequest = | ||
CreateDraftRequest( | ||
body = "Hello, I just sent a message using Nylas!", | ||
cc = listOf(EmailName(email = "[email protected]", name = "Test")), | ||
bcc = listOf(EmailName(email = "[email protected]", name = "BCC")), | ||
subject = "Hello from Nylas!", | ||
starred = true, | ||
sendAt = 1620000000, | ||
replyToMessageId = "reply-to-message-id", | ||
trackingOptions = TrackingOptions(label = "label", links = true, opens = true, threadReplies = true), | ||
attachments = listOf( | ||
CreateAttachmentRequest( | ||
content = testInputStream, | ||
contentType = "text/plain", | ||
filename = "attachment.txt", | ||
size = 3 * 1024 * 1024, | ||
), | ||
), | ||
) | ||
|
||
drafts.create(grantId, createDraftRequest) | ||
|
||
val pathCaptor = argumentCaptor<String>() | ||
val methodCaptor = argumentCaptor<NylasClient.HttpMethod>() | ||
val typeCaptor = argumentCaptor<Type>() | ||
|
@@ -245,10 +332,13 @@ class DraftsTests { | |
assertEquals(NylasClient.HttpMethod.POST, methodCaptor.firstValue) | ||
assertNull(queryParamCaptor.firstValue) | ||
val multipart = requestBodyCaptor.firstValue as MultipartBody | ||
assertEquals(1, multipart.size()) | ||
assertEquals(2, multipart.size()) | ||
val buffer = Buffer() | ||
val fileBuffer = Buffer() | ||
multipart.part(0).body().writeTo(buffer) | ||
multipart.part(1).body().writeTo(fileBuffer) | ||
assertEquals(adapter.toJson(createDraftRequest), buffer.readUtf8()) | ||
assertEquals("test data", fileBuffer.readUtf8()) | ||
} | ||
|
||
@Test | ||
|
@@ -265,6 +355,86 @@ class DraftsTests { | |
|
||
drafts.update(grantId, draftId, updateDraftRequest) | ||
|
||
val pathCaptor = argumentCaptor<String>() | ||
val typeCaptor = argumentCaptor<Type>() | ||
val requestBodyCaptor = argumentCaptor<String>() | ||
val queryParamCaptor = argumentCaptor<IQueryParams>() | ||
verify(mockNylasClient).executePut<Response<Draft>>( | ||
pathCaptor.capture(), | ||
typeCaptor.capture(), | ||
requestBodyCaptor.capture(), | ||
queryParamCaptor.capture(), | ||
) | ||
|
||
assertEquals("v3/grants/$grantId/drafts/$draftId", pathCaptor.firstValue) | ||
assertEquals(Types.newParameterizedType(Response::class.java, Draft::class.java), typeCaptor.firstValue) | ||
assertEquals(adapter.toJson(updateDraftRequest), requestBodyCaptor.firstValue) | ||
assertNull(queryParamCaptor.firstValue) | ||
} | ||
|
||
@Test | ||
fun `updating a draft with small attachments calls requests with the correct params`() { | ||
val draftId = "draft-123" | ||
val adapter = JsonHelper.moshi().adapter(UpdateDraftRequest::class.java) | ||
val testInputStream = ByteArrayInputStream("test data".toByteArray()) | ||
val updateDraftRequest = | ||
UpdateDraftRequest( | ||
body = "Hello, I just sent a message using Nylas!", | ||
subject = "Hello from Nylas!", | ||
unread = false, | ||
starred = true, | ||
attachments = listOf( | ||
CreateAttachmentRequest( | ||
content = testInputStream, | ||
contentType = "text/plain", | ||
filename = "attachment.txt", | ||
size = 100, | ||
), | ||
), | ||
) | ||
|
||
drafts.update(grantId, draftId, updateDraftRequest) | ||
|
||
val pathCaptor = argumentCaptor<String>() | ||
val typeCaptor = argumentCaptor<Type>() | ||
val requestBodyCaptor = argumentCaptor<String>() | ||
val queryParamCaptor = argumentCaptor<IQueryParams>() | ||
verify(mockNylasClient).executePut<Response<Draft>>( | ||
pathCaptor.capture(), | ||
typeCaptor.capture(), | ||
requestBodyCaptor.capture(), | ||
queryParamCaptor.capture(), | ||
) | ||
|
||
assertEquals("v3/grants/$grantId/drafts/$draftId", pathCaptor.firstValue) | ||
assertEquals(Types.newParameterizedType(Response::class.java, Draft::class.java), typeCaptor.firstValue) | ||
assertEquals(adapter.toJson(updateDraftRequest), requestBodyCaptor.firstValue) | ||
assertNull(queryParamCaptor.firstValue) | ||
} | ||
|
||
@Test | ||
fun `updating a draft with large attachments calls requests with the correct params`() { | ||
val draftId = "draft-123" | ||
val adapter = JsonHelper.moshi().adapter(UpdateDraftRequest::class.java) | ||
val testInputStream = ByteArrayInputStream("test data".toByteArray()) | ||
val updateDraftRequest = | ||
UpdateDraftRequest( | ||
body = "Hello, I just sent a message using Nylas!", | ||
subject = "Hello from Nylas!", | ||
unread = false, | ||
starred = true, | ||
attachments = listOf( | ||
CreateAttachmentRequest( | ||
content = testInputStream, | ||
contentType = "text/plain", | ||
filename = "attachment.txt", | ||
size = 3 * 1024 * 1024, | ||
), | ||
), | ||
) | ||
|
||
drafts.update(grantId, draftId, updateDraftRequest) | ||
|
||
val pathCaptor = argumentCaptor<String>() | ||
val methodCaptor = argumentCaptor<NylasClient.HttpMethod>() | ||
val typeCaptor = argumentCaptor<Type>() | ||
|
@@ -283,10 +453,13 @@ class DraftsTests { | |
assertEquals(NylasClient.HttpMethod.PUT, methodCaptor.firstValue) | ||
assertNull(queryParamCaptor.firstValue) | ||
val multipart = requestBodyCaptor.firstValue as MultipartBody | ||
assertEquals(1, multipart.size()) | ||
assertEquals(2, multipart.size()) | ||
val buffer = Buffer() | ||
val fileBuffer = Buffer() | ||
multipart.part(0).body().writeTo(buffer) | ||
multipart.part(1).body().writeTo(fileBuffer) | ||
assertEquals(adapter.toJson(updateDraftRequest), buffer.readUtf8()) | ||
assertEquals("test data", fileBuffer.readUtf8()) | ||
} | ||
|
||
@Test | ||
|
Oops, something went wrong.