Struct twitch_api::eventsub::Payload
source · [−]#[non_exhaustive]pub struct Payload<E: EventSubscription + Clone> {
pub subscription: EventSubscriptionInformation<E>,
pub message: Message<E>,
}
eventsub
only.Expand description
Notification received
Fields (Non-exhaustive)
This struct is marked as non-exhaustive
Struct { .. }
syntax; cannot be matched against without a wildcard ..
; and struct update syntax will not work.subscription: EventSubscriptionInformation<E>
Subscription information.
message: Message<E>
Event information.
Implementations
sourceimpl<E: EventSubscription> Payload<E>
impl<E: EventSubscription> Payload<E>
sourcepub fn parse(source: &str) -> Result<Payload<E>, PayloadParseError>
pub fn parse(source: &str) -> Result<Payload<E>, PayloadParseError>
Parse string slice as a Payload
, this will assume your string is from an eventsub message with type notification
sourcepub fn parse_notification(source: &str) -> Result<Payload<E>, PayloadParseError>
pub fn parse_notification(source: &str) -> Result<Payload<E>, PayloadParseError>
Parse string slice as a Payload
with a message of Message::Notification
.
Examples found in repository?
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
pub fn parse(source: &str) -> Result<Payload<E>, PayloadParseError> {
Self::parse_notification(source)
}
/// Parse string slice as a [`Payload`] with a message of [`Message::Notification`].
pub fn parse_notification(source: &str) -> Result<Payload<E>, PayloadParseError> {
#[derive(Deserialize)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
struct Notification<E: EventSubscription> {
#[serde(bound = "E: EventSubscription")]
pub subscription: EventSubscriptionInformation<E>,
#[serde(bound = "E: EventSubscription")]
pub event: <E as EventSubscription>::Payload,
}
let Notification {
subscription,
event,
} = parse_json::<Notification<E>>(source, true)?;
Ok(Payload {
subscription,
message: Message::Notification(event),
})
}
/// Parse string slice as a [`Payload`] with a message of [`Message::Revocation`].
pub fn parse_revocation(source: &str) -> Result<Payload<E>, PayloadParseError> {
#[derive(Deserialize)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
struct Notification<E: EventSubscription> {
#[serde(bound = "E: EventSubscription")]
pub subscription: EventSubscriptionInformation<E>,
}
let Notification { subscription } = parse_json::<Notification<E>>(source, true)?;
Ok(Payload {
subscription,
message: Message::Revocation(),
})
}
/// Parse string slice as a [`Payload`] with a message of [`Message::VerificationRequest`].
pub fn parse_verification_request(source: &str) -> Result<Payload<E>, PayloadParseError> {
#[derive(Deserialize)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
struct Notification<E: EventSubscription> {
#[serde(bound = "E: EventSubscription")]
pub subscription: EventSubscriptionInformation<E>,
#[serde(bound = "E: EventSubscription")]
pub challenge: String,
}
let Notification {
subscription,
challenge,
} = parse_json::<Notification<E>>(source, true)?;
Ok(Payload {
subscription,
message: Message::VerificationRequest(VerificationRequest { challenge }),
})
}
/// Parse http post request as a [Payload] with a specific [event](EventSubscription).
///
/// If you don't know what event this payload is, use [`Event::parse_http`] instead.
///
/// If your [`Request<B>`](http::Request) is of another type that doesn't implement `AsRef<[u8]>`, try converting it with [`Request::map`](http::Request::map)
///
/// ```rust
/// use http::Request;
/// use twitch_api::eventsub::{Payload, channel::ChannelFollowV1};
/// # struct Body {} impl Body { fn new() -> Self {Body {}} fn to_bytes(&self) -> &[u8] { &[] } }
/// # fn a() -> Result<(), twitch_api::eventsub::PayloadParseError> {
/// // Example of a request with a body that doesn't implement `AsRef<[u8]>`
/// let original_request: Request<Body> = http::Request::new(Body::new());
/// // Convert to a request with a body of `Vec<u8>`, which does implement `AsRef<[u8]>`
/// let converted_request: Request<Vec<u8>> = original_request.map(|r| r.to_bytes().to_owned());
/// Payload::<ChannelFollowV1>::parse_http(&converted_request)?
/// # ; Ok(())}
/// ```
pub fn parse_http<B>(request: &http::Request<B>) -> Result<Payload<E>, PayloadParseError>
where B: AsRef<[u8]> {
// FIXME: Add some debug assertions for version and type
let source = request.body().as_ref().into();
let ty = request
.headers()
.get("Twitch-Eventsub-Message-Type")
.map(|v| v.as_bytes())
.unwrap_or_else(|| b"notification")
.into();
Self::parse_request(ty, source)
}
/// Parse a string slice as a [`Payload`] with a specific message type. You should not use this, instead, use [`Payload::parse_http`] or [`Payload::parse`].
#[doc(hidden)]
pub fn parse_request<'a>(
ty: Cow<'a, [u8]>,
source: Cow<'a, [u8]>,
) -> Result<Payload<E>, PayloadParseError> {
let source = std::str::from_utf8(&source)?;
match ty.as_ref() {
b"notification" => Payload::parse_notification(source),
b"webhook_callback_verification" => Payload::parse_verification_request(source),
b"revocation" => Payload::parse_revocation(source),
typ => Err(PayloadParseError::UnknownMessageType(
String::from_utf8_lossy(typ).into_owned(),
)),
}
}
sourcepub fn parse_revocation(source: &str) -> Result<Payload<E>, PayloadParseError>
pub fn parse_revocation(source: &str) -> Result<Payload<E>, PayloadParseError>
Parse string slice as a Payload
with a message of Message::Revocation
.
Examples found in repository?
268 269 270 271 272 273 274 275 276 277 278 279 280 281
pub fn parse_request<'a>(
ty: Cow<'a, [u8]>,
source: Cow<'a, [u8]>,
) -> Result<Payload<E>, PayloadParseError> {
let source = std::str::from_utf8(&source)?;
match ty.as_ref() {
b"notification" => Payload::parse_notification(source),
b"webhook_callback_verification" => Payload::parse_verification_request(source),
b"revocation" => Payload::parse_revocation(source),
typ => Err(PayloadParseError::UnknownMessageType(
String::from_utf8_lossy(typ).into_owned(),
)),
}
}
sourcepub fn parse_verification_request(
source: &str
) -> Result<Payload<E>, PayloadParseError>
pub fn parse_verification_request(
source: &str
) -> Result<Payload<E>, PayloadParseError>
Parse string slice as a Payload
with a message of Message::VerificationRequest
.
Examples found in repository?
268 269 270 271 272 273 274 275 276 277 278 279 280 281
pub fn parse_request<'a>(
ty: Cow<'a, [u8]>,
source: Cow<'a, [u8]>,
) -> Result<Payload<E>, PayloadParseError> {
let source = std::str::from_utf8(&source)?;
match ty.as_ref() {
b"notification" => Payload::parse_notification(source),
b"webhook_callback_verification" => Payload::parse_verification_request(source),
b"revocation" => Payload::parse_revocation(source),
typ => Err(PayloadParseError::UnknownMessageType(
String::from_utf8_lossy(typ).into_owned(),
)),
}
}
sourcepub fn parse_http<B>(
request: &Request<B>
) -> Result<Payload<E>, PayloadParseError>where
B: AsRef<[u8]>,
pub fn parse_http<B>(
request: &Request<B>
) -> Result<Payload<E>, PayloadParseError>where
B: AsRef<[u8]>,
Parse http post request as a Payload with a specific event.
If you don’t know what event this payload is, use Event::parse_http
instead.
If your Request<B>
is of another type that doesn’t implement AsRef<[u8]>
, try converting it with Request::map
use http::Request;
use twitch_api::eventsub::{Payload, channel::ChannelFollowV1};
// Example of a request with a body that doesn't implement `AsRef<[u8]>`
let original_request: Request<Body> = http::Request::new(Body::new());
// Convert to a request with a body of `Vec<u8>`, which does implement `AsRef<[u8]>`
let converted_request: Request<Vec<u8>> = original_request.map(|r| r.to_bytes().to_owned());
Payload::<ChannelFollowV1>::parse_http(&converted_request)?
sourceimpl<E: EventSubscription + Clone> Payload<E>
impl<E: EventSubscription + Clone> Payload<E>
sourcepub fn get_event_type(&self) -> EventType
pub fn get_event_type(&self) -> EventType
Convenience method for getting the event type from the payload.
sourcepub fn get_event_version(&self) -> &'static str
pub fn get_event_version(&self) -> &'static str
Convenience method for getting the event version from the payload.