From 712f1759e5e9ca4790eed2386fa357970bd3d2cc Mon Sep 17 00:00:00 2001 From: 50U10FCA7 Date: Wed, 27 Nov 2024 05:53:42 -0500 Subject: [PATCH 1/4] Support custom endpoints [skip ci] --- src/client.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index 9da3c8d..124c853 100644 --- a/src/client.rs +++ b/src/client.rs @@ -16,6 +16,7 @@ use hyper_rustls::{ConfigBuilderExt, HttpsConnector, HttpsConnectorBuilder}; use hyper_util::client::legacy::connect::HttpConnector; use hyper_util::client::legacy::Client as HttpClient; use hyper_util::rt::TokioExecutor; +use std::borrow::Cow; use std::convert::Infallible; use std::io::Read; use std::time::Duration; @@ -28,6 +29,8 @@ type HyperConnector = HttpsConnector; /// The APNs service endpoint to connect. #[derive(Debug, Clone)] pub enum Endpoint { + /// Custom endpoint. + Custom(Cow<'static, str>), /// The production environment (api.push.apple.com) Production, /// The development/test environment (api.development.push.apple.com) @@ -37,11 +40,12 @@ pub enum Endpoint { impl fmt::Display for Endpoint { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let host = match self { + Endpoint::Custom(host) => host.as_ref(), Endpoint::Production => "api.push.apple.com", Endpoint::Sandbox => "api.development.push.apple.com", }; - write!(f, "{}", host) + write!(f, "{host}") } } From 246fdfde903e3d7883caa92949c9d8f1aa71230b Mon Sep 17 00:00:00 2001 From: 50U10FCA7 Date: Wed, 27 Nov 2024 13:06:13 -0500 Subject: [PATCH 2/4] Refactor endpoint to be URI [run ci] --- src/client.rs | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/client.rs b/src/client.rs index 124c853..32707a1 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; @@ -16,7 +17,6 @@ use hyper_rustls::{ConfigBuilderExt, HttpsConnector, HttpsConnectorBuilder}; use hyper_util::client::legacy::connect::HttpConnector; use hyper_util::client::legacy::Client as HttpClient; use hyper_util::rt::TokioExecutor; -use std::borrow::Cow; use std::convert::Infallible; use std::io::Read; use std::time::Duration; @@ -26,26 +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 `[endpoint]/[device_token]` format. #[derive(Debug, Clone)] pub enum Endpoint { - /// Custom endpoint. - Custom(Cow<'static, str>), - /// The production environment (api.push.apple.com) + /// Custom endpoint [`Uri`]. + /// + /// [`Uri::path`] should not contains trailing `/`. + Custom(Uri), + + /// The production environment (`https://api.push.apple.com/3/device`). Production, - /// The development/test environment (api.development.push.apple.com) + + /// The development/test environment + /// (`https://api.development.push.apple.com/3/device`). Sandbox, } impl fmt::Display for Endpoint { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let host = match self { - Endpoint::Custom(host) => host.as_ref(), - 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/3/device"), + Endpoint::Sandbox => write!(f, "https://api.development.push.apple.com/3/device"), + } } } @@ -261,11 +267,7 @@ impl Client { } fn build_request(&self, payload: T) -> Result>, Error> { - let path = format!( - "https://{}/3/device/{}", - self.options.endpoint, - payload.get_device_token() - ); + let path = format!("{}/{}", self.options.endpoint, payload.get_device_token()); let mut builder = hyper::Request::builder() .uri(&path) From a665e77949273d11166b4ee9e3b506571d85f8e4 Mon Sep 17 00:00:00 2001 From: tyranron Date: Thu, 28 Nov 2024 15:11:10 +0100 Subject: [PATCH 3/4] Corrections --- src/client.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/client.rs b/src/client.rs index 32707a1..854cbaa 100644 --- a/src/client.rs +++ b/src/client.rs @@ -29,19 +29,19 @@ type HyperConnector = HttpsConnector; /// The APNs service endpoint to send requests to. /// /// Being appended with device token the notification -/// is sent to in `[endpoint]/[device_token]` format. +/// is sent to in a `[endpoint]/[device_token]` format. #[derive(Debug, Clone)] pub enum Endpoint { /// Custom endpoint [`Uri`]. /// - /// [`Uri::path`] should not contains trailing `/`. + /// [`Uri::path`] should not contain trailing `/`. Custom(Uri), - /// The production environment (`https://api.push.apple.com/3/device`). + /// The production environment (`https://api.push.apple.com`). Production, /// The development/test environment - /// (`https://api.development.push.apple.com/3/device`). + /// (`https://api.development.push.apple.com`). Sandbox, } @@ -49,8 +49,8 @@ impl fmt::Display for Endpoint { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Endpoint::Custom(uri) => write!(f, "{uri}"), - Endpoint::Production => write!(f, "https://api.push.apple.com/3/device"), - Endpoint::Sandbox => write!(f, "https://api.development.push.apple.com/3/device"), + Endpoint::Production => write!(f, "https://api.push.apple.com"), + Endpoint::Sandbox => write!(f, "https://api.development.push.apple.com"), } } } @@ -267,7 +267,7 @@ impl Client { } fn build_request(&self, payload: T) -> Result>, Error> { - let path = format!("{}/{}", self.options.endpoint, payload.get_device_token()); + let path = format!("{}/3/device/{}", self.options.endpoint, payload.get_device_token()); let mut builder = hyper::Request::builder() .uri(&path) From 289ac68cdb4ae2dc029655367e05de70a1d85dc6 Mon Sep 17 00:00:00 2001 From: 50U10FCA7 Date: Thu, 9 Jan 2025 07:54:28 -0500 Subject: [PATCH 4/4] Fix [run ci] --- src/client.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/client.rs b/src/client.rs index 854cbaa..2fef515 100644 --- a/src/client.rs +++ b/src/client.rs @@ -34,7 +34,7 @@ type HyperConnector = HttpsConnector; pub enum Endpoint { /// Custom endpoint [`Uri`]. /// - /// [`Uri::path`] should not contain trailing `/`. + /// [`Uri::path`] should contain trailing `/`. Custom(Uri), /// The production environment (`https://api.push.apple.com`). @@ -49,8 +49,8 @@ impl fmt::Display for Endpoint { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 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"), + Endpoint::Production => write!(f, "https://api.push.apple.com/"), + Endpoint::Sandbox => write!(f, "https://api.development.push.apple.com/"), } } } @@ -267,10 +267,10 @@ impl Client { } fn build_request(&self, payload: T) -> Result>, Error> { - let path = format!("{}/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");