Skip to content

Commit

Permalink
Merge pull request #13 from kimbauters/dry-run
Browse files Browse the repository at this point in the history
Dry run
  • Loading branch information
rj76 authored Jun 26, 2024
2 parents bbbd91c + afcdaaa commit 1f4d70c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct FcmClientBuilder {
service_account_key_json_path: Option<PathBuf>,
token_cache_json_path: Option<PathBuf>,
fcm_request_timeout: Option<Duration>,
dry_run: Option<bool>,
}

impl FcmClientBuilder {
Expand Down Expand Up @@ -86,6 +87,11 @@ impl FcmClientBuilder {
self
}

pub fn dry_run(mut self, dry_run: bool) -> Self {
self.dry_run = Some(dry_run);
self
}

pub async fn build(self) -> Result<FcmClient, FcmClientError> {
FcmClient::new_from_builder(self).await
}
Expand All @@ -95,6 +101,7 @@ impl FcmClientBuilder {
pub struct FcmClient {
http_client: reqwest::Client,
oauth_client: OauthClient,
dry_run: bool,
}

impl FcmClient {
Expand Down Expand Up @@ -130,6 +137,7 @@ impl FcmClient {
Ok(FcmClient {
http_client,
oauth_client,
dry_run: fcm_builder.dry_run.unwrap_or(false),
})
}

Expand All @@ -150,7 +158,7 @@ impl FcmClient {
.http_client
.post(&url)
.bearer_auth(access_token)
.json(&MessageWrapper::new(message.as_ref()))
.json(&MessageWrapper::new(message.as_ref(), self.dry_run))
.build()?;

let response = self.http_client.execute(request).await?;
Expand Down
13 changes: 11 additions & 2 deletions src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,22 @@ impl AsRef<Message> for Message {
}

/// Wrap the message in a "message" field
fn is_validate_only_default(b: &bool) -> bool {
*b == false

Check warning on line 86 in src/message/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

equality checks against false can be replaced by a negation

warning: equality checks against false can be replaced by a negation --> src/message/mod.rs:86:5 | 86 | *b == false | ^^^^^^^^^^^ help: try simplifying it as shown: `!(*b)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_comparison = note: `#[warn(clippy::bool_comparison)]` on by default
}

#[derive(Serialize)]
pub(crate) struct MessageWrapper<'a> {
#[serde(skip_serializing_if = "is_validate_only_default")]
validate_only: bool,
message: &'a Message,
}

impl MessageWrapper<'_> {
pub fn new(message: &Message) -> MessageWrapper {
MessageWrapper { message }
pub fn new(message: &Message, dry_run: bool) -> MessageWrapper {
MessageWrapper {
validate_only: dry_run,
message,
}
}
}

0 comments on commit 1f4d70c

Please sign in to comment.