Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dry run #13

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}

#[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,
}
}
}
Loading