1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
//! Subscribe to or unsubscribe from events for a specified topic.
//! [`subscribe-tounsubscribe-from-events`](https://dev.twitch.tv/docs/api/webhooks-reference#subscribe-tounsubscribe-from-events)
//!
//! # Accessing the endpoint
//!
//! ## Request: [WebhookHubRequest]
//!
//! To use this endpoint, construct a [`WebhookHubRequest`] with the [`WebhookHubRequest::builder()`] method.
//!
//! ```rust, no_run
//! use twitch_api2::helix::webhooks::hub;
//! let request = hub::WebhookHubRequest::<twitch_api2::helix::webhooks::topics::users::UserFollowsTopic>::builder()
//!     .build();
//! ```
//!
//! ## Body: [WebhookHubBody]
//!
//! We also need to provide a body to the request containing what we want to change.
//!
//! ```
//! # use twitch_api2::helix::webhooks::hub;
//! let body = hub::WebhookHubBody::builder()
//!     .callback("https://example.com/this-is-a-callback")
//!     .lease_seconds(864000)
//!     .mode(hub::WebhookSubscriptionMode::Subscribe)
//!     .secret("12233213890390".to_string())
//!     .topic(twitch_api2::helix::webhooks::topics::users::UserFollowsTopic::builder().from_id(1336.to_string()).build())
//!     .build();
//! ```
//!
//! ## Response: [WebhookHub]
//!
//!
//! Send the request to receive the response with [`HelixClient::req_post()`](helix::HelixClient::req_post).
//!
//!
//! ```rust, no_run
//! use twitch_api2::helix::{self, webhooks::hub};
//! # use twitch_api2::client;
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
//! # let client: helix::HelixClient<'static, client::DummyHttpClient> = helix::HelixClient::default();
//! # let token = twitch_oauth2::AccessToken::new("validtoken".to_string());
//! # let token = twitch_oauth2::UserToken::from_existing(twitch_oauth2::dummy_http_client, token, None, None).await?;
//! let request = hub::WebhookHubRequest::builder()
//!     .build();
//! let body = hub::WebhookHubBody::builder()
//!     .callback("https://example.com/this-is-a-callback")
//!     .lease_seconds(864000)
//!     .mode(hub::WebhookSubscriptionMode::Subscribe)
//!     .secret("12233213890390".to_string())
//!     .topic(twitch_api2::helix::webhooks::topics::users::UserFollowsTopic::builder().from_id(1336.to_string()).build())
//!     .build();
//! let response: hub::WebhookHub = client.req_post(request, body, &token).await?.data;
//! # Ok(())
//! # }
//! ```
//!
//! You can also get the [`http::Request`] with [`request.create_request(&token, &client_id)`](helix::RequestPost::create_request)
//! and parse the [`http::Response`] with [`request.parse_response(&request.get_uri()?)`](helix::RequestPost::parse_response())

use crate::helix;
use std::convert::TryInto;

use serde::{Deserialize, Serialize};

use super::Topic;
/// Query Parameters for [Subscribe to/Unsubscribe From Events](super::hub)
///
/// [`subscribe-tounsubscribe-from-events`](https://dev.twitch.tv/docs/api/webhooks-reference#subscribe-tounsubscribe-from-events)
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug, Default)]
#[non_exhaustive]
pub struct WebhookHubRequest<T: Topic> {
    #[builder(setter(skip), default)]
    #[serde(skip)]
    _phantom: std::marker::PhantomData<T>,
}

/// Body Parameters for [Subscribe to/Unsubscribe From Events](super::hub)
///
/// [`subscribe-tounsubscribe-from-events`](https://dev.twitch.tv/docs/api/webhooks-reference#subscribe-tounsubscribe-from-events)
///
/// # Notes
///
/// This body is quite different from the official body. If you want the true representation in text, see [`helix::RequestPost::body`] on [`WebhookHubRequest<T: Topic>`](WebhookHubRequest)
#[derive(PartialEq, typed_builder::TypedBuilder, Deserialize, Serialize, Clone, Debug)]
#[non_exhaustive]
pub struct WebhookHubBody<T: Topic> {
    /// URL where notifications will be delivered.
    #[builder(setter(into))]
    pub callback: String,
    /// Type of request. Valid values: subscribe, unsubscribe
    pub mode: WebhookSubscriptionMode,
    /// URL for the topic to subscribe to or unsubscribe from. topic maps to a new Twitch API endpoint.
    #[serde(bound = "T: Topic")]
    pub topic: T,
    /// Number of seconds until the subscription expires. Default: 0. Maximum: 864000.
    pub lease_seconds: u32,
    /// Secret used to sign notification payloads
    #[builder(setter(into))]
    pub secret: Option<String>,
}

/// Subscription mode
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[serde(rename_all = "lowercase")]
pub enum WebhookSubscriptionMode {
    /// Subscribe
    Subscribe,
    /// Unsubscribe
    Unsubscribe,
}

/// Return Values for [Subscribe to/Unsubscribe From Events](super::hub)
///
/// [`subscribe-tounsubscribe-from-events`](https://dev.twitch.tv/docs/api/webhooks-reference#subscribe-tounsubscribe-from-events)
#[derive(PartialEq, Deserialize, Debug, Clone)]
#[cfg_attr(not(feature = "allow_unknown_fields"), serde(deny_unknown_fields))]
#[non_exhaustive]
pub enum WebhookHub {
    /// 202 - Success
    Success,
}
impl std::convert::TryFrom<http::StatusCode> for WebhookHub {
    type Error = std::borrow::Cow<'static, str>;

    fn try_from(
        s: http::StatusCode,
    ) -> Result<Self, <Self as std::convert::TryFrom<http::StatusCode>>::Error> {
        match s {
            http::StatusCode::ACCEPTED | http::StatusCode::OK => Ok(WebhookHub::Success),
            other => Err(other.canonical_reason().unwrap_or("").into()),
        }
    }
}
impl<T: Topic> helix::Request for WebhookHubRequest<T> {
    type Response = WebhookHub;

    const PATH: &'static str = "webhooks/hub";
    #[cfg(feature = "twitch_oauth2")]
    const SCOPE: &'static [twitch_oauth2::Scope] = &[];
}

impl<T: Topic> helix::RequestPost for WebhookHubRequest<T> {
    type Body = WebhookHubBody<T>;

    fn body(&self, body: &Self::Body) -> Result<String, helix::BodyError> {
        #[derive(PartialEq, Serialize)]
        struct IWebhookHubBody<'a> {
            #[serde(rename = "hub.callback")]
            callback: &'a str,
            #[serde(rename = "hub.mode")]
            mode: &'a WebhookSubscriptionMode,
            #[serde(rename = "hub.topic")]
            topic: String,
            #[serde(rename = "hub.lease_seconds")]
            lease_seconds: u32,
            #[serde(rename = "hub.secret")]
            secret: Option<&'a str>,
        }

        let b = IWebhookHubBody {
            callback: &body.callback,
            mode: &body.mode,
            topic: body.topic.get_uri()?.to_string(),
            lease_seconds: body.lease_seconds,
            secret: body.secret.as_deref(),
        };
        serde_json::to_string(&b).map_err(Into::into)
    }

    fn parse_response(
        request: Option<Self>,
        uri: &http::Uri,
        response: http::Response<Vec<u8>>,
    ) -> Result<
        helix::Response<Self, <Self as helix::Request>::Response>,
        helix::HelixRequestPostError,
    >
    where
        Self: Sized,
    {
        let text = std::str::from_utf8(&response.body()).map_err(|e| {
            helix::HelixRequestPostError::Utf8Error(response.body().clone(), e, uri.clone())
        })?;
        if let Ok(helix::HelixRequestError {
            error,
            status,
            message,
        }) = serde_json::from_str::<helix::HelixRequestError>(&text)
        {
            return Err(helix::HelixRequestPostError::Error {
                error,
                status: status.try_into().unwrap_or(http::StatusCode::BAD_REQUEST),
                message,
                uri: uri.clone(),
                body: response.body().clone(),
            });
        }

        let response = response.status().try_into().map_err(|_| {
            // This path should never be taken, but just to be sure we do this
            helix::HelixRequestPostError::Error {
                status: response.status(),
                uri: uri.clone(),
                body: response.body().clone(),
                message: String::new(), // FIXME: None, but this branch should really never be hit
                error: String::new(),
            }
        })?;
        Ok(helix::Response {
            data: response, // FIXME: This should be a bit better...
            pagination: <_>::default(),
            request,
        })
    }
}

#[test]
fn test_request() {
    use helix::*;
    let req =
        WebhookHubRequest::<webhooks::topics::users::user_follows::UserFollowsTopic>::builder()
            .build();

    // From twitch docs
    let data = br#""#.to_vec();

    let http_response = http::Response::builder().status(202).body(data).unwrap();

    let uri = req.get_uri().unwrap();
    assert_eq!(uri.to_string(), "https://api.twitch.tv/helix/webhooks/hub?");

    dbg!(WebhookHubRequest::parse_response(Some(req), &uri, http_response).unwrap());
}