-
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.
# Description This PR adds support for the Attachments API # 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
97254ff
commit 1424840
Showing
5 changed files
with
135 additions
and
19 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
30 changes: 30 additions & 0 deletions
30
src/main/kotlin/com/nylas/models/FindAttachmentQueryParams.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,30 @@ | ||
package com.nylas.models | ||
|
||
import com.squareup.moshi.Json | ||
|
||
/** | ||
* Class representation of the query parameters for finding an attachment. | ||
*/ | ||
data class FindAttachmentQueryParams( | ||
/** | ||
* Message ID to find the attachment in. | ||
*/ | ||
@Json(name = "message_id") | ||
val messageId: String, | ||
) : IQueryParams { | ||
/** | ||
* Builder for [FindEventQueryParams]. | ||
* @property messageId Message ID to find the attachment in. | ||
*/ | ||
data class Builder( | ||
private val messageId: String, | ||
) { | ||
/** | ||
* Builds a new [FindEventQueryParams] instance. | ||
* @return [FindEventQueryParams] | ||
*/ | ||
fun build() = FindEventQueryParams( | ||
messageId, | ||
) | ||
} | ||
} |
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,63 @@ | ||
package com.nylas.resources | ||
|
||
import com.nylas.NylasClient | ||
import com.nylas.models.* | ||
import okhttp3.ResponseBody | ||
|
||
/** | ||
* Nylas Attachments API | ||
* | ||
* The Nylas Attachments API allows you to fetch attachment metadata and download data. | ||
* | ||
* @param client The configured Nylas API client | ||
*/ | ||
class Attachments(client: NylasClient) : Resource<Attachment>(client, Attachment::class.java) { | ||
/** | ||
* Return metadata of an attachment | ||
* @param identifier Grant ID or email account to query | ||
* @param attachmentId The id of the attachment to retrieve. | ||
* @param queryParams The query parameters to include in the request | ||
* @return The attachment metadata | ||
*/ | ||
@Throws(NylasOAuthError::class, NylasSdkTimeoutError::class) | ||
fun find(identifier: String, attachmentId: String, queryParams: FindAttachmentQueryParams): Response<Attachment> { | ||
val path = String.format("v3/grants/%s/attachments/%s", identifier, attachmentId) | ||
return findResource(path, queryParams) | ||
} | ||
|
||
/** | ||
* Download the attachment data | ||
* | ||
* This method returns a [ResponseBody] which can be used to stream the attachment data, | ||
* and exposes useful headers such as the content type and content length. | ||
* **NOTE**: The caller is responsible for closing the response body. | ||
* | ||
* Alternatively, you can use [downloadBytes] to download the attachment as a byte array. | ||
* | ||
* @param identifier Grant ID or email account to query | ||
* @param attachmentId The id of the attachment to download. | ||
* @param queryParams The query parameters to include in the request | ||
* @return The [ResponseBody] containing the file data | ||
*/ | ||
@Throws(NylasOAuthError::class, NylasSdkTimeoutError::class) | ||
fun download(identifier: String, attachmentId: String, queryParams: FindAttachmentQueryParams): ResponseBody { | ||
val path = String.format("v3/grants/%s/attachments/%s/download", identifier, attachmentId) | ||
|
||
return client.downloadResponse(path, queryParams) | ||
} | ||
|
||
/** | ||
* Download the attachment as a byte array | ||
* @param identifier Grant ID or email account to query | ||
* @param attachmentId The id of the attachment to download. | ||
* @param queryParams The query parameters to include in the request | ||
* @return The raw file data | ||
*/ | ||
@Throws(NylasOAuthError::class, NylasSdkTimeoutError::class) | ||
fun downloadBytes(identifier: String, attachmentId: String, queryParams: FindAttachmentQueryParams): ByteArray { | ||
val download = download(identifier, attachmentId, queryParams) | ||
val fileBytes = download.bytes() | ||
download.close() | ||
return fileBytes | ||
} | ||
} |
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