Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom endpoints #93

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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