From 8665b4a9baef1ad9c4bf90d99b38d8e2a268036f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20H=C3=B6rl?= Date: Wed, 28 Aug 2024 20:05:19 +0200 Subject: [PATCH] use different api version for sms api; provide email adress as fallback name for email api; --- .gitignore | 73 +++++++++++++++++++++++++++++++++++++- Cargo.lock | 2 +- Cargo.toml | 2 +- examples/email.rs | 8 ++--- src/communication.rs | 2 -- src/communication/email.rs | 6 ++-- src/communication/sms.rs | 9 +++-- src/types.rs | 4 +-- 8 files changed, 88 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..8f76b59 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,72 @@ -/target +# Created by https://www.toptal.com/developers/gitignore/api/rust,macos,visualstudiocode +# Edit at https://www.toptal.com/developers/gitignore?templates=rust,macos,visualstudiocode + +### macOS ### +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### macOS Patch ### +# iCloud generated files +*.icloud + +### Rust ### +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +# End of https://www.toptal.com/developers/gitignore/api/rust,macos,visualstudiocode \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index e44e1b9..2771866 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -46,7 +46,7 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "azure-communications" -version = "0.1.0" +version = "0.1.1" dependencies = [ "anyhow", "base64", diff --git a/Cargo.toml b/Cargo.toml index 80ba61d..c0a8306 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "azure-communications" -version = "0.1.0" +version = "0.1.1" edition = "2021" description = "API Wrapper for the Azure Communication Services in Rust." repository = "https://github.com/AlexanderProd/azure-communications-rust" diff --git a/examples/email.rs b/examples/email.rs index b837454..0b316a8 100644 --- a/examples/email.rs +++ b/examples/email.rs @@ -9,10 +9,10 @@ async fn main() { let sender_adress = env::var("SENDER_ADDRESS").expect("Missing SENDER_ADDRESS"); - let recipients = vec![Recipient { - address: env::var("RECIPIENT_ADDRESS").expect("Missing RECIPIENT_ADDRESS"), - display_name: None, - }]; + let recipients = vec![Recipient::new( + &env::var("RECIPIENT_ADDRESS").expect("Missing RECIPIENT_ADDRESS"), + None, + )]; let az_communications = AzureCommunicationService::new(&connection_string, None); diff --git a/src/communication.rs b/src/communication.rs index fba7bb3..ea0fb79 100644 --- a/src/communication.rs +++ b/src/communication.rs @@ -12,8 +12,6 @@ use time::OffsetDateTime; pub mod email; pub mod sms; -const AZURE_API_VERSION: &str = "2023-03-31"; - #[derive(Debug, Clone)] pub struct AzureCommunicationService { pub endpoint: String, diff --git a/src/communication/email.rs b/src/communication/email.rs index 5ee972d..3c9131d 100644 --- a/src/communication/email.rs +++ b/src/communication/email.rs @@ -4,7 +4,9 @@ use serde_json::json; use crate::types::Recipient; -use super::{AzureCommunicationService, AZURE_API_VERSION}; +use super::AzureCommunicationService; + +const API_VERSION: &str = "2023-03-31"; impl AzureCommunicationService { pub async fn send_mail( @@ -19,7 +21,7 @@ impl AzureCommunicationService { let url = Url::parse(&format!( "{}emails:send?api-version={}", - endpoint, AZURE_API_VERSION + endpoint, API_VERSION ))?; let body = json!({ diff --git a/src/communication/sms.rs b/src/communication/sms.rs index efe3e5e..fda5052 100644 --- a/src/communication/sms.rs +++ b/src/communication/sms.rs @@ -2,7 +2,9 @@ use anyhow::Result; use reqwest::Url; use serde_json::json; -use super::{AzureCommunicationService, AZURE_API_VERSION}; +use super::AzureCommunicationService; + +const API_VERSION: &str = "2021-03-07"; impl AzureCommunicationService { /// Message must not exceed 160 characters. @@ -15,10 +17,7 @@ impl AzureCommunicationService { ) -> Result<()> { let endpoint = &self.endpoint; - let url = Url::parse(&format!( - "{}sms?api-version={}", - endpoint, AZURE_API_VERSION - ))?; + let url = Url::parse(&format!("{}sms?api-version={}", endpoint, API_VERSION))?; let body = json!({ "from": sender_name, diff --git a/src/types.rs b/src/types.rs index 7958e11..3b9d257 100644 --- a/src/types.rs +++ b/src/types.rs @@ -3,14 +3,14 @@ use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Recipient { pub address: String, - pub display_name: Option, + pub display_name: String, } impl Recipient { pub fn new(address: &str, display_name: Option<&str>) -> Self { Recipient { address: address.to_string(), - display_name: display_name.map(|s| s.to_string()), + display_name: display_name.unwrap_or(address).to_string(), } } }