Skip to content

Commit

Permalink
Add async feature and rework Dispatcher
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Jan 6, 2025
1 parent 947835a commit d325d4f
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 187 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ jobs:
matrix:
build-args:
[
--no-default-features,
--no-default-features --features async,
--no-default-features --features blocking,
]
steps:
- name: Checkout
Expand Down
65 changes: 0 additions & 65 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ readme = "README.md"
keywords = ["ntfy", "notifications", "sdk"]

[features]
default = ["dep:reqwest"]
default = ["async"]
async = ["dep:reqwest"]
blocking = ["dep:ureq"]

[dependencies]
Expand All @@ -23,14 +24,15 @@ ureq = { version = "2.12", features = ["json", "socks-proxy"], optional = true }
url = { version = "2", features = ["serde"] }

[dev-dependencies]
tokio = { version = "1", features = ["full"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

[[example]]
name = "blocking"
required-features = ["blocking"]

[[example]]
name = "client"
required-features = ["async"]

[profile.release]
lto = true
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use ntfy::prelude::*;
#[tokio::main]
async fn main() -> Result<(), NtfyError> {
let dispatcher = Dispatcher::builder("https://ntfy.sh")
let dispatcher = DispatcherBuilder::new("https://ntfy.sh")
.credentials(Auth::credentials("username", "password")) // Add optional credentials
.proxy("socks5h://127.0.0.1:9050") // Add optional proxy
.build()?; // Build dispatcher
.build_async()?; // Build dispatcher
let action = Action::new(
ActionType::Http,
Expand Down
4 changes: 2 additions & 2 deletions examples/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
use ntfy::prelude::*;

fn main() -> Result<(), NtfyError> {
let dispatcher = Dispatcher::builder("https://ntfy.sh")
let dispatcher = DispatcherBuilder::new("https://ntfy.sh")
.credentials(Auth::credentials("username", "password")) // Add optional credentials
.proxy("socks5://127.0.0.1:9050") // Add optional proxy
.build()?; // Build dispatcher
.build_blocking()?; // Build dispatcher

let action = Action::new(
ActionType::Http,
Expand Down
4 changes: 2 additions & 2 deletions examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use ntfy::prelude::*;

#[tokio::main]
async fn main() -> Result<(), NtfyError> {
let dispatcher = Dispatcher::builder("https://ntfy.sh")
let dispatcher = DispatcherBuilder::new("https://ntfy.sh")
.credentials(Auth::credentials("username", "password")) // Add optional credentials
.proxy("socks5://127.0.0.1:9050") // Add optional proxy
.build()?; // Build dispatcher
.build_async()?; // Build dispatcher

let action = Action::new(
ActionType::Http,
Expand Down
9 changes: 6 additions & 3 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

check:
cargo fmt --all -- --config format_code_in_doc_comments=true
cargo check
cargo test
cargo clippy -- -D warnings
cargo check --no-default-features --features async
cargo check --no-default-features --features blocking
cargo test --no-default-features --features async
cargo test --no-default-features --features blocking
cargo clippy --no-default-features --features async -- -D warnings
cargo clippy --no-default-features --features blocking -- -D warnings
49 changes: 49 additions & 0 deletions src/dispatcher/async.rs
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(())
}
}
44 changes: 44 additions & 0 deletions src/dispatcher/blocking.rs
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(())
}
}
Loading

0 comments on commit d325d4f

Please sign in to comment.