Skip to content

Commit

Permalink
Added contact update by id/email
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoniosBarotsis committed Jan 16, 2025
1 parent f769cb1 commit 60da9ca
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@ and this project adheres to

<!-- ## [Unreleased] -->

## [0.11.0] - 202-01-13
## [0.11.1] - 2025-01-16

### Added

- `Contacts::update_by_id`, `Contacts::update_by_email`

### Changed

- `Contacts::update` has been deprecated in favor of the above methods. The ability to use emails
instead of ids was just added to the Resend backend.

## [0.11.0] - 2025-01-13

### Added

Expand Down Expand Up @@ -207,6 +218,7 @@ Disabled `reqwest`'s default features and enabled `rustls-tls`.

Initial release.

[0.11.1]: https://crates.io/crates/resend-rs/0.10.0
[0.11.0]: https://crates.io/crates/resend-rs/0.10.0
[0.10.0]: https://crates.io/crates/resend-rs/0.10.0
[0.9.2]: https://crates.io/crates/resend-rs/0.9.2
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[package]
name = "resend-rs"
version = "0.11.0"
version = "0.11.1"
edition = "2021"

license = "MIT"
Expand Down
55 changes: 52 additions & 3 deletions src/contacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ impl ContactsSvc {
#[maybe_async::maybe_async]
// Reasoning for allow: https://github.com/resend/resend-rust/pull/1#issuecomment-2081646115
#[allow(clippy::needless_pass_by_value)]
#[deprecated(
since = "0.11.1",
note = "Use `update_by_id` instead or alternatively `update_by_email`. This method will likely be removed in the future."
)]
pub async fn update(
&self,
contact_id: &str,
Expand All @@ -66,14 +70,56 @@ impl ContactsSvc {
Ok(content)
}

/// Updates an existing contact identified by its id.
///
/// <https://resend.com/docs/api-reference/contacts/update-contact>
#[maybe_async::maybe_async]
// Reasoning for allow: https://github.com/resend/resend-rust/pull/1#issuecomment-2081646115
#[allow(clippy::needless_pass_by_value)]
pub async fn update_by_id(
&self,
contact_id: &str,
audience_id: &str,
update: ContactChanges,
) -> Result<UpdateContactResponse> {
let path = format!("/audiences/{audience_id}/contacts/{contact_id}");

let request = self.0.build(Method::PATCH, &path);
let response = self.0.send(request.json(&update)).await?;
let content = response.json::<UpdateContactResponse>().await?;

Ok(content)
}

/// Updates an existing contact identified by its email.
///
/// <https://resend.com/docs/api-reference/contacts/update-contact>
#[maybe_async::maybe_async]
// Reasoning for allow: https://github.com/resend/resend-rust/pull/1#issuecomment-2081646115
#[allow(clippy::needless_pass_by_value)]
pub async fn update_by_email(
&self,
contact_email: &str,
audience_id: &str,
update: ContactChanges,
) -> Result<UpdateContactResponse> {
let path = format!("/audiences/{audience_id}/contacts/{contact_email}");

let request = self.0.build(Method::PATCH, &path);
let response = self.0.send(request.json(&update)).await?;
let content = response.json::<UpdateContactResponse>().await?;

Ok(content)
}

/// Removes an existing contact from an audience by their email.
///
/// Returns whether the contact was deleted successfully.
///
/// <https://resend.com/docs/api-reference/contacts/delete-contact>
#[maybe_async::maybe_async]
pub async fn delete_by_email(&self, audience_id: &str, email: &str) -> Result<bool> {
let path = format!("/audiences/{audience_id}/contacts/{email}");
pub async fn delete_by_email(&self, audience_id: &str, contact_email: &str) -> Result<bool> {
let path = format!("/audiences/{audience_id}/contacts/{contact_email}");

let request = self.0.build(Method::DELETE, &path);
let response = self.0.send(request).await?;
Expand Down Expand Up @@ -324,7 +370,10 @@ mod test {

// Update.
let changes = ContactChanges::new().with_unsubscribed(true);
let _res = resend.contacts.update(&id, &audience_id, changes).await?;
let _res = resend
.contacts
.update_by_id(&id, &audience_id, changes)
.await?;
std::thread::sleep(std::time::Duration::from_secs(1));

// Retrieve.
Expand Down

0 comments on commit 60da9ca

Please sign in to comment.