|
| 1 | +==================== |
| 2 | +Codegen Integrations |
| 3 | +==================== |
| 4 | + |
| 5 | +Smithy Java provides a number of client codegen integrations that modify the code |
| 6 | +generated by the client codegen plugin. |
| 7 | + |
| 8 | +-------------- |
| 9 | +Waiter codegen |
| 10 | +-------------- |
| 11 | + |
| 12 | +.. warning:: |
| 13 | + |
| 14 | + Only synchronous waiters are supported at this time. |
| 15 | + |
| 16 | +:ref:`Waiters <waiters>` are a client-side abstraction used to poll a resource until a |
| 17 | +desired state is reached, or until it is determined that the resource will never enter |
| 18 | +a desirable end state. Waiters can be defined in the Smithy model using the |
| 19 | +``smithy.waiters#waitable`` trait. |
| 20 | + |
| 21 | +For example: |
| 22 | + |
| 23 | +.. code-block:: smithy |
| 24 | + :caption: model.smithy |
| 25 | +
|
| 26 | + use smithy.waiters#waitable |
| 27 | +
|
| 28 | + @waitable( |
| 29 | + BucketExists: { |
| 30 | + documentation: "Wait until a bucket exists" |
| 31 | + acceptors: [ |
| 32 | + { |
| 33 | + state: "success" |
| 34 | + matcher: { |
| 35 | + success: true |
| 36 | + } |
| 37 | + } |
| 38 | + { |
| 39 | + state: "retry" |
| 40 | + matcher: { |
| 41 | + errorType: "NotFound" |
| 42 | + } |
| 43 | + } |
| 44 | + ] |
| 45 | + } |
| 46 | + ) |
| 47 | + operation HeadBucket { |
| 48 | + input: HeadBucketInput |
| 49 | + output: HeadBucketOutput |
| 50 | + errors: [NotFound] |
| 51 | + } |
| 52 | +
|
| 53 | +Using the waiters integration, you can automatically generate waiters from instances |
| 54 | +of the ``waitable`` trait in your Smithy model. If you are using the Smithy Gradle plugins, |
| 55 | +you can add this integration to your project like so: |
| 56 | + |
| 57 | +.. code-block:: kotlin |
| 58 | + :caption: build.gradle.kts |
| 59 | +
|
| 60 | + dependencies { |
| 61 | + // Add codegen integration as a smithy-build dependency so it can be |
| 62 | + // discovered by the client codegen plugin |
| 63 | + smithyBuild("software.amazon.smithy.java.codegen:waiters:__smithy_java_version__") |
| 64 | +
|
| 65 | + // Add waiters core package as a runtime dependency |
| 66 | + implementation("software.amazon.smithy.java:waiters:__smithy_java_version__") |
| 67 | + } |
| 68 | +
|
| 69 | +The code generator will create a class named ``<ServiceName>Waiters`` in the client package. |
| 70 | +This class provides a method per waiter defined in your Smithy model. The methods return ``Waiter``` instances based on the configuration set in your Smithy model. |
| 71 | + |
| 72 | +To get an instance of a waiter, call the ``waiters()`` method on the client object: |
| 73 | + |
| 74 | +.. code-block:: java |
| 75 | +
|
| 76 | + // Get the generated waiter container |
| 77 | + var waiters = client.waiters(); |
| 78 | +
|
| 79 | + // Get the configurable waiter from the container |
| 80 | + var orderCompletedWaiter = waiters.orderCompleted(); |
| 81 | +
|
| 82 | + // Wait for up to 2 seconds for the waiter to complete. |
| 83 | + orderCompletedWaiter.wait(input, Duration.ofSeconds(2)); |
0 commit comments