Unverified Commit 7955da2e authored by Russell Cohen's avatar Russell Cohen Committed by GitHub
Browse files

Add support for Streaming Response Bodies (#338)

* Assert field-by-field equality in protocol tests

Protocol tests currently use `PartialEq`, but when we add streaming members, we can no longer compare equality synchronously & without
side effects. This diff removes `PartialEq` from output shapes (we may re add it later for non-streaming shapes) & instead asserts equality of each output member in turn.

* Cleaner version of body streaming, still some work to get it to work with tests

* Finalize protocol test support

* Add default to the operation parsers

* Add helper for isStreaming on member shapes

* Cleanups & add test

* allow upper case keys in rustTemplate
parent d4bf8e6e
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
audio.mp3
+0 −11
Original line number Diff line number Diff line
[package]
name = "polly-generate-speech"
version = "0.1.0"
authors = ["Russell Cohen <rcoh@amazon.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
polly = { package = "aws-sdk-polly", path = "../../build/aws-sdk/polly"}
tokio = { version = "1", features = ["full"] }
+0 −30
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

use polly::model::{Engine, OutputFormat, VoiceId};
use std::error::Error;
use tokio::fs::File;
use tokio::io::AsyncWriteExt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
    let client = polly::Client::from_env();
    let resp = client
        .synthesize_speech()
        .voice_id(VoiceId::Emma)
        .engine(Engine::Neural)
        .output_format(OutputFormat::Mp3)
        .text("Hello, I am polly!")
        .send()
        .await?;
    let audio = resp.audio_stream.expect("data should be included");
    let mut file = File::create("audio.mp3").await?;
    file.write_all(audio.as_ref()).await?;
    println!(
        "Audio written to audio.mp3 ({} bytes)",
        audio.as_ref().len()
    );
    Ok(())
}
+1 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ edition = "2018"

[dependencies]
polly = { package = "aws-sdk-polly", path = "../../build/aws-sdk/polly" }
bytes = "1"
tokio = { version = "1", features = ["full"] }
structopt = { version = "0.3", default-features = false }
tracing-subscriber = { version = "0.2.16", features = ["fmt"] }
+15 −4
Original line number Diff line number Diff line
@@ -10,7 +10,9 @@ use polly::{Client, Config, Region};

use aws_types::region::{EnvironmentProvider, ProvideRegion};

use bytes::Buf;
use structopt::StructOpt;
use tokio::io::AsyncWriteExt;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::fmt::SubscriberBuilder;

@@ -76,12 +78,21 @@ async fn main() {
    };

    // Get MP3 data from response and save it
    let blob = resp.audio_stream.expect("Could not get synthesized text");
    let bytes = blob.as_ref();
    let mut blob = resp
        .audio_stream
        .collect()
        .await
        .expect("failed to read data");

    // Create output filename from input filename
    let parts: Vec<&str> = filename.split('.').collect();
    let out_file = format!("{}{}", String::from(parts[0]), ".mp3");

    fs::write(out_file, bytes).expect("Could not write to file");
    let mut file = tokio::fs::File::create(out_file)
        .await
        .expect("failed to create file");
    while blob.has_remaining() {
        file.write_buf(&mut blob)
            .await
            .expect("failed to write to file");
    }
}
Loading