Skip to content

Commit

Permalink
Add support for custom headers field for drafts and messages (#223)
Browse files Browse the repository at this point in the history
# Description
This PR adds support to the custom headers field used to attach headers
to message and draft objects.

# 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
mrashed-dev authored Apr 30, 2024
1 parent ded393a commit 6d4f58d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Nylas Java SDK Changelog

## Unreleased
### [Unreleased]

### Added
* Added support for custom headers field for Drafts and Messages
* Added support for overriding various fields of outgoing requests

## [2.2.1] - Released 2024-03-05
Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/com/nylas/models/CreateDraftRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ data class CreateDraftRequest(
*/
@Json(name = "tracking_options")
val trackingOptions: TrackingOptions? = null,
/**
* A list of custom headers to add to the message.
*/
@Json(name = "custom_headers")
val customHeaders: List<CustomHeader>? = null,
) : IMessageAttachmentRequest {
/**
* Builder for [CreateDraftRequest].
Expand All @@ -78,6 +83,7 @@ data class CreateDraftRequest(
private var sendAt: Int? = null
private var replyToMessageId: String? = null
private var trackingOptions: TrackingOptions? = null
private var customHeaders: List<CustomHeader>? = null

/**
* Sets the recipients.
Expand Down Expand Up @@ -157,6 +163,13 @@ data class CreateDraftRequest(
*/
fun trackingOptions(trackingOptions: TrackingOptions?) = apply { this.trackingOptions = trackingOptions }

/**
* Sets the custom headers to add to the message.
* @param customHeaders The custom headers to add to the message.
* @return The builder.
*/
fun customHeaders(customHeaders: List<CustomHeader>?) = apply { this.customHeaders = customHeaders }

/**
* Builds a [SendMessageRequest] instance.
* @return The [SendMessageRequest] instance.
Expand All @@ -174,6 +187,7 @@ data class CreateDraftRequest(
sendAt,
replyToMessageId,
trackingOptions,
customHeaders,
)
}
}
36 changes: 36 additions & 0 deletions src/main/kotlin/com/nylas/models/CustomHeader.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.nylas.models

import com.squareup.moshi.Json

/**
* Custom headers to be used when drafting or sending an email.
*/
data class CustomHeader(
/**
* The name of the custom header.
*/
@Json(name = "name")
val name: String,
/**
* The value of the custom header.
*/
@Json(name = "value")
val value: String,
) {
/**
* Builder for [CustomHeader].
* @property name The name of the custom header.
* @property value The value of the custom header.
*/
data class Builder(
private val name: String,
private val value: String,
) {
/**
* Build the [CustomHeader] object.
*/
fun build(): CustomHeader {
return CustomHeader(name, value)
}
}
}
14 changes: 14 additions & 0 deletions src/main/kotlin/com/nylas/models/SendMessageRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ data class SendMessageRequest(
*/
@Json(name = "use_draft")
val useDraft: Boolean? = null,
/**
* A list of custom headers to add to the message.
*/
@Json(name = "custom_headers")
val customHeaders: List<CustomHeader>? = null,
) : IMessageAttachmentRequest {
/**
* Builder for [SendMessageRequest].
Expand All @@ -87,6 +92,7 @@ data class SendMessageRequest(
private var replyToMessageId: String? = null
private var trackingOptions: TrackingOptions? = null
private var useDraft: Boolean? = null
private var customHeaders: List<CustomHeader>? = null

/**
* Sets the bcc recipients.
Expand Down Expand Up @@ -167,6 +173,13 @@ data class SendMessageRequest(
*/
fun useDraft(useDraft: Boolean?) = apply { this.useDraft = useDraft }

/**
* Sets the custom headers to add to the message.
* @param customHeaders The custom headers to add to the message.
* @return The builder.
*/
fun customHeaders(customHeaders: List<CustomHeader>?) = apply { this.customHeaders = customHeaders }

/**
* Builds a [SendMessageRequest] instance.
* @return The [SendMessageRequest] instance.
Expand All @@ -185,6 +198,7 @@ data class SendMessageRequest(
replyToMessageId,
trackingOptions,
useDraft,
customHeaders,
)
}
}
3 changes: 3 additions & 0 deletions src/test/kotlin/com/nylas/resources/DraftsTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ class DraftsTests {
sendAt = 1620000000,
replyToMessageId = "reply-to-message-id",
trackingOptions = TrackingOptions(label = "label", links = true, opens = true, threadReplies = true),
customHeaders = listOf(CustomHeader(name = "header", value = "value")),
)

drafts.create(grantId, createDraftRequest)
Expand Down Expand Up @@ -275,6 +276,7 @@ class DraftsTests {
size = 100,
),
),
customHeaders = listOf(CustomHeader(name = "header", value = "value")),
)

drafts.create(grantId, createDraftRequest)
Expand Down Expand Up @@ -320,6 +322,7 @@ class DraftsTests {
size = 3 * 1024 * 1024,
),
),
customHeaders = listOf(CustomHeader(name = "header", value = "value")),
)

drafts.create(grantId, createDraftRequest)
Expand Down
3 changes: 3 additions & 0 deletions src/test/kotlin/com/nylas/resources/MessagesTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ class MessagesTests {
sendAt = 1620000000,
replyToMessageId = "reply-to-message-id",
trackingOptions = TrackingOptions(label = "label", links = true, opens = true, threadReplies = true),
customHeaders = listOf(CustomHeader(name = "header-name", value = "header-value")),
)

messages.send(grantId, sendMessageRequest)
Expand Down Expand Up @@ -430,6 +431,7 @@ class MessagesTests {
size = 100,
),
),
customHeaders = listOf(CustomHeader(name = "header-name", value = "header-value")),
)

messages.send(grantId, sendMessageRequest)
Expand Down Expand Up @@ -476,6 +478,7 @@ class MessagesTests {
size = 3 * 1024 * 1024,
),
),
customHeaders = listOf(CustomHeader(name = "header-name", value = "header-value")),
)

messages.send(grantId, sendMessageRequest)
Expand Down

0 comments on commit 6d4f58d

Please sign in to comment.