This repository has been archived by the owner on Aug 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate to suspendCommand and suspendCommandAsync to be more clear wh…
…ich operation is taking place
- Loading branch information
1 parent
5c6e641
commit 8c3022c
Showing
7 changed files
with
109 additions
and
79 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
31 changes: 0 additions & 31 deletions
31
java-sdk/src/main/kotlin/com/github/bradynpoulsen/aws/coroutines/AwsBlocking.kt
This file was deleted.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
java-sdk/src/main/kotlin/com/github/bradynpoulsen/aws/coroutines/AwsSync.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,45 @@ | ||
package com.github.bradynpoulsen.aws.coroutines | ||
|
||
import com.amazonaws.AmazonWebServiceRequest | ||
import com.amazonaws.AmazonWebServiceResult | ||
import kotlinx.coroutines.* | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.coroutines.EmptyCoroutineContext | ||
|
||
/** | ||
* Executes the synchronous [method] in the current [CoroutineScope]'s dispatcher. | ||
* | ||
* Example: | ||
* | ||
* val client: AmazonEC2 = buildClient() | ||
* val describeResult: DescribeInstancesResult = suspendCommand(client::describeInstances) | ||
*/ | ||
@ExperimentalAwsCoroutineApi | ||
@UseExperimental(ExperimentalCoroutinesApi::class) | ||
suspend inline fun <AwsResult : AmazonWebServiceResult<*>> CoroutineScope.suspendCommand( | ||
crossinline method: () -> AwsResult, | ||
context: CoroutineContext = EmptyCoroutineContext | ||
): AwsResult = withContext(newCoroutineContext(context)) { | ||
method() | ||
} | ||
|
||
/** | ||
* Executes the synchronous [method] in the current [CoroutineScope]'s dispatcher. | ||
* | ||
* Example: | ||
* | ||
* val client: AmazonEC2 = buildClient() | ||
* val describeResult: DescribeImagesResult = suspendCommand(client::describeImages) { | ||
* DescribeImagesRequest() | ||
* .withImageIds("ami-12312412431754", "ami-12334237461523") | ||
* } | ||
*/ | ||
@ExperimentalAwsCoroutineApi | ||
@UseExperimental(ExperimentalCoroutinesApi::class) | ||
suspend inline fun <AwsRequest : AmazonWebServiceRequest, AwsResult : AmazonWebServiceResult<*>> CoroutineScope.suspendCommand( | ||
crossinline method: (request: AwsRequest) -> AwsResult, | ||
context: CoroutineContext = EmptyCoroutineContext, | ||
crossinline requestBuilder: () -> AwsRequest | ||
): AwsResult = withContext(newCoroutineContext(context)) { | ||
method(requestBuilder()) | ||
} |
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 was deleted.
Oops, something went wrong.
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,28 @@ | ||
package example | ||
|
||
import com.amazonaws.services.ec2.AmazonEC2 | ||
import com.amazonaws.services.ec2.model.DescribeImagesRequest | ||
import com.github.bradynpoulsen.aws.coroutines.suspendCommand | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.coroutineScope | ||
|
||
val ec2Client: AmazonEC2 = TODO() | ||
|
||
private suspend fun exampleBlocking() = coroutineScope { | ||
val result = suspendCommand(ec2Client::describeReservedInstances) | ||
|
||
result.reservedInstances.forEach { | ||
println("${it.reservedInstancesId}: ${it.start..it.end} ${it.availabilityZone}") | ||
} | ||
} | ||
|
||
private suspend fun exampleBlockingWithRequestBuilder() = coroutineScope { | ||
val result = suspendCommand(ec2Client::describeImages, context = Dispatchers.IO) { | ||
DescribeImagesRequest() | ||
.withImageIds("ami-12312412431754", "ami-12334237461523") | ||
} | ||
|
||
result.images.forEach { | ||
println("${it.imageId}: ${it.description}") | ||
} | ||
} |