Skip to content

Commit

Permalink
Updated SES code examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug-AWS committed Nov 4, 2021
1 parent 7ede9b9 commit 8da7afd
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 120 deletions.
4 changes: 4 additions & 0 deletions ALPHA/ses/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ edition = "2018"
[dependencies]
aws-config = { path = "../../build/aws-sdk/aws-config" }
aws-sdk-ses = { package = "aws-sdk-sesv2", path = "../../build/aws-sdk/sesv2" }

aws-types = { path = "../../build/aws-sdk/aws-types" }

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

structopt = { version = "0.3", default-features = false }
tracing-subscriber = "0.2.18"
35 changes: 16 additions & 19 deletions ALPHA/ses/src/bin/create-contact-list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use aws_config::meta::region::RegionProviderChain;
use aws_sdk_ses::{Client, Error, Region, PKG_VERSION};

use structopt::StructOpt;

#[derive(Debug, StructOpt)]
Expand All @@ -15,57 +16,53 @@ struct Opt {

/// The AWS Region.
#[structopt(short, long)]
region: Option<String>,
default_region: Option<String>,

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

/// Creates a new contact list in the Region.
/// Creates a new contact list.
/// # Arguments
///
/// * `-c CONTACT-LIST` - The name of the contact list.
/// * `[-r REGION]` - The Region in which the client is created.
/// If not supplied, uses the value of the **AWS_REGION** environment variable.
/// * `[-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<(), Error> {
tracing_subscriber::fmt::init();

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

let region_provider = RegionProviderChain::first_try(region.map(Region::new))
let region_provider = RegionProviderChain::first_try(default_region.map(Region::new))
.or_default_provider()
.or_else(Region::new("us-west-2"));

println!();
let shared_config = aws_config::from_env().region(region_provider).load().await;

if verbose {
println!("SES client version: {}", PKG_VERSION);
println!(
"Region: {}",
region_provider.region().await.unwrap().as_ref()
);
println!("Region: {:?}", shared_config.region().unwrap());
println!("Contact list: {}", &contact_list);
println!();
}

let shared_config = aws_config::from_env().region(region_provider).load().await;
let client = Client::new(&shared_config);

client
let resp = client
.create_contact_list()
.contact_list_name(contact_list)
.send()
.await?;
.await;

println!("Created contact list.");
match resp {
Ok(_) => println!("Created contact list."),
Err(e) => eprintln!("Got an error attempting to create contact list: {}", e),
};

Ok(())
}
36 changes: 16 additions & 20 deletions ALPHA/ses/src/bin/create-contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use aws_config::meta::region::RegionProviderChain;
use aws_sdk_ses::{Client, Error, Region, PKG_VERSION};

use structopt::StructOpt;

#[derive(Debug, StructOpt)]
Expand All @@ -15,65 +16,60 @@ struct Opt {

/// The AWS Region.
#[structopt(short, long)]
region: Option<String>,
default_region: Option<String>,

/// The email address of the contact to add to the contact list.
#[structopt(short, long)]
email_address: String,

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

/// Adds a new contact to the contact list in the Region.
/// Adds a new contact to the contact list.
/// # Arguments
///
/// * `-c CONTACT-LIST` - The name of the contact list.
/// * `-e EMAIL-ADDRESS` - The email address of the contact to add to the contact list.
/// * `[-r REGION]` - The Region in which the client is created.
/// If not supplied, uses the value of the **AWS_REGION** environment variable.
/// * `[-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<(), Error> {
tracing_subscriber::fmt::init();

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

let region_provider = RegionProviderChain::first_try(region.map(Region::new))
let region_provider = RegionProviderChain::first_try(default_region.map(Region::new))
.or_default_provider()
.or_else(Region::new("us-west-2"));

println!();
let shared_config = aws_config::from_env().region(region_provider).load().await;

if verbose {
println!("SES client version: {}", PKG_VERSION);
println!(
"Region: {}",
region_provider.region().await.unwrap().as_ref()
);
println!("Region: {:?}", shared_config.region().unwrap());
println!("Contact list: {}", &contact_list);
println!("Email address: {}", &email_address);
println!();
}

let shared_config = aws_config::from_env().region(region_provider).load().await;
let client = Client::new(&shared_config);

client
let new_contact = client
.create_contact()
.contact_list_name(contact_list)
.email_address(email_address)
.send()
.await?;

println!("Created contact");
.await;
match new_contact {
Ok(_) => println!("Created contact"),
Err(e) => eprintln!("Got error attempting to create contact: {}", e),
};

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ struct Opt {
verbose: bool,
}

// Is email address has been verified?
// snippet-start:[ses.rust.is-email-verfied]
async fn is_verified(client: &Client, email: &str) -> Result<(), Error> {
let resp = client
.get_email_identity()
.email_identity(email)
.send()
.await?;

if resp.verified_for_sending_status {
println!("The address is verified");
} else {
println!("The address is not verified");
}

Ok(())
}
// snippet-end:[ses.rust.is-email-verfied]

/// Determines whether the email address has been verified.
/// # Arguments
///
Expand Down Expand Up @@ -59,17 +78,5 @@ async fn main() -> Result<(), Error> {
let shared_config = aws_config::from_env().region(region_provider).load().await;
let client = Client::new(&shared_config);

let resp = client
.get_email_identity()
.email_identity(email_address)
.send()
.await?;

if resp.verified_for_sending_status {
println!("The address is verified");
} else {
println!("The address is not verified");
}

Ok(())
is_verified(&client, &email_address).await
}
40 changes: 16 additions & 24 deletions ALPHA/ses/src/bin/list-contact-lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,51 @@

use aws_config::meta::region::RegionProviderChain;
use aws_sdk_ses::{Client, Error, Region, PKG_VERSION};

use structopt::StructOpt;

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

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

/// Lists your contact lists (there should only be one) in the Region.
/// Lists your contact lists (there should only be one).
/// # Arguments
///
/// * `[-r REGION]` - The Region in which the client is created.
/// If not supplied, uses the value of the **AWS_REGION** environment variable.
/// * `[-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<(), Error> {
tracing_subscriber::fmt::init();

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

let region_provider = RegionProviderChain::first_try(region.map(Region::new))
let region_provider = RegionProviderChain::first_try(default_region.map(Region::new))
.or_default_provider()
.or_else(Region::new("us-west-2"));

println!();
let shared_config = aws_config::from_env().region(region_provider).load().await;

if verbose {
println!("SES client version: {}", PKG_VERSION);
println!(
"Region: {}",
region_provider.region().await.unwrap().as_ref()
);
println!("Region: {:?}", shared_config.region().unwrap());
println!();
}

let shared_config = aws_config::from_env().region(region_provider).load().await;
let client = Client::new(&shared_config);

let resp = client.list_contact_lists().send().await?;

println!("Contact lists:");
let resp = client.list_contact_lists().send().await;

for list in resp.contact_lists.unwrap_or_default() {
println!(
" {}",
list.contact_list_name.as_deref().unwrap_or_default()
);
for list in resp.unwrap().contact_lists.unwrap_or_default() {
println!("{}", list.contact_list_name.as_deref().unwrap_or_default());
}

Ok(())
Expand Down
34 changes: 13 additions & 21 deletions ALPHA/ses/src/bin/list-contacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use aws_config::meta::region::RegionProviderChain;
use aws_sdk_ses::{Client, Error, Region, PKG_VERSION};

use structopt::StructOpt;

#[derive(Debug, StructOpt)]
Expand All @@ -15,60 +16,51 @@ struct Opt {

/// The AWS Region.
#[structopt(short, long)]
region: Option<String>,
default_region: Option<String>,

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

/// Lists the contacts in a contact list in the Region.
/// Lists the contacts in a contact list.
/// # Arguments
///
/// * `-c CONTACT-LIST` - The name of the contact list.
/// * `[-r REGION]` - The Region in which the client is created.
/// If not supplied, uses the value of the **AWS_REGION** environment variable.
/// * `[-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<(), Error> {
tracing_subscriber::fmt::init();

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

let region_provider = RegionProviderChain::first_try(region.map(Region::new))
let region_provider = RegionProviderChain::first_try(default_region.map(Region::new))
.or_default_provider()
.or_else(Region::new("us-west-2"));

println!();
let shared_config = aws_config::from_env().region(region_provider).load().await;

if verbose {
println!("SES client version: {}", PKG_VERSION);
println!(
"Region: {}",
region_provider.region().await.unwrap().as_ref()
);
println!("Region: {:?}", shared_config.region().unwrap());
println!("Contact list: {}", &contact_list);
println!();
}

let shared_config = aws_config::from_env().region(region_provider).load().await;
let client = Client::new(&shared_config);

let resp = client
.list_contacts()
.contact_list_name(contact_list)
.send()
.await?;

println!("Contacts:");
.await;

for contact in resp.contacts.unwrap_or_default() {
println!(" {}", contact.email_address.as_deref().unwrap_or_default());
for contact in resp.unwrap().contacts.unwrap_or_default() {
println!("{}", contact.email_address.as_deref().unwrap_or_default());
}

Ok(())
Expand Down
Loading

0 comments on commit 8da7afd

Please sign in to comment.