Skip to content

Commit 26e410d

Browse files
authored
Bootstrap quickstart kotlin example
bootstrap quickstart kotlin example
2 parents c9c6cf9 + e51c674 commit 26e410d

File tree

24 files changed

+669
-0
lines changed

24 files changed

+669
-0
lines changed

settings.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ include(":smithy-java-examples:integ")
6464
// ---- Smithy-Rust examples ----
6565
// templates
6666
includeBuild("smithy-rs-examples/quickstart-rust")
67+
68+
// ---- Smithy-Kotlin examples ----
69+
// templates
70+
includeBuild("smithy-kotlin-examples/quickstart-kotlin")

smithy-kotlin-examples/README.MD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Smithy Kotlin
2+
The examples in this directory demonstrate the use of the [Smithy Kotlin](https://github.com/smithy-lang/smithy-kotlin) code generator for clients.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## Smithy-Kotlin Quickstart
2+
3+
This project provides a template to get started using [Smithy Kotlin](https://github.com/smithy-lang/smithy-kotlin)
4+
to create Kotlin clients.
5+
6+
### Layout
7+
8+
- `client/`: Code generated Kotlin client that can call the server.
9+
- `smithy/`: Common package for the service API model. Shared by both client and server.
10+
- `server/`: Code generated Java Server that implements stubbed operations code-generated from the service model.
11+
12+
### Usage
13+
14+
To create a new project from this template, use the [Smithy CLI](https://smithy.io/2.0/guides/smithy-cli/index.html)
15+
`init` command as follows:
16+
17+
```console
18+
smithy init -t smithy-kotlin-quickstart
19+
```
20+
21+
### Running and testing client
22+
23+
To run and test the client, first start a server by running `./gradlew :server:run` from the root of a project created from this
24+
template. That will start the server running on port `8888`.
25+
26+
Once the server is running you can run the client application in the `client` subproject
27+
28+
```console
29+
./gradlew :client:run
30+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
// Add repositories for all subprojects to resolve dependencies.
3+
allprojects {
4+
repositories {
5+
mavenLocal()
6+
mavenCentral()
7+
}
8+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
description = "Coffee shop service client"
2+
3+
plugins {
4+
application
5+
kotlin("jvm") version "2.3.0"
6+
// Executes smithy-build process to generate client code
7+
id("software.amazon.smithy.gradle.smithy-base")
8+
}
9+
10+
// Add generated client source code to the main sourceSet
11+
afterEvaluate {
12+
val clientPath = smithy.getPluginProjectionPath(smithy.sourceProjection.get(), "kotlin-codegen")
13+
sourceSets.main.get().kotlin.srcDir(clientPath)
14+
}
15+
16+
tasks.named("compileKotlin") {
17+
dependsOn("smithyBuild")
18+
}
19+
20+
dependencies {
21+
// Code generators
22+
compileOnly(libs.smithy.kotlin.aws.codegen)
23+
24+
// Service model
25+
implementation(project(":smithy"))
26+
27+
// Client Dependencies
28+
implementation(libs.smithy.kotlin.runtime.core)
29+
implementation(libs.smithy.kotlin.smithy.client)
30+
implementation(libs.smithy.kotlin.http.client)
31+
implementation(libs.smithy.kotlin.telemetry.api)
32+
implementation(libs.smithy.kotlin.telemetry.defaults)
33+
implementation(libs.smithy.kotlin.rpcv2.protocol)
34+
implementation(libs.smithy.kotlin.aws.protocol.core)
35+
implementation(libs.smithy.kotlin.aws.signing.common)
36+
implementation(libs.smithy.kotlin.serde)
37+
implementation(libs.smithy.kotlin.serde.cbor)
38+
implementation(libs.smithy.kotlin.http.client.engine.default)
39+
implementation(libs.kotlinx.coroutines.core)
40+
}
41+
42+
val optinAnnotations = listOf("kotlin.RequiresOptIn", "aws.smithy.kotlin.runtime.InternalApi")
43+
kotlin.sourceSets.all {
44+
optinAnnotations.forEach { languageSettings.optIn(it) }
45+
}
46+
47+
application {
48+
mainClass.set("io.smithy.kotlin.client.example.MainKt")
49+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"version": "1.0",
3+
"plugins": {
4+
"kotlin-codegen": {
5+
"service": "com.example#CoffeeShop",
6+
"sdkId": "CoffeeShop",
7+
"package": {
8+
"name": "io.smithy.kotlin.client.example",
9+
"version": "0.0.1"
10+
},
11+
"build": {
12+
"rootProject": false,
13+
"generateDefaultBuildFiles": false
14+
},
15+
"api": {
16+
"nullabilityCheckMode": "client"
17+
}
18+
}
19+
}
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.smithy.kotlin.client.example
2+
3+
import aws.smithy.kotlin.runtime.client.endpoints.Endpoint
4+
import io.smithy.kotlin.client.example.endpoints.CoffeeShopEndpointProvider
5+
import io.smithy.kotlin.client.example.model.CoffeeType
6+
import kotlinx.coroutines.delay
7+
import kotlinx.coroutines.runBlocking
8+
import java.util.logging.Logger
9+
import kotlin.time.Duration.Companion.seconds
10+
11+
fun main(): Unit = runBlocking {
12+
val logger: Logger = Logger.getLogger("SmithyKotlinClient")
13+
14+
CoffeeShopClient {
15+
endpointProvider = CoffeeShopEndpointProvider {
16+
Endpoint("http://localhost:8888")
17+
}
18+
}.use { client ->
19+
logger.info("Creating coffee order")
20+
21+
val createOrderResponse = client.createOrder {
22+
coffeeType = CoffeeType.Latte
23+
}
24+
25+
logger.info("Created order with ID: ${createOrderResponse.id}")
26+
27+
logger.info("Waiting for order to complete.")
28+
delay(5.seconds)
29+
30+
logger.info("Checking order status")
31+
val orderStatus = client.getOrder {
32+
id = createOrderResponse.id
33+
}.status
34+
35+
logger.info("Order status: $orderStatus")
36+
}
37+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[versions]
2+
smithy-version="1.63.0"
3+
smithy-java-version="0.0.1"
4+
smithy-kotlin-codegen-version="0.35.25"
5+
smithy-kotlin-runtime-version="1.5.25"
6+
coroutines-core-version="1.10.2"
7+
8+
[libraries]
9+
smithy-aws-traits = { module="software.amazon.smithy:smithy-aws-traits", version.ref = "smithy-version" }
10+
11+
smithy-java-plugins = { module="software.amazon.smithy.java.codegen:plugins", version.ref = "smithy-java-version" }
12+
smithy-java-server-netty = { module="software.amazon.smithy.java:server-netty", version.ref = "smithy-java-version" }
13+
smithy-java-aws-server-restjson = { module="software.amazon.smithy.java:aws-server-restjson", version.ref = "smithy-java-version" }
14+
smithy-java-aws-server-rpcv2-cbor = { module="software.amazon.smithy.java:server-rpcv2-cbor", version.ref = "smithy-java-version" }
15+
16+
smithy-kotlin-aws-codegen = { module = "software.amazon.smithy.kotlin:smithy-aws-kotlin-codegen", version.ref = "smithy-kotlin-codegen-version" }
17+
smithy-kotlin-runtime-core = { module = "aws.smithy.kotlin:runtime-core", version.ref = "smithy-kotlin-runtime-version" }
18+
smithy-kotlin-smithy-client = { module = "aws.smithy.kotlin:smithy-client", version.ref = "smithy-kotlin-runtime-version" }
19+
smithy-kotlin-http-client = { module = "aws.smithy.kotlin:http-client", version.ref = "smithy-kotlin-runtime-version" }
20+
smithy-kotlin-telemetry-api = { module = "aws.smithy.kotlin:telemetry-api", version.ref = "smithy-kotlin-runtime-version" }
21+
smithy-kotlin-telemetry-defaults = { module = "aws.smithy.kotlin:telemetry-defaults", version.ref = "smithy-kotlin-runtime-version" }
22+
smithy-kotlin-rpcv2-protocol = { module = "aws.smithy.kotlin:smithy-rpcv2-protocols", version.ref = "smithy-kotlin-runtime-version" }
23+
smithy-kotlin-aws-protocol-core = { module = "aws.smithy.kotlin:aws-protocol-core", version.ref = "smithy-kotlin-runtime-version" }
24+
smithy-kotlin-aws-signing-common = { module = "aws.smithy.kotlin:aws-signing-common", version.ref = "smithy-kotlin-runtime-version" }
25+
smithy-kotlin-serde = { module = "aws.smithy.kotlin:serde", version.ref = "smithy-kotlin-runtime-version" }
26+
smithy-kotlin-serde-cbor = { module = "aws.smithy.kotlin:serde-cbor", version.ref = "smithy-kotlin-runtime-version" }
27+
smithy-kotlin-http-client-engine-default = { module = "aws.smithy.kotlin:http-client-engine-default", version.ref = "smithy-kotlin-runtime-version" }
28+
29+
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines-core-version" }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
* Example file license header.
3+
* File header line two
4+
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
description = "Coffee shop service server implementation"
2+
3+
plugins {
4+
`java-library`
5+
application
6+
// Executes smithy-build process to generate server stubs
7+
id("software.amazon.smithy.gradle.smithy-base")
8+
}
9+
10+
dependencies {
11+
// Code generators
12+
smithyBuild(libs.smithy.java.plugins)
13+
14+
// Service model
15+
implementation(project(":smithy"))
16+
17+
// Server dependencies
18+
// Adds an HTTP server implementation based on netty
19+
implementation(libs.smithy.java.server.netty)
20+
// Adds a server implementation of the `RestJson1` protocol
21+
implementation(libs.smithy.java.aws.server.restjson)
22+
// Adds a server implementation of the `Rpcv2Cbor` protocol
23+
implementation(libs.smithy.java.aws.server.rpcv2.cbor)
24+
}
25+
26+
// Add generated source code to the compilation sourceSet
27+
afterEvaluate {
28+
val serverPath = smithy.getPluginProjectionPath(smithy.sourceProjection.get(), "java-server-codegen")
29+
sourceSets.main.get().java.srcDir(serverPath)
30+
}
31+
32+
tasks.named("compileJava") {
33+
dependsOn("smithyBuild")
34+
}
35+
36+
// Use the application plugin to start the service via the `run` task.
37+
application {
38+
mainClass = "io.smithy.kotlin.server.example.CoffeeShopService"
39+
}
40+

0 commit comments

Comments
 (0)