web_push/lib.rs
1//! # Web Push
2//!
3//! A library for creating and sending push notifications to a web browser. For
4//! content payload encryption it uses [RFC8188](https://datatracker.ietf.org/doc/html/rfc8188).
5//! The client is asynchronous and can run on any executor. An optional [`hyper`](https://crates.io/crates/hyper) based client is
6//! available with the feature `hyper-client`.
7//!
8//! # Example
9//!
10//! ```no_run
11//! # use web_push::*;
12//! # use std::fs::File;
13//! # #[tokio::main]
14//! # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
15//! let endpoint = "https://updates.push.services.mozilla.com/wpush/v1/...";
16//! let p256dh = "key_from_browser_as_base64";
17//! let auth = "auth_from_browser_as_base64";
18//!
19//! // You would likely get this by deserializing a browser `pushSubscription` object.
20//! let subscription_info = SubscriptionInfo::new(
21//! endpoint,
22//! p256dh,
23//! auth
24//! );
25//!
26//! // Read signing material for payload.
27//! let file = File::open("private.pem").unwrap();
28//! let mut sig_builder = VapidSignatureBuilder::from_pem(file, &subscription_info)?.build()?;
29//!
30//! // Now add payload and encrypt.
31//! let mut builder = WebPushMessageBuilder::new(&subscription_info);
32//! let content = "Encrypted payload to be sent in the notification".as_bytes();
33//! builder.set_payload(ContentEncoding::Aes128Gcm, content);
34//! builder.set_vapid_signature(sig_builder);
35//!
36//! # #[cfg(feature = "isahc-client")]
37//! let client = IsahcWebPushClient::new()?;
38//!
39//! // Finally, send the notification!
40//! # #[cfg(feature = "isahc-client")]
41//! client.send(builder.build()?).await?;
42//! # Ok(())
43//! # }
44//! ```
45
46#[macro_use]
47extern crate log;
48#[macro_use]
49extern crate serde_derive;
50
51#[cfg(feature = "hyper-client")]
52pub use crate::clients::hyper_client::HyperWebPushClient;
53#[cfg(feature = "isahc-client")]
54pub use crate::clients::isahc_client::IsahcWebPushClient;
55pub use crate::{
56 clients::{request_builder, WebPushClient},
57 error::WebPushError,
58 http_ece::ContentEncoding,
59 message::{SubscriptionInfo, SubscriptionKeys, Urgency, WebPushMessage, WebPushMessageBuilder, WebPushPayload},
60 vapid::{builder::PartialVapidSignatureBuilder, VapidSignature, VapidSignatureBuilder},
61};
62
63mod clients;
64mod error;
65mod http_ece;
66mod message;
67mod vapid;