diff --git a/docs/CreateNotificationSuccessResponse.md b/docs/CreateNotificationSuccessResponse.md index 9aa73f9..adaa2ca 100644 --- a/docs/CreateNotificationSuccessResponse.md +++ b/docs/CreateNotificationSuccessResponse.md @@ -4,7 +4,7 @@ Name | Type | Description | Notes ------------ | ------------- | ------------- | ------------- -**id** | Option<**String**> | Notification identifier when the request created a notification. An empty string means no notification was created; read `errors` for details (HTTP may still be 200). | [optional] +**id** | Option<**String**> | Notification identifier when the request created a notification. An empty string means no notification was created; read `errors` for details (HTTP may still be 200). All OneSignal server SDKs expose message-sent / message-not-sent narrowing helpers (named idiomatically per language — e.g. `isMessageSent`, `is_message_sent`, `message_sent?`); prefer them over comparing `id` directly. | [optional] **external_id** | Option<**String**> | Optional correlation / idempotency-related value from the API response. This is not the end-user External ID used for targeting recipients (that lives under `include_aliases.external_id`). | [optional] **errors** | Option<[**serde_json::Value**](.md)> | Polymorphic field: may be an array of human-readable strings and/or an object (for example with `invalid_aliases`, `invalid_external_user_ids`, or `invalid_player_ids`) depending on the API response; HTTP may still be 200 with partial success. Typed SDKs model this loosely so both shapes deserialize. | [optional] diff --git a/src/helpers.rs b/src/helpers.rs index 303c7bb..4857610 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -170,6 +170,53 @@ async fn send_once( } } +/// The branch of a POST /notifications 200 response where a notification was +/// created (`id` is a non-empty string). Shares the +/// [`models::CreateNotificationSuccessResponse`] shape; see +/// [`models::CreateNotificationSuccessResponse::as_sent`]. +pub type MessageSent = models::CreateNotificationSuccessResponse; + +/// The branch of a POST /notifications 200 response where NO notification was +/// created (`id` is absent or empty); `errors` carries the reason. Shares the +/// [`models::CreateNotificationSuccessResponse`] shape; see +/// [`models::CreateNotificationSuccessResponse::as_not_sent`]. +pub type MessageNotSent = models::CreateNotificationSuccessResponse; + +impl models::CreateNotificationSuccessResponse { + /// Whether this is the [`MessageSent`] branch — a notification was created + /// (`id` is present and non-empty). Prefer this over inspecting `id` + /// directly. + pub fn is_message_sent(&self) -> bool { + self.id.as_deref().map_or(false, |id| !id.is_empty()) + } + + /// Whether this is the [`MessageNotSent`] branch — no notification was + /// created (`id` absent or empty); inspect `errors` for why. + pub fn is_message_not_sent(&self) -> bool { + !self.is_message_sent() + } + + /// Returns `Some(self)` viewed as a [`MessageSent`] when a notification was + /// created, otherwise `None`. + pub fn as_sent(&self) -> Option<&MessageSent> { + if self.is_message_sent() { + Some(self) + } else { + None + } + } + + /// Returns `Some(self)` viewed as a [`MessageNotSent`] when no notification + /// was created, otherwise `None`. + pub fn as_not_sent(&self) -> Option<&MessageNotSent> { + if self.is_message_not_sent() { + Some(self) + } else { + None + } + } +} + fn header_value(resp: &reqwest::Response, name: &str) -> Option { resp.headers() .get(name) diff --git a/src/models/create_notification_success_response.rs b/src/models/create_notification_success_response.rs index 5873529..e6f41d9 100644 --- a/src/models/create_notification_success_response.rs +++ b/src/models/create_notification_success_response.rs @@ -13,7 +13,7 @@ #[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)] pub struct CreateNotificationSuccessResponse { - /// Notification identifier when the request created a notification. An empty string means no notification was created; read `errors` for details (HTTP may still be 200). + /// Notification identifier when the request created a notification. An empty string means no notification was created; read `errors` for details (HTTP may still be 200). All OneSignal server SDKs expose message-sent / message-not-sent narrowing helpers (named idiomatically per language — e.g. `isMessageSent`, `is_message_sent`, `message_sent?`); prefer them over comparing `id` directly. #[serde(rename = "id", skip_serializing_if = "Option::is_none")] pub id: Option, /// Optional correlation / idempotency-related value from the API response. This is not the end-user External ID used for targeting recipients (that lives under `include_aliases.external_id`).