Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing getAttachmentDownloadUrl #33

Merged
merged 4 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,23 @@ suspend fun downloadAttachment(attachmentId: String, filename: String): Result<U
* The name of the file to save the attachment as.
* Type: `String`
* Return result type: `URL`
* The return URL points to the location of the downloaded attachment in the local temporary storage.

--------------------

#### `ChatSession.getAttachmentDownloadUrl`
Returns the download URL link for the given attachment ID.

```
suspend fun getAttachmentDownloadUrl(attachmentId: String): Result<URL>
```

* `attachmentId`
* The ID of the attachment.
* Type: `String`
* Return result type: `URL`
* This return URL points to the S3 download URL of the attachment.

--------------------

### ChatSession Events
Expand Down
15 changes: 15 additions & 0 deletions chat-sdk/src/main/java/com/amazon/connect/chat/sdk/ChatSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ interface ChatSession {
*/
suspend fun downloadAttachment(attachmentId: String, filename: String): Result<URL>

/**
* Returns the S3 download URL for an attachment.
* @param attachmentId The ID of the attachment.
* @return A Result containing the download URL for the attachment.
*/
suspend fun getAttachmentDownloadUrl(attachmentId: String): Result<URL>

/**
* Gets the transcript.
* @param scanDirection The direction of the scan.
Expand Down Expand Up @@ -220,6 +227,14 @@ class ChatSessionImpl @Inject constructor(private val chatService: ChatService)
}
}

override suspend fun getAttachmentDownloadUrl(attachmentId: String): Result<URL> {
return withContext(Dispatchers.IO) {
runCatching {
chatService.getAttachmentDownloadUrl(attachmentId).getOrThrow()
}
}
}

override suspend fun getTranscript(
scanDirection: ScanDirection?,
sortKey: SortKey?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class AttachmentsManager @Inject constructor(
}

suspend fun completeAttachmentUpload(connectionToken: String, attachmentId: String) {
print("HIT!!")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove print statement

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

val request = CompleteAttachmentUploadRequest().apply {
this.connectionToken = connectionToken
this.setAttachmentIds(listOf(attachmentId))
Expand Down Expand Up @@ -110,9 +111,9 @@ class AttachmentsManager @Inject constructor(
}

suspend fun downloadAttachment(
connectionToken: String,
attachmentId: String,
fileName: String,
connectionToken: String
): Result<URL> {
return getAttachmentDownloadUrl(connectionToken, attachmentId).mapCatching { url ->
downloadFile(url, fileName).getOrThrow()
Expand All @@ -121,7 +122,7 @@ class AttachmentsManager @Inject constructor(
}
}

private suspend fun getAttachmentDownloadUrl(connectionToken: String, attachmentId: String): Result<URL> {
suspend fun getAttachmentDownloadUrl(connectionToken: String, attachmentId: String): Result<URL> {
return runCatching {
val response = awsClient.getAttachment(connectionToken, attachmentId)
URL(response.getOrNull()?.url ?: throw IOException("Invalid URL"))
Expand All @@ -130,7 +131,7 @@ class AttachmentsManager @Inject constructor(
}
}

private suspend fun downloadFile(url: URL, fileName: String): Result<URL> = withContext(Dispatchers.IO) {
suspend fun downloadFile(url: URL, fileName: String): Result<URL> = withContext(Dispatchers.IO) {
runCatching {
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "GET"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ interface ChatService {
*/
suspend fun downloadAttachment(attachmentId: String, fileName: String): Result<URL>


/**
* Returns the S3 download URL for an attachment.
* @param attachmentId The ID of the attachment.
* @return A Result containing the download URL for the attachment.
*/
suspend fun getAttachmentDownloadUrl(attachmentId: String): Result<URL>

/**
* Gets the transcript.
* @param scanDirection The direction of the scan.
Expand Down Expand Up @@ -590,9 +598,19 @@ class ChatServiceImpl @Inject constructor(
return runCatching {
val connectionDetails = connectionDetailsProvider.getConnectionDetails()
?: throw Exception("No connection details available")
attachmentsManager.downloadAttachment(attachmentId, fileName, connectionDetails.connectionToken).getOrThrow()
attachmentsManager.downloadAttachment(connectionDetails.connectionToken, attachmentId, fileName).getOrThrow()
}.onFailure { exception ->
SDKLogger.logger.logError { "Failed to download attachment for attachmentId $attachmentId. Error: ${exception.message}" }
}
}

override suspend fun getAttachmentDownloadUrl(attachmentId: String): Result<URL> {
return runCatching {
val connectionDetails = connectionDetailsProvider.getConnectionDetails()
?: throw Exception("No connection details available")
attachmentsManager.getAttachmentDownloadUrl(attachmentId, connectionDetails.connectionToken).getOrThrow()
}.onFailure { exception ->
SDKLogger.logger.logError { "Failed to download attachment: ${exception.message}" }
SDKLogger.logger.logError { "Failed to retrieve attachment download URL for attachment $attachmentId. Error: ${exception.message}" }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.amazon.connect.chat.sdk.utils.Constants
import com.amazonaws.services.connectparticipant.AmazonConnectParticipantClient
import com.amazonaws.services.connectparticipant.model.CompleteAttachmentUploadRequest
import com.amazonaws.services.connectparticipant.model.CompleteAttachmentUploadResult
import com.amazonaws.services.connectparticipant.model.GetAttachmentResult
import com.amazonaws.services.connectparticipant.model.StartAttachmentUploadRequest
import com.amazonaws.services.connectparticipant.model.StartAttachmentUploadResult
import com.amazonaws.services.connectparticipant.model.UploadMetadata
Expand All @@ -33,6 +34,7 @@ import org.robolectric.RobolectricTestRunner
import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import java.net.URL
import retrofit2.Response as RetrofitResponse

@ExperimentalCoroutinesApi
Expand All @@ -56,13 +58,15 @@ class AttachmentsManagerTest {
private val contentResolver = mock(ContentResolver::class.java)
private val mockStartAttachmentUploadRequest = StartAttachmentUploadRequest()
private val mockStartAttachmentUploadResult = StartAttachmentUploadResult()
private val mockGetAttachmentResult = GetAttachmentResult()

private val mockUri: Uri = Uri.parse("https://example.com/dummy")
private val mockAttachmentId = "12345"
private val mockUploadMetadata = UploadMetadata()
private val mockUrl = "https://example.com/"
private val mockHeadersToInclude = mapOf("key1" to "value1", "key2" to "value2", "key3" to "value3")
private val mockConnectionToken = "connectionToken"
private val mockFilename = "example.txt"

@Before
fun setUp() {
Expand Down Expand Up @@ -178,6 +182,19 @@ class AttachmentsManagerTest {
File("test").deleteRecursively()
}

@Test
fun test_downloadAttachment() = runTest {
mockGetAttachmentResult.url = mockUrl
doReturn(mockGetAttachmentResult)
.`when`(awsClient)
.getAttachment(mockConnectionToken, mockAttachmentId)

attachmentsManager.downloadAttachment(mockConnectionToken, mockAttachmentId, mockFilename)
verify(awsClient).getAttachment(mockConnectionToken, mockAttachmentId)
verify(attachmentsManager).getAttachmentDownloadUrl(mockConnectionToken, mockAttachmentId)
verify(attachmentsManager).downloadFile(URL(mockUrl), mockFilename)
}

@Test
fun test_getFileExtension() = runTest {
for (extension in Constants.attachmentTypeMap.keys) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,13 @@ class ChatServiceImplTest {
val mockConnectionDetails = createMockConnectionDetails("valid_token")

`when`(connectionDetailsProvider.getConnectionDetails()).thenReturn(mockConnectionDetails)
`when`(attachmentsManager.downloadAttachment(attachmentId, fileName, mockConnectionDetails.connectionToken)).thenReturn(Result.success(mockUrl))
`when`(attachmentsManager.downloadAttachment(mockConnectionDetails.connectionToken, attachmentId, fileName)).thenReturn(Result.success(mockUrl))

val result = chatService.downloadAttachment(attachmentId, fileName)

assertTrue(result.isSuccess)
assertEquals(mockUrl, result.getOrNull())
verify(attachmentsManager).downloadAttachment(attachmentId, fileName, mockConnectionDetails.connectionToken)
verify(attachmentsManager).downloadAttachment(mockConnectionDetails.connectionToken, attachmentId, fileName)
}

@Test
Expand All @@ -342,12 +342,12 @@ class ChatServiceImplTest {
val mockConnectionDetails = createMockConnectionDetails("valid_token")

`when`(connectionDetailsProvider.getConnectionDetails()).thenReturn(mockConnectionDetails)
`when`(attachmentsManager.downloadAttachment(attachmentId, fileName, mockConnectionDetails.connectionToken)).thenThrow(RuntimeException("Download error"))
`when`(attachmentsManager.downloadAttachment(mockConnectionDetails.connectionToken, attachmentId, fileName)).thenThrow(RuntimeException("Download error"))

val result = chatService.downloadAttachment(attachmentId, fileName)

assertTrue(result.isFailure)
verify(attachmentsManager).downloadAttachment(attachmentId, fileName, mockConnectionDetails.connectionToken)
verify(attachmentsManager).downloadAttachment(mockConnectionDetails.connectionToken, attachmentId, fileName)
}

@Test
Expand Down
Loading