Skip to content

Commit

Permalink
Refactored QLDB code examples (#558)
Browse files Browse the repository at this point in the history
* 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()
  • Loading branch information
Doug-AWS committed Jun 30, 2021
1 parent 61ef37f commit 0a16d96
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 65 deletions.
8 changes: 0 additions & 8 deletions aws/sdk/examples/qldb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
26 changes: 10 additions & 16 deletions aws/sdk/examples/qldb/src/bin/create-ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,21 @@
*/

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>,

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

/// Whether to display additional runtime information
/// Whether to display additional runtime information.
#[structopt(short, long)]
verbose: bool,
}
Expand All @@ -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,
Expand All @@ -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!("Region: {:?}", &region);

SubscriberBuilder::default()
.with_env_filter("info")
.with_span_events(FmtSpan::CLOSE)
.init();
println!("QLDB version: {}", PKG_VERSION);
println!("Region: {:?}", &region);
println!();
}

let conf = Config::builder().region(region).build();
Expand Down
27 changes: 10 additions & 17 deletions aws/sdk/examples/qldb/src/bin/list-ledgers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,31 @@
*/

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,
}

/// 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,
Expand All @@ -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!("Region: {:?}", &region);

SubscriberBuilder::default()
.with_env_filter("info")
.with_span_events(FmtSpan::CLOSE)
.init();
println!("OLDB version: {}", PKG_VERSION);
println!("Region: {:?}", &region);
println!();
}

let conf = Config::builder().region(region).build();
Expand Down
42 changes: 18 additions & 24 deletions aws/sdk/examples/qldb/src/bin/qldb-helloworld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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!("Region: {:?}", &region);
println!("Ledger: {}", ledger);

SubscriberBuilder::default()
.with_env_filter("info")
.with_span_events(FmtSpan::CLOSE)
.init();
println!("OLDB version: {}", PKG_VERSION);
println!("Region: {:?}", &region);
println!("Ledger: {}", ledger);
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(())
}

0 comments on commit 0a16d96

Please sign in to comment.