Commit c1dedbcf authored by Doug's avatar Doug Committed by Russell Cohen
Browse files

Refactored QLDB code examples (#558)

* Refactored QLDB code examples to use common example pattern; re-ordered crates in Cargo.toml

* Updated QLDB helloworld code example to use ? instead of expect()
parent 72134a07
Loading
Loading
Loading
Loading
+0 −8
Original line number Diff line number Diff line
@@ -10,16 +10,8 @@ edition = "2018"
qldb = { package = "aws-sdk-qldb", path = "../../build/aws-sdk/qldb" }
qldbsession = { package = "aws-sdk-qldbsession", path = "../../build/aws-sdk/qldbsession" }
aws-types = { path = "../../build/aws-sdk/aws-types" }

tokio = { version = "1", features = ["full"] }

# For command-line arguments
structopt = { version = "0.3", default-features = false }

tracing-subscriber = { version = "0.2.16", features = ["fmt"] }

# used only for static endpoint configuration:
http = "0.2.3"
# used only to enable basic logging:
env_logger = "0.8.2"
+10 −16
Original line number Diff line number Diff line
@@ -4,17 +4,13 @@
 */

use aws_types::region::ProvideRegion;

use qldb::model::PermissionsMode;
use qldb::{Client, Config, Error, Region};

use qldb::{Client, Config, Error, Region, PKG_VERSION};
use structopt::StructOpt;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::fmt::SubscriberBuilder;

#[derive(Debug, StructOpt)]
struct Opt {
    /// The region. Overrides environment variable AWS_DEFAULT_REGION.
    /// The default AWS Region.
    #[structopt(short, long)]
    default_region: Option<String>,

@@ -22,7 +18,7 @@ struct Opt {
    #[structopt(short, long)]
    ledger: String,

    /// Whether to display additional runtime information
    /// Whether to display additional runtime information.
    #[structopt(short, long)]
    verbose: bool,
}
@@ -31,12 +27,14 @@ struct Opt {
/// # Arguments
///
/// * `-l LEDGER` - The name of the ledger.
/// * `[-d DEFAULT-REGION]` - The region in which the client is created.
///    If not supplied, uses the value of the **AWS_DEFAULT_REGION** environment variable.
/// * `[-d DEFAULT-REGION]` - The Region in which the client is created.
///    If not supplied, uses the value of the **AWS_REGION** environment variable.
///    If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.
#[tokio::main]
async fn main() -> Result<(), Error> {
    tracing_subscriber::fmt::init();

    let Opt {
        default_region,
        ledger,
@@ -50,13 +48,9 @@ async fn main() -> Result<(), Error> {
        .unwrap_or_else(|| Region::new("us-west-2"));

    if verbose {
        println!("QLDB client version: {}\n", qldb::PKG_VERSION);
        println!("QLDB version: {}", PKG_VERSION);
        println!("Region:       {:?}", &region);

        SubscriberBuilder::default()
            .with_env_filter("info")
            .with_span_events(FmtSpan::CLOSE)
            .init();
        println!();
    }

    let conf = Config::builder().region(region).build();
+10 −17
Original line number Diff line number Diff line
@@ -4,21 +4,16 @@
 */

use aws_types::region::ProvideRegion;

//use qldbsession::model::StartSessionRequest;
use qldb::{Client, Config, Error, Region};

use qldb::{Client, Config, Error, Region, PKG_VERSION};
use structopt::StructOpt;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::fmt::SubscriberBuilder;

#[derive(Debug, StructOpt)]
struct Opt {
    /// The region. Overrides environment variable AWS_DEFAULT_REGION.
    /// The default AWS Region.
    #[structopt(short, long)]
    default_region: Option<String>,

    /// Whether to display additional runtime information
    /// Whether to display additional information.
    #[structopt(short, long)]
    verbose: bool,
}
@@ -26,12 +21,14 @@ struct Opt {
/// Lists your Amazon QLDB ledgers.
/// # Arguments
///
/// * `[-d DEFAULT-REGION]` - The region in which the client is created.
///    If not supplied, uses the value of the **AWS_DEFAULT_REGION** environment variable.
/// * `[-d DEFAULT-REGION]` - The Region in which the client is created.
///    If not supplied, uses the value of the **AWS_REGION** environment variable.
///    If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.
#[tokio::main]
async fn main() -> Result<(), Error> {
    tracing_subscriber::fmt::init();

    let Opt {
        default_region,
        verbose,
@@ -44,13 +41,9 @@ async fn main() -> Result<(), Error> {
        .unwrap_or_else(|| Region::new("us-west-2"));

    if verbose {
        println!("OLDB client version: {}\n", qldb::PKG_VERSION);
        println!("OLDB version: {}", PKG_VERSION);
        println!("Region:       {:?}", &region);

        SubscriberBuilder::default()
            .with_env_filter("info")
            .with_span_events(FmtSpan::CLOSE)
            .init();
        println!();
    }

    let conf = Config::builder().region(region).build();
+18 −24
Original line number Diff line number Diff line
@@ -4,39 +4,38 @@
 */

use aws_types::region::ProvideRegion;

use qldbsession::model::StartSessionRequest;
use qldbsession::{Client, Config, Error, Region};
use qldbsession::{Client, Config, Error, Region, PKG_VERSION};

use structopt::StructOpt;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::fmt::SubscriberBuilder;

#[derive(Debug, StructOpt)]
struct Opt {
    /// The region. Overrides environment variable AWS_DEFAULT_REGION.
    /// The default AWS Region.
    #[structopt(short, long)]
    default_region: Option<String>,

    /// Specifies the ledger
    /// The name of the ledger.
    #[structopt(short, long)]
    ledger: String,

    /// Whether to display additional runtime information
    /// Whether to display additional information.
    #[structopt(short, long)]
    verbose: bool,
}

/// Creates a low-level Amazon QLDB session against a ledger.
/// Creates a low-level Amazon QLDB session.
/// # Arguments
///
/// * `-l LEDGER` - The name of the ledger to start a new session against.
/// * `[-d DEFAULT-REGION]` - The region in which the client is created.
///    If not supplied, uses the value of the **AWS_DEFAULT_REGION** environment variable.
/// * `[-d DEFAULT-REGION]` - The Region in which the client is created.
///    If not supplied, uses the value of the **AWS_REGION** environment variable.
///    If the environment variable is not set, defaults to **us-west-2**.
/// * `[-v]` - Whether to display additional information.
#[tokio::main]
async fn main() -> Result<(), Error> {
    tracing_subscriber::fmt::init();

    let Opt {
        ledger,
        default_region,
@@ -50,30 +49,25 @@ async fn main() -> Result<(), Error> {
        .unwrap_or_else(|| Region::new("us-west-2"));

    if verbose {
        println!("OLDB client version: {}\n", qldb::PKG_VERSION);
        println!("OLDB version: {}", PKG_VERSION);
        println!("Region:       {:?}", &region);
        println!("Ledger:       {}", ledger);

        SubscriberBuilder::default()
            .with_env_filter("info")
            .with_span_events(FmtSpan::CLOSE)
            .init();
        println!();
    }

    let conf = Config::builder().region(region).build();
    let client = Client::from_conf(conf);

    let result = client
        .send_command()
        .start_session(StartSessionRequest::builder().ledger_name(ledger).build())
        .send()
        .await?;

    match result.start_session {
        Some(s) => {
            println!("Your session id: {:?}", s.session_token);
        }
        None => unreachable!("a start session will result in an Err or a start session result"),
    }
    println!(
        "Session id: {:?}",
        result.start_session.unwrap().session_token
    );

    Ok(())
}