Unverified Commit c75a58f7 authored by Doug's avatar Doug Committed by GitHub
Browse files

Made SNS hello-world code example take a command-line argument for email address (#511)



Co-authored-by: default avatarRussell Cohen <rcoh@amazon.com>
parent 3749a6c9
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
[package]
name = "sns"
name = "sns-code-examples"
version = "0.1.0"
authors = ["Russell Cohen <rcoh@amazon.com>"]
authors = ["Russell Cohen <rcoh@amazon.com>", "Doug Schwartz <dougsch@amazon.com>"]
edition = "2018"

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

[dependencies]
sns = { package = "aws-sdk-sns", path = "../../build/aws-sdk/sns" }
aws-types = { path = "../../build/aws-sdk/aws-types" }

tokio = { version = "1", features = ["full"] }
tracing-subscriber = "0.2.18"

[[bin]]
name = "sns-hello-world"
structopt = { version = "0.3", default-features = false }
+60 −10
Original line number Diff line number Diff line
@@ -3,16 +3,63 @@
 * SPDX-License-Identifier: Apache-2.0.
 */

use sns::Region;
use sns::{Client, Config, Region};
use std::process::exit;

use aws_types::region::ProvideRegion;

use structopt::StructOpt;

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

    /// Specifies the email address to subscribe to the topic
    #[structopt(short, long)]
    email_address: String,

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

/// Subscribes an email address and publishes a message to a topic.
/// If the email address has not been confirmed for the topic,
/// a confirmation request is also sent to the email address.
/// # Arguments
///
/// * `-e EMAIL_ADDRESS` - The email address of a user subscribing to the topic.
/// * `[-d DEFAULT-REGION]` - The region in which the client is created.
///    If not supplied, uses the value of the **AWS_DEFAULT_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<(), sns::Error> {
    tracing_subscriber::fmt::init();
    let conf = sns::Config::builder()
        .region(Region::new("us-east-2"))
        .build();
    let client = sns::Client::from_conf(conf);

    let Opt {
        default_region,
        email_address,
        verbose,
    } = Opt::from_args();

    let region = default_region
        .as_ref()
        .map(|region| Region::new(region.clone()))
        .or_else(|| aws_types::region::default_provider().region())
        .unwrap_or_else(|| Region::new("us-west-2"));

    if verbose {
        println!("SNS client version: {}", sns::PKG_VERSION);
        println!("Region:             {:?}", &region);
        println!("Email address:      {}", &email_address);
    }

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

    let topics = client.list_topics().send().await?;
    let mut topics = topics.topics.unwrap_or_default();
    let topic_arn = match topics.pop() {
@@ -22,15 +69,18 @@ async fn main() -> Result<(), sns::Error> {
            exit(1);
        }
    };
    println!("receiving on `{}`", topic_arn);

    println!("Receiving on topic with ARN: `{}`", topic_arn);

    let rsp = client
        .subscribe()
        .topic_arn(&topic_arn)
        .protocol("email")
        .endpoint("some.email.address@example.com")
        .endpoint(email_address)
        .send()
        .await?;
    println!("added a subscription: {:?}", rsp);

    println!("Added a subscription: {:?}", rsp);

    let rsp = client
        .publish()
@@ -38,8 +88,8 @@ async fn main() -> Result<(), sns::Error> {
        .message("hello sns!")
        .send()
        .await?;
    println!("published a message: {:?}", rsp);

    // If you set this to your email address, you should get an email from SNS
    println!("Published message: {:?}", rsp);

    Ok(())
}