Skip to content

Commit

Permalink
Unify Pokemon model for Rust and Python servers (#2700)
Browse files Browse the repository at this point in the history
## Motivation and Context
Now we have the feature parity between Rust and Python servers (at least
for the Pokémon service's needs) we can use the same model in both.
Closes #1508

## Testing
```bash
$ cd smithy-rs/examples
$ make test # test Rust servers
$ cd python
$ make test # test Python servers
```

## Checklist
<!--- If a checkbox below is not applicable, then please DELETE it
rather than leaving it unchecked -->
- [ ] I have updated `CHANGELOG.next.toml` if I made changes to the
smithy-rs codegen or runtime crates
- [ ] I have updated `CHANGELOG.next.toml` if I made changes to the AWS
SDK, generated SDK code, or SDK runtime crates

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._
  • Loading branch information
unexge authored and david-perez committed May 18, 2023
1 parent 28742d5 commit 6023df2
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 150 deletions.
4 changes: 2 additions & 2 deletions codegen-client-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ val allCodegenTests = "../codegen-core/common-test-models".let { commonModels ->
imports = listOf("$commonModels/naming-obstacle-course-structs.smithy"),
),
CodegenTest("aws.protocoltests.json#TestService", "endpoint-rules"),
CodegenTest("com.aws.example.rust#PokemonService", "pokemon-service-client", imports = listOf("$commonModels/pokemon.smithy", "$commonModels/pokemon-common.smithy")),
CodegenTest("com.aws.example.rust#PokemonService", "pokemon-service-awsjson-client", imports = listOf("$commonModels/pokemon-awsjson.smithy", "$commonModels/pokemon-common.smithy")),
CodegenTest("com.aws.example#PokemonService", "pokemon-service-client", imports = listOf("$commonModels/pokemon.smithy", "$commonModels/pokemon-common.smithy")),
CodegenTest("com.aws.example#PokemonService", "pokemon-service-awsjson-client", imports = listOf("$commonModels/pokemon-awsjson.smithy", "$commonModels/pokemon-common.smithy")),
)
}

Expand Down
2 changes: 1 addition & 1 deletion codegen-core/common-test-models/pokemon-awsjson.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ $version: "1.0"
// This is a temporary model to test AwsJson 1.0 with @streaming.
// This model will be removed when protocol tests support @streaming.

namespace com.aws.example.rust
namespace com.aws.example

use aws.protocols#awsJson1_0
use smithy.framework#ValidationException
Expand Down
21 changes: 19 additions & 2 deletions codegen-core/common-test-models/pokemon.smithy
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$version: "1.0"

namespace com.aws.example.rust
namespace com.aws.example

use aws.protocols#restJson1
use smithy.framework#ValidationException
Expand All @@ -20,7 +20,8 @@ service PokemonService {
GetServerStatistics,
DoNothing,
CapturePokemon,
CheckHealth
CheckHealth,
StreamPokemonRadio
],
}

Expand Down Expand Up @@ -146,3 +147,19 @@ structure MasterBallUnsuccessful {

@error("client")
structure ThrottlingError {}

/// Fetch a radio song from the database and stream it back as a playable audio.
@readonly
@http(uri: "/radio", method: "GET")
operation StreamPokemonRadio {
output: StreamPokemonRadioOutput
}

@output
structure StreamPokemonRadioOutput {
@httpPayload
data: StreamingBlob
}

@streaming
blob StreamingBlob
4 changes: 2 additions & 2 deletions codegen-server-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ val allCodegenTests = "../codegen-core/common-test-models".let { commonModels ->
CodegenTest("com.amazonaws.ebs#Ebs", "ebs", imports = listOf("$commonModels/ebs.json")),
CodegenTest("com.amazonaws.s3#AmazonS3", "s3"),
CodegenTest(
"com.aws.example.rust#PokemonService",
"com.aws.example#PokemonService",
"pokemon-service-server-sdk",
imports = listOf("$commonModels/pokemon.smithy", "$commonModels/pokemon-common.smithy"),
),
CodegenTest(
"com.aws.example.rust#PokemonService",
"com.aws.example#PokemonService",
"pokemon-service-awsjson-server-sdk",
imports = listOf("$commonModels/pokemon-awsjson.smithy", "$commonModels/pokemon-common.smithy"),
),
Expand Down
6 changes: 5 additions & 1 deletion codegen-server-test/python/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ dependencies {
val allCodegenTests = "../../codegen-core/common-test-models".let { commonModels ->
listOf(
CodegenTest("com.amazonaws.simple#SimpleService", "simple", imports = listOf("$commonModels/simple.smithy")),
CodegenTest("com.aws.example.python#PokemonService", "pokemon-service-server-sdk"),
CodegenTest(
"com.aws.example#PokemonService",
"pokemon-service-server-sdk",
imports = listOf("$commonModels/pokemon.smithy", "$commonModels/pokemon-common.smithy"),
),
CodegenTest(
"com.amazonaws.ebs#Ebs", "ebs",
imports = listOf("$commonModels/ebs.json"),
Expand Down
1 change: 0 additions & 1 deletion codegen-server-test/python/model/pokemon-common.smithy

This file was deleted.

126 changes: 0 additions & 126 deletions codegen-server-test/python/model/pokemon.smithy

This file was deleted.

5 changes: 2 additions & 3 deletions examples/pokemon-service-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ rand = "0.8"
tracing = "0.1"
tracing-subscriber = { version = "0.3.16", features = ["env-filter", "json"] }
tokio = { version = "1", default-features = false, features = ["time"] }
tower = "0.4"

# Local paths
aws-smithy-client = { path = "../../rust-runtime/aws-smithy-client" }
aws-smithy-http = { path = "../../rust-runtime/aws-smithy-http" }
aws-smithy-http-server = { path = "../../rust-runtime/aws-smithy-http-server" }
pokemon-service-client = { path = "../pokemon-service-client" }
pokemon-service-server-sdk = { path = "../pokemon-service-server-sdk" }

[dev-dependencies]
tower = "0.4"
37 changes: 35 additions & 2 deletions examples/pokemon-service-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use std::{
};

use async_stream::stream;
use aws_smithy_http::operation::Request;
use aws_smithy_client::{conns, hyper_ext::Adapter};
use aws_smithy_http::{body::SdkBody, byte_stream::ByteStream, operation::Request};
use aws_smithy_http_server::Extension;
use http::{
uri::{Authority, Scheme},
Expand All @@ -24,7 +25,8 @@ use http::{
use pokemon_service_server_sdk::{
error, input, model, model::CapturingPayload, output, types::Blob,
};
use rand::Rng;
use rand::{seq::SliceRandom, Rng};
use tower::Service;
use tracing_subscriber::{prelude::*, EnvFilter};

const PIKACHU_ENGLISH_FLAVOR_TEXT: &str =
Expand Down Expand Up @@ -327,6 +329,37 @@ pub async fn check_health(_input: input::CheckHealthInput) -> output::CheckHealt
output::CheckHealthOutput {}
}

const RADIO_STREAMS: [&str; 2] = [
"https://ia800107.us.archive.org/33/items/299SoundEffectCollection/102%20Palette%20Town%20Theme.mp3",
"https://ia600408.us.archive.org/29/items/PocketMonstersGreenBetaLavenderTownMusicwwwFlvtoCom/Pocket%20Monsters%20Green%20Beta-%20Lavender%20Town%20Music-%5Bwww_flvto_com%5D.mp3",
];

/// Streams a random Pokémon song.
pub async fn stream_pokemon_radio(
_input: input::StreamPokemonRadioInput,
) -> output::StreamPokemonRadioOutput {
let radio_stream_url = RADIO_STREAMS
.choose(&mut rand::thread_rng())
.expect("`RADIO_STREAMS` is empty")
.parse::<Uri>()
.expect("Invalid url in `RADIO_STREAMS`");

let mut connector = Adapter::builder().build(conns::https());
let result = connector
.call(
http::Request::builder()
.uri(radio_stream_url)
.body(SdkBody::empty())
.unwrap(),
)
.await
.unwrap();

output::StreamPokemonRadioOutput {
data: ByteStream::new(result.into_body()),
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
3 changes: 2 additions & 1 deletion examples/pokemon-service-lambda/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use aws_smithy_http_server::{routing::LambdaHandler, AddExtensionLayer};

use pokemon_service_common::{
capture_pokemon, check_health, do_nothing, get_pokemon_species, get_server_statistics,
setup_tracing, State,
setup_tracing, stream_pokemon_radio, State,
};
use pokemon_service_lambda::get_storage_lambda;
use pokemon_service_server_sdk::PokemonService;
Expand All @@ -28,6 +28,7 @@ pub async fn main() {
.capture_pokemon(capture_pokemon)
.do_nothing(do_nothing)
.check_health(check_health)
.stream_pokemon_radio(stream_pokemon_radio)
.build()
.expect("failed to build an instance of PokemonService")
// Set up shared state and middlewares.
Expand Down
3 changes: 2 additions & 1 deletion examples/pokemon-service-tls/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use tokio_rustls::{

use pokemon_service_common::{
capture_pokemon, check_health, do_nothing, get_pokemon_species, get_server_statistics,
get_storage, setup_tracing, State,
get_storage, setup_tracing, stream_pokemon_radio, State,
};
use pokemon_service_server_sdk::PokemonService;
use pokemon_service_tls::{DEFAULT_ADDRESS, DEFAULT_PORT, DEFAULT_TEST_CERT, DEFAULT_TEST_KEY};
Expand Down Expand Up @@ -71,6 +71,7 @@ pub async fn main() {
.capture_pokemon(capture_pokemon)
.do_nothing(do_nothing)
.check_health(check_health)
.stream_pokemon_radio(stream_pokemon_radio)
.build()
.expect("failed to build an instance of PokemonService")
// Set up shared state and middlewares.
Expand Down
4 changes: 3 additions & 1 deletion examples/pokemon-service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use pokemon_service::{
do_nothing_but_log_request_ids, get_storage_with_local_approved, DEFAULT_ADDRESS, DEFAULT_PORT,
};
use pokemon_service_common::{
capture_pokemon, check_health, get_pokemon_species, get_server_statistics, setup_tracing, State,
capture_pokemon, check_health, get_pokemon_species, get_server_statistics, setup_tracing,
stream_pokemon_radio, State,
};
use pokemon_service_server_sdk::PokemonService;

Expand Down Expand Up @@ -67,6 +68,7 @@ pub async fn main() {
.capture_pokemon(capture_pokemon)
.do_nothing(do_nothing_but_log_request_ids)
.check_health(check_health)
.stream_pokemon_radio(stream_pokemon_radio)
.build()
.expect("failed to build an instance of PokemonService");

Expand Down
5 changes: 4 additions & 1 deletion examples/python/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ release: codegen
$(MAKE) generate-stubs
$(MAKE) build-wheel-release

run: build
run: build install-wheel
python3 $(CUR_DIR)/pokemon_service.py

run-release: release install-wheel
python3 $(CUR_DIR)/pokemon_service.py

py-check: install-wheel
Expand Down
Loading

0 comments on commit 6023df2

Please sign in to comment.