Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,23 +26,32 @@ const DEFAULT_REQUEST_TIMEOUT_SECS: u64 = 20;

type HyperConnector = HttpsConnector<HttpConnector>;

/// 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/"),
}
}
}

Expand Down Expand Up @@ -257,14 +267,10 @@ impl Client {
}

fn build_request<T: PayloadLike>(&self, payload: T) -> Result<hyper::Request<BoxBody<Bytes, Infallible>>, 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");

Expand Down