diff --git a/src/client.rs b/src/client.rs index 9da3c8d..2fef515 100644 --- a/src/client.rs +++ b/src/client.rs @@ -8,6 +8,7 @@ use tokio::time::timeout; use crate::request::payload::PayloadLike; use crate::response::Response; use http::header::{AUTHORIZATION, CONTENT_LENGTH, CONTENT_TYPE}; +use http::Uri; use http_body_util::combinators::BoxBody; use http_body_util::{BodyExt, Full}; use hyper::body::Bytes; @@ -25,23 +26,32 @@ const DEFAULT_REQUEST_TIMEOUT_SECS: u64 = 20; type HyperConnector = HttpsConnector; -/// The APNs service endpoint to connect. +/// The APNs service endpoint to send requests to. +/// +/// Being appended with device token the notification +/// is sent to in a `[endpoint]/[device_token]` format. #[derive(Debug, Clone)] pub enum Endpoint { - /// The production environment (api.push.apple.com) + /// Custom endpoint [`Uri`]. + /// + /// [`Uri::path`] should contain trailing `/`. + Custom(Uri), + + /// The production environment (`https://api.push.apple.com`). Production, - /// The development/test environment (api.development.push.apple.com) + + /// The development/test environment + /// (`https://api.development.push.apple.com`). Sandbox, } impl fmt::Display for Endpoint { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let host = match self { - Endpoint::Production => "api.push.apple.com", - Endpoint::Sandbox => "api.development.push.apple.com", - }; - - write!(f, "{}", host) + match self { + Endpoint::Custom(uri) => write!(f, "{uri}"), + Endpoint::Production => write!(f, "https://api.push.apple.com/"), + Endpoint::Sandbox => write!(f, "https://api.development.push.apple.com/"), + } } } @@ -257,14 +267,10 @@ impl Client { } fn build_request(&self, payload: T) -> Result>, Error> { - let path = format!( - "https://{}/3/device/{}", - self.options.endpoint, - payload.get_device_token() - ); + let uri = format!("{}3/device/{}", self.options.endpoint, payload.get_device_token()); let mut builder = hyper::Request::builder() - .uri(&path) + .uri(&uri) .method("POST") .header(CONTENT_TYPE, "application/json");