-
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
* Rename `NtfyError` to `Error` Ref #7 Signed-off-by: Yuki Kishimoto <[email protected]>
- Loading branch information
Showing
13 changed files
with
258 additions
and
235 deletions.
There are no files selected for viewing
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
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,68 @@ | ||
// Copyright (c) 2022 Yuki Kishimoto | ||
// Distributed under the MIT software license | ||
|
||
use reqwest::header::{HeaderMap, HeaderValue}; | ||
use reqwest::{Client, ClientBuilder, Proxy, Response}; | ||
use url::Url; | ||
|
||
use super::builder::DispatcherBuilder; | ||
use crate::error::Error; | ||
use crate::payload::Payload; | ||
|
||
/// Async dispatcher | ||
#[derive(Debug, Clone)] | ||
pub struct Async { | ||
client: Client, | ||
} | ||
|
||
impl Async { | ||
#[inline] | ||
pub(crate) fn new(builder: DispatcherBuilder) -> Result<Self, Error> { | ||
let mut client = ClientBuilder::new(); | ||
|
||
if let Some(auth) = builder.auth { | ||
let mut headers = HeaderMap::new(); | ||
let mut auth_value = HeaderValue::from_str(&auth.to_header_value())?; | ||
auth_value.set_sensitive(true); | ||
headers.insert("Authorization", auth_value); | ||
client = client.default_headers(headers); | ||
} | ||
|
||
if let Some(proxy) = builder.proxy { | ||
client = client.proxy(Proxy::all(proxy)?); | ||
} | ||
|
||
Ok(Self { | ||
client: client.build()?, | ||
}) | ||
} | ||
|
||
/// Send payload to ntfy server | ||
pub(crate) async fn send(&self, url: &Url, payload: &Payload) -> Result<(), Error> { | ||
// 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(Error::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,66 @@ | ||
// Copyright (c) 2022 Yuki Kishimoto | ||
// Distributed under the MIT software license | ||
|
||
use ureq::{Agent, MiddlewareNext, Request, Response}; | ||
use url::Url; | ||
|
||
use super::builder::DispatcherBuilder; | ||
use crate::error::Error; | ||
use crate::payload::Payload; | ||
|
||
/// Blocking dispatcher | ||
#[derive(Debug, Clone)] | ||
pub struct Blocking { | ||
client: Agent, | ||
} | ||
|
||
impl Blocking { | ||
#[inline] | ||
pub(crate) fn new(builder: DispatcherBuilder) -> Result<Self, Error> { | ||
let mut client = ureq::builder(); | ||
|
||
if let Some(auth) = builder.auth { | ||
let heaver_value = auth.to_header_value(); | ||
|
||
// Set the authorization headers of every request using a middleware function | ||
client = client.middleware( | ||
move |req: Request, next: MiddlewareNext| -> Result<Response, ureq::Error> { | ||
next.handle(req.set("Authorization", &heaver_value)) | ||
}, | ||
); | ||
} | ||
|
||
if let Some(proxy) = builder.proxy { | ||
let proxy = ureq::Proxy::new(proxy)?; | ||
client = client.proxy(proxy); | ||
} | ||
|
||
Ok(Self { | ||
client: client.build(), | ||
}) | ||
} | ||
|
||
pub(crate) fn send(&self, url: &Url, payload: &Payload) -> Result<(), Error> { | ||
// 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(Error::EmptyResponse); | ||
} | ||
|
||
// TODO: check the text? | ||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.