Skip to content

Commit

Permalink
Add Messages, Drafts, and Smart Compose APIs support (#166)
Browse files Browse the repository at this point in the history
# Description
This PR adds support for messages, drafts, and smart compose APIs.

# 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 Nov 13, 2023
1 parent 358dce8 commit d5532ef
Show file tree
Hide file tree
Showing 35 changed files with 2,576 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Added
* Added support for the free-busy endpoint
* Added Messages, Drafts, and Smart Compose APIs support
* Added support for custom authentication, connectors, and credentials APIs

### Changed
Expand Down
45 changes: 45 additions & 0 deletions src/main/kotlin/com/nylas/NylasClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ class NylasClient(
return Connectors(this)
}

/**
* Access the Drafts API
* @return The Drafts API
*/
fun drafts(): Drafts {
return Drafts(this)
}

/**
* Access the Events API
* @return The Events API
Expand All @@ -111,6 +119,22 @@ class NylasClient(
return Events(this)
}

/**
* Access the Messages API
* @return The Messages API
*/
fun messages(): Messages {
return Messages(this)
}

/**
* Access the Threads API
* @return The Threads API
*/
fun threads(): Threads {
return Threads(this)
}

/**
* Access the Webhooks API
* @return The Webhooks API
Expand Down Expand Up @@ -223,6 +247,27 @@ class NylasClient(
return executeRequest(url, HttpMethod.DELETE, null, resultType)
}

/**
* Execute a request with a form-body payload to the Nylas API.
* @param path The path to request.
* @param method The HTTP method to use.
* @param requestBody The form-data request body.
* @param resultType The type of the response body.
* @param queryParams The query parameters.
* @suppress Not for public use.
*/
@Throws(AbstractNylasApiError::class, NylasSdkTimeoutError::class)
fun <T> executeFormRequest(
path: String,
method: HttpMethod,
requestBody: RequestBody,
resultType: Type? = null,
queryParams: IQueryParams? = null,
): T {
val url = buildUrl(path, queryParams)
return executeRequest(url, method, requestBody, resultType)
}

private fun buildRequest(
url: HttpUrl.Builder,
method: HttpMethod,
Expand Down
49 changes: 49 additions & 0 deletions src/main/kotlin/com/nylas/models/Attachment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.nylas.models

import com.squareup.moshi.Json

/**
* Class representation of a Nylas attachment object
*/
data class Attachment(
/**
* Globally unique object identifier.
*/
@Json(name = "id")
val id: String? = null,
/**
* Nylas grant ID that is now successfully created.
*/
@Json(name = "grant_id")
val grantId: String? = null,
/**
* The size of the attachment in bytes.
*/
@Json(name = "size")
val size: Int? = null,
/**
* The filename of the attachment.
*/
@Json(name = "filename")
val filename: String? = null,
/**
* The content type of the attachment.
*/
@Json(name = "content_type")
val contentType: String? = null,
/**
* If it's an inline attachment.
*/
@Json(name = "is_inline")
val isInline: Boolean? = null,
/**
* The content ID of the attachment.
*/
@Json(name = "content_id")
val contentId: String? = null,
/**
* The content disposition if the attachment is located inline.
*/
@Json(name = "content_disposition")
val contentDisposition: String? = null,
)
11 changes: 11 additions & 0 deletions src/main/kotlin/com/nylas/models/ComposeMessageRequest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.nylas.models

/**
* Class representing a request to compose a message.
*/
data class ComposeMessageRequest(
/**
* The prompt that smart compose will use to generate a message suggestion.
*/
val prompt: String,
)
11 changes: 11 additions & 0 deletions src/main/kotlin/com/nylas/models/ComposeMessageResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.nylas.models

/**
* Class representing a response to a message composition request.
*/
data class ComposeMessageResponse(
/**
* The message suggestion generated by smart compose.
*/
val suggestion: String,
)
97 changes: 97 additions & 0 deletions src/main/kotlin/com/nylas/models/CreateAttachmentRequest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.nylas.models

import com.squareup.moshi.Json
import java.io.InputStream

/**
* Class representing a Nylas attachment object.
*/
class CreateAttachmentRequest(
/**
* The filename of the attachment.
*/
@Json(name = "filename")
val filename: String,
/**
* The content type of the attachment.
*/
@Json(name = "content_type")
val contentType: String,
/**
* The content of the attachment.
*/
@Transient
val content: InputStream,
/**
* The size of the attachment in bytes.
*/
@Json(name = "size")
val size: Int,
/**
* If it's an inline attachment.
*/
@Json(name = "is_inline")
val isInline: Boolean? = null,
/**
* The content ID of the attachment.
*/
@Json(name = "content_id")
val contentId: String? = null,
/**
* The content disposition if the attachment is located inline.
*/
@Json(name = "content_disposition")
val contentDisposition: String? = null,
) {
/**
* Builder for [CreateAttachmentRequest].
* @property filename The filename of the attachment.
* @property contentType The content type of the attachment.
* @property size The size of the attachment in bytes.
*/
data class Builder(
private val filename: String,
private val contentType: String,
private val content: InputStream,
private val size: Int,
) {
private var isInline: Boolean? = null
private var contentId: String? = null
private var contentDisposition: String? = null

/**
* Set if the attachment is inline.
* @param isInline If the attachment is inline.
* @return The builder.
*/
fun isInline(isInline: Boolean) = apply { this.isInline = isInline }

/**
* Set the content ID of the attachment.
* @param contentId The content ID of the attachment.
* @return The builder.
*/
fun contentId(contentId: String) = apply { this.contentId = contentId }

/**
* Set the content disposition of the attachment.
* @param contentDisposition The content disposition of the attachment.
* @return The builder.
*/
fun contentDisposition(contentDisposition: String) = apply { this.contentDisposition = contentDisposition }

/**
* Build the [CreateAttachmentRequest].
* @return The [CreateAttachmentRequest].
*/
fun build() = CreateAttachmentRequest(
filename = filename,
contentType = contentType,
content = content,
size = size,
isInline = isInline,
contentId = contentId,
contentDisposition = contentDisposition,
)
}
}
6 changes: 3 additions & 3 deletions src/main/kotlin/com/nylas/models/CreateCredentialRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ sealed class CreateCredentialRequest(
* Data that specifies some special data required for a Microsoft credential
*/
@Json(name = "credential_data")
override val credentialData: MicrosoftAdminConsentCredentialData,
override val credentialData: CredentialData.MicrosoftAdminConsent,
) : CreateCredentialRequest(name, credentialData, CredentialType.ADMINCONSENT)

/**
Expand All @@ -51,7 +51,7 @@ sealed class CreateCredentialRequest(
* Data that specifies some special data required for a Google credential
*/
@Json(name = "credential_data")
override val credentialData: GoogleServiceAccountCredentialData,
override val credentialData: CredentialData.GoogleServiceAccount,
) : CreateCredentialRequest(name, credentialData, CredentialType.SERVICEACCOUNT)

/**
Expand All @@ -67,6 +67,6 @@ sealed class CreateCredentialRequest(
* Data that specifies some special data required for an override credential
*/
@Json(name = "credential_data")
override val credentialData: ConnectorOverrideCredentialData,
override val credentialData: CredentialData.ConnectorOverride,
) : CreateCredentialRequest(name, credentialData, CredentialType.CONNECTOR)
}
Loading

0 comments on commit d5532ef

Please sign in to comment.