Skip to content

Commit

Permalink
Drop blocking support
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Sep 14, 2024
1 parent cb2675f commit 4cc4c61
Show file tree
Hide file tree
Showing 9 changed files with 4 additions and 131 deletions.
1 change: 0 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobs:
build-args:
[
--no-default-features,
--features blocking,
]
steps:
- name: Checkout
Expand Down
18 changes: 0 additions & 18 deletions Cargo.lock

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

10 changes: 1 addition & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ repository = "https://github.com/shadowylab/ntfy.git"
readme = "README.md"
keywords = ["ntfy", "notifications", "sdk"]

[features]
default = []
blocking = ["reqwest/blocking"]

[dependencies]
base64 = "0.22"
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls-webpki-roots", "socks"] }
Expand All @@ -28,11 +24,7 @@ tokio = { version = "1", features = ["full"] }
[[example]]
name = "client"

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

[profile.release]
lto = true
codegen-units = 1
panic = "abort"
panic = "abort"
10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,14 @@ async fn main() -> Result<(), NtfyError> {
.delay(1639194738) // Add optional delay
.markdown(true); // Use markdown
dispatcher.send(&payload).await.unwrap();
dispatcher.send(&payload).await?;
Ok(())
}
```

More examples can be found in the [examples](examples) directory.

## Crate Feature Flags

The following crate feature flags are available:

| Feature | Default | Description |
|------------|:-------:|-------------------------------------------------------------------|
| `blocking` | No | Needed if you want to use this library in not async/await context |

## License

This project is distributed under the MIT software license - see the [LICENSE](LICENSE) file for details
Expand Down
32 changes: 0 additions & 32 deletions examples/blocking.rs

This file was deleted.

7 changes: 0 additions & 7 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
#!/usr/bin/env just --justfile

precommit:
cargo fmt --all -- --config format_code_in_doc_comments=true
cargo test
cargo clippy
cargo clippy --features blocking

check:
cargo fmt --all -- --config format_code_in_doc_comments=true
cargo check
cargo test
cargo clippy -- -D warnings
cargo clippy --features blocking -- -D warnings
3 changes: 0 additions & 3 deletions src/dispatcher/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@

use std::str::FromStr;

#[cfg(feature = "blocking")]
use reqwest::blocking::ClientBuilder;
use reqwest::header::{HeaderMap, HeaderValue};
#[cfg(not(feature = "blocking"))]
use reqwest::ClientBuilder;
use reqwest::Proxy;
use url::Url;
Expand Down
18 changes: 2 additions & 16 deletions src/dispatcher/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Copyright (c) 2022 Yuki Kishimoto
// Distributed under the MIT software license

#[cfg(feature = "blocking")]
use reqwest::blocking::Client as ReqwestClient;
#[cfg(not(feature = "blocking"))]
use reqwest::Client as ReqwestClient;
use reqwest::Client;
use url::Url;

pub mod auth;
Expand All @@ -19,7 +16,7 @@ use crate::payload::Payload;
#[derive(Debug, Clone)]
pub struct Dispatcher {
url: Url,
client: ReqwestClient,
client: Client,
}

impl Dispatcher {
Expand Down Expand Up @@ -50,22 +47,11 @@ impl Dispatcher {
}

/// Send payload to ntfy server
#[cfg(not(feature = "blocking"))]
pub async fn send(&self, payload: &Payload) -> Result<(), NtfyError> {
let mut builder = self.client.post(self.url.as_str());
if payload.markdown {
builder = builder.header("Markdown", "yes");
}
request(builder.json(payload)).await
}

/// Send payload to ntfy server
#[cfg(feature = "blocking")]
pub fn send(&self, payload: &Payload) -> Result<(), NtfyError> {
let mut builder = self.client.post(self.url.as_str());
if payload.markdown {
builder = builder.header("Markdown", "yes");
}
request(builder.json(payload))
}
}
36 changes: 0 additions & 36 deletions src/net.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
// Copyright (c) 2022 Yuki Kishimoto
// Distributed under the MIT software license

#[cfg(feature = "blocking")]
use reqwest::blocking::RequestBuilder;
#[cfg(not(feature = "blocking"))]
use reqwest::RequestBuilder;
use reqwest::StatusCode;

use crate::error::NtfyError;

#[cfg(not(feature = "blocking"))]
pub(crate) async fn request(req: RequestBuilder) -> Result<(), NtfyError> {
let res = req.send().await?;

Expand Down Expand Up @@ -40,35 +36,3 @@ pub(crate) async fn request(req: RequestBuilder) -> Result<(), NtfyError> {
_ => Err(NtfyError::UnhandledServerError),
}
}

#[cfg(feature = "blocking")]
pub(crate) fn request(req: RequestBuilder) -> Result<(), NtfyError> {
let res = req.send()?;

match StatusCode::as_u16(&res.status()) {
0_u16..=399_u16 => {
let res = res.text()?;

if res.is_empty() {
return Err(NtfyError::EmptyResponse);
}

Ok(())
}
400 => Err(NtfyError::BadRequest),
401 => Err(NtfyError::Unauthorized),
402 => Err(NtfyError::UnhandledClientError),
403 => Err(NtfyError::Forbidden),
404 => Err(NtfyError::NotFound),
405 => Err(NtfyError::MethodNotAllowed),
406_u16..=428_u16 => Err(NtfyError::UnhandledClientError),
429 => Err(NtfyError::TooManyRequests),
430_u16..=499_u16 => Err(NtfyError::UnhandledClientError),
500 => Err(NtfyError::InternalServerError),
501 => Err(NtfyError::NotImplemented),
502 => Err(NtfyError::BadGateway),
503 => Err(NtfyError::ServiceUnavailable),
504 => Err(NtfyError::GatewayTimeout),
_ => Err(NtfyError::UnhandledServerError),
}
}

0 comments on commit 4cc4c61

Please sign in to comment.