Skip to content

Commit

Permalink
Remove ID types from function args
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoniosBarotsis committed Jan 13, 2025
1 parent f619804 commit e1c3bf1
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 20 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ and this project adheres to

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

## [0.11.0] - 202-01-13

### Added

- `BroadcastId::new`

### Changed

- Made the following methods require `&str` instead of their specific ID types. This was changed to
make their usage a bit simpler from the user's perspective, after all, the ID types were meant to
be more like hints that a string is of an id type, rather than having specific properties in the
program itself. The ID types are still used in the return types for this reason and they are
also all constructable in case you want to use them. All the ID types dereference to a string
so `&IdType` is equivalent to a `&str` (you might need to make this change if you were
using any ID types directly before).
- `ApiKeys::delete`
- `Audiences::delete`
- `Broadcasts::get,delete`
- `Domains::delete`
- `SendBroadcastOptions::new` is no longer `const`

## [0.10.0] - 2024-12-13

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

Initial release.

[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
[0.9.1]: https://crates.io/crates/resend-rs/0.9.1
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.10.0"
version = "0.11.0"
edition = "2021"

license = "MIT"
Expand Down
6 changes: 3 additions & 3 deletions src/api_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;

use reqwest::Method;

use crate::types::{ApiKey, ApiKeyId, ApiKeyToken, CreateApiKeyOptions};
use crate::types::{ApiKey, ApiKeyToken, CreateApiKeyOptions};
use crate::{Config, Result};

/// `Resend` APIs for `/api-keys` endpoints.
Expand Down Expand Up @@ -42,7 +42,7 @@ impl ApiKeysSvc {
/// <https://resend.com/docs/api-reference/api-keys/delete-api-key>
#[maybe_async::maybe_async]
#[allow(clippy::needless_pass_by_value)]
pub async fn delete(&self, api_key_id: ApiKeyId) -> Result<()> {
pub async fn delete(&self, api_key_id: &str) -> Result<()> {
let path = format!("/api-keys/{api_key_id}");

let request = self.0.build(Method::DELETE, &path);
Expand Down Expand Up @@ -221,7 +221,7 @@ mod test {
let api_keys_amt = api_keys.len();

// Delete.
resend.api_keys.delete(id).await?;
resend.api_keys.delete(&id).await?;

// List.
let api_keys = resend.api_keys.list().await?;
Expand Down
6 changes: 3 additions & 3 deletions src/audiences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use reqwest::Method;
use crate::types::Audience;
use crate::{Config, Result};

use self::types::{AudienceId, CreateAudienceResponse};
use self::types::CreateAudienceResponse;

/// `Resend` APIs for `/audiences` endpoints.
#[derive(Clone)]
Expand Down Expand Up @@ -50,7 +50,7 @@ impl AudiencesSvc {
/// <https://resend.com/docs/api-reference/audiences/delete-audience>
#[maybe_async::maybe_async]
#[allow(clippy::needless_pass_by_value)]
pub async fn delete(&self, id: AudienceId) -> Result<bool> {
pub async fn delete(&self, id: &str) -> Result<bool> {
let path = format!("/audiences/{id}");

let request = self.0.build(Method::DELETE, &path);
Expand Down Expand Up @@ -192,7 +192,7 @@ mod test {
assert!(audiences_before > 1);

// Delete.
let deleted = resend.audiences.delete(id).await?;
let deleted = resend.audiences.delete(&id).await?;
assert!(deleted);

Ok(())
Expand Down
29 changes: 20 additions & 9 deletions src/broadcasts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use reqwest::Method;

use crate::types::{
Broadcast, BroadcastId, CreateBroadcastOptions, CreateBroadcastResponse,
Broadcast, CreateBroadcastOptions, CreateBroadcastResponse,
RemoveBroadcastResponse, SendBroadcastOptions, SendBroadcastResponse,
};
use crate::{Config, Result};
Expand Down Expand Up @@ -60,7 +60,7 @@ impl BroadcastsSvc {
/// <https://resend.com/docs/api-reference/broadcasts/get-broadcast>
#[maybe_async::maybe_async]
#[allow(clippy::needless_pass_by_value)]
pub async fn get(&self, broadcast_id: BroadcastId) -> Result<Broadcast> {
pub async fn get(&self, broadcast_id: &str) -> Result<Broadcast> {
let path = format!("/broadcasts/{broadcast_id}");

let request = self.0.build(Method::GET, &path);
Expand All @@ -73,7 +73,7 @@ impl BroadcastsSvc {
/// Remove an existing broadcast.
#[maybe_async::maybe_async]
#[allow(clippy::needless_pass_by_value)]
pub async fn delete(&self, broadcast_id: BroadcastId) -> Result<bool> {
pub async fn delete(&self, broadcast_id: &str) -> Result<bool> {
let path = format!("/broadcasts/{broadcast_id}");

let request = self.0.build(Method::DELETE, &path);
Expand Down Expand Up @@ -167,6 +167,15 @@ pub mod types {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BroadcastId(EcoString);

impl BroadcastId {
/// Creates a new [`BroadcastId`].
#[inline]
#[must_use]
pub fn new(id: &str) -> Self {
Self(EcoString::from(id))
}
}

impl Deref for BroadcastId {
type Target = str;

Expand Down Expand Up @@ -204,7 +213,9 @@ pub mod types {
}

impl SendBroadcastOptions {
pub const fn new(broadcast_id: BroadcastId) -> Self {
pub fn new(broadcast_id: &str) -> Self {
let broadcast_id = BroadcastId(EcoString::from(broadcast_id.to_owned()));

Self {
broadcast_id,
scheduled_at: None,
Expand Down Expand Up @@ -289,13 +300,13 @@ mod test {
std::thread::sleep(std::time::Duration::from_secs(2));

// Send
let opts = SendBroadcastOptions::new(res.id);
let opts = SendBroadcastOptions::new(&res.id);
let _res = resend.broadcasts.send(opts).await?;

// Cleanup
std::thread::sleep(std::time::Duration::from_secs(2));

let deleted = resend.audiences.delete(audience_id).await?;
let deleted = resend.audiences.delete(&audience_id).await?;
std::thread::sleep(std::time::Duration::from_secs(1));

assert!(deleted);
Expand All @@ -313,8 +324,8 @@ mod test {
assert!(!broadcasts.is_empty(), "No broadcasts found");
let broadcast = broadcasts[0].clone();

let _res = resend.broadcasts.get(broadcast.id.clone()).await?;
let deleted = resend.broadcasts.delete(broadcast.id).await;
let _res = resend.broadcasts.get(&broadcast.id.clone()).await?;
let deleted = resend.broadcasts.delete(&broadcast.id).await;
// Already used broadcasts cant be deleted
assert!(deleted.is_err());

Expand All @@ -327,7 +338,7 @@ mod test {
let broadcast = CreateBroadcastOptions::new(&audience_id, from, subject).with_text(text);
let res = resend.broadcasts.create(broadcast).await?;
std::thread::sleep(std::time::Duration::from_secs(2));
let deleted = resend.broadcasts.delete(res.id).await;
let deleted = resend.broadcasts.delete(&res.id).await;
std::thread::sleep(std::time::Duration::from_secs(1));

assert!(deleted.is_ok());
Expand Down
2 changes: 1 addition & 1 deletion src/contacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ mod test {
.await?;

// Delete audience.
let _ = resend.audiences.delete(audience_id.clone()).await?;
let _ = resend.audiences.delete(&audience_id.clone()).await?;
std::thread::sleep(std::time::Duration::from_secs(1));

// List.
Expand Down
6 changes: 3 additions & 3 deletions src/domains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use types::DeleteDomainResponse;
use crate::types::{CreateDomainOptions, Domain, DomainChanges};
use crate::{Config, Result};

use self::types::{DomainId, UpdateDomainResponse};
use self::types::UpdateDomainResponse;

/// `Resend` APIs for `/domains` endpoints.
#[derive(Clone)]
Expand Down Expand Up @@ -93,7 +93,7 @@ impl DomainsSvc {
/// <https://resend.com/docs/api-reference/domains/delete-domain>
#[maybe_async::maybe_async]
#[allow(clippy::needless_pass_by_value)]
pub async fn delete(&self, domain_id: DomainId) -> Result<DeleteDomainResponse> {
pub async fn delete(&self, domain_id: &str) -> Result<DeleteDomainResponse> {
let path = format!("/domains/{domain_id}");

let request = self.0.build(Method::DELETE, &path);
Expand Down Expand Up @@ -426,7 +426,7 @@ mod test {
std::thread::sleep(std::time::Duration::from_secs(4));

// Delete
let resp = resend.domains.delete(domain.id).await?;
let resp = resend.domains.delete(&domain.id).await?;
assert!(resp.deleted);

// List.
Expand Down

0 comments on commit e1c3bf1

Please sign in to comment.