-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
async
feature and rework Dispatcher
- Loading branch information
Showing
12 changed files
with
201 additions
and
186 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2022 Yuki Kishimoto | ||
// Distributed under the MIT software license | ||
|
||
use reqwest::{Client, Response}; | ||
use url::Url; | ||
|
||
use crate::{NtfyError, Payload}; | ||
|
||
/// Async dispatcher | ||
#[derive(Debug, Clone)] | ||
pub struct Async { | ||
client: Client, | ||
} | ||
|
||
impl Async { | ||
#[inline] | ||
pub(crate) fn new(client: Client) -> Self { | ||
Self { client } | ||
} | ||
|
||
/// Send payload to ntfy server | ||
pub(crate) async fn send(&self, url: &Url, payload: &Payload) -> Result<(), NtfyError> { | ||
// Build request | ||
let mut builder = self.client.post(url.as_str()); | ||
|
||
// If markdown, set headers | ||
if payload.markdown { | ||
builder = builder.header("Markdown", "yes"); | ||
} | ||
|
||
// Add payload | ||
builder = builder.json(payload); | ||
|
||
// Send request | ||
let res: Response = builder.send().await?; | ||
let res: Response = res.error_for_status()?; | ||
|
||
// Get full response text | ||
let text: String = res.text().await?; | ||
|
||
if text.is_empty() { | ||
return Err(NtfyError::EmptyResponse); | ||
} | ||
|
||
// TODO: check the text? | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) 2022 Yuki Kishimoto | ||
// Distributed under the MIT software license | ||
|
||
use ureq::{Agent, Response}; | ||
use url::Url; | ||
|
||
use crate::{NtfyError, Payload}; | ||
|
||
/// Blocking dispatcher | ||
#[derive(Debug, Clone)] | ||
pub struct Blocking { | ||
client: Agent, | ||
} | ||
|
||
impl Blocking { | ||
#[inline] | ||
pub(crate) fn new(client: Agent) -> Self { | ||
Self { client } | ||
} | ||
|
||
pub(crate) fn send(&self, url: &Url, payload: &Payload) -> Result<(), NtfyError> { | ||
// Build request | ||
let mut builder = self.client.post(url.as_str()); | ||
|
||
// If markdown, set headers | ||
if payload.markdown { | ||
builder = builder.set("Markdown", "yes"); | ||
} | ||
|
||
// Send request | ||
let res: Response = builder.send_json(payload)?; | ||
|
||
// Get full response text | ||
let text: String = res.into_string()?; | ||
|
||
if text.is_empty() { | ||
return Err(NtfyError::EmptyResponse); | ||
} | ||
|
||
// TODO: check the text? | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.