twitch_api/helix/endpoints/clips/
create_clip.rs

1//! Create Clip using Broadcaster ID (one only)
2//! [`create-clip`](https://dev.twitch.tv/docs/api/reference/#create-clip)
3//!
4//! # Accessing the endpoint
5//!
6//! ## Request: [CreateClipRequest]
7//!
8//! To use this endpoint, construct a [`CreateClipRequest`] with the [`CreateClipRequest::broadcaster_id()`] method.
9//!
10//! ```rust
11//! use twitch_api::helix::clips::create_clip;
12//! let request = create_clip::CreateClipRequest::broadcaster_id("1234");
13//! ```
14//!
15//! ## Response: [CreatedClip]
16//!
17//! Send the request to receive the response with [`HelixClient::req_get()`](helix::HelixClient::req_get).
18//!
19//! ```rust, no_run
20//! use twitch_api::helix::{self, clips::create_clip};
21//! # use twitch_api::client;
22//! # #[tokio::main]
23//! # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
24//! # let client: helix::HelixClient<'static, client::DummyHttpClient> = helix::HelixClient::default();
25//! # let token = twitch_oauth2::AccessToken::new("validtoken".to_string());
26//! # let token = twitch_oauth2::UserToken::from_existing(&client, token, None, None).await?;
27//! let request = create_clip::CreateClipRequest::broadcaster_id("1234");
28//! let response: Vec<create_clip::CreatedClip> = client.req_get(request, &token).await?.data;
29//! # Ok(())
30//! # }
31//! ```
32//!
33//! You can also get the [`http::Request`] with [`request.create_request(&token, &client_id)`](helix::RequestGet::create_request)
34//! and parse the [`http::Response`] with [`CreateClipRequest::parse_response(None, &request.get_uri(), response)`](CreateClipRequest::parse_response)
35
36use super::*;
37use helix::RequestGet;
38
39/// Query Parameters for [Create Clip](super::create_clip)
40///
41/// [`create-clip`](https://dev.twitch.tv/docs/api/reference/#create-clip)
42#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
43#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
44#[must_use]
45#[non_exhaustive]
46pub struct CreateClipRequest<'a> {
47    /// The ID of the broadcaster whose stream you want to create a clip from.
48    #[cfg_attr(feature = "typed-builder", builder(setter(into)))]
49    #[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
50    pub broadcaster_id: Cow<'a, types::UserIdRef>,
51    /// A Boolean value that determines whether the API captures the clip at the moment the viewer requests it or after a delay. If false (default), Twitch captures the clip at the moment the viewer requests it (this is the same clip experience as the Twitch UX). If true, Twitch adds a delay before capturing the clip
52    #[cfg_attr(feature = "typed-builder", builder(default, setter(into)))]
53    pub has_delay: Option<bool>,
54}
55
56impl<'a> CreateClipRequest<'a> {
57    /// Create a new [`CreateClipRequest`] with the given broadcaster_id
58    pub fn broadcaster_id(broadcaster_id: impl types::IntoCow<'a, types::UserIdRef> + 'a) -> Self {
59        Self {
60            broadcaster_id: broadcaster_id.into_cow(),
61            has_delay: None,
62        }
63    }
64
65    /// Sets the has_delay parameter
66    pub const fn has_delay(mut self, has_delay: bool) -> Self {
67        self.has_delay = Some(has_delay);
68        self
69    }
70}
71
72/// Return Value for [Create Clip](super::create_clip)
73///
74/// [`create-clip`](https://dev.twitch.tv/docs/api/reference#create-clip)
75#[derive(PartialEq, Deserialize, Serialize, Debug, Clone)]
76#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
77#[non_exhaustive]
78pub struct CreatedClip {
79    /// ID of the created clip.
80    pub id: types::ClipId,
81    /// A URL that you can use to edit the clip’s title, identify the part of the clip to publish, and publish the clip.
82    pub edit_url: String,
83}
84
85impl Request for CreateClipRequest<'_> {
86    type Response = Vec<CreatedClip>;
87
88    const PATH: &'static str = "clips";
89    #[cfg(feature = "twitch_oauth2")]
90    const SCOPE: twitch_oauth2::Validator =
91        twitch_oauth2::validator![twitch_oauth2::Scope::ClipsEdit];
92}
93
94impl RequestGet for CreateClipRequest<'_> {}
95
96#[cfg(test)]
97#[test]
98fn test_request() {
99    use helix::*;
100    let req = CreateClipRequest::broadcaster_id("44322889");
101
102    let data = br#"
103    {
104        "data":
105        [{
106           "id": "FiveWordsForClipSlug",
107           "edit_url": "http://clips.twitch.tv/FiveWordsForClipSlug/edit"
108        }]
109     }
110    "#
111    .to_vec();
112
113    let http_response = http::Response::builder().body(data).unwrap();
114
115    let uri = req.get_uri().unwrap();
116
117    assert_eq!(
118        uri.to_string(),
119        "https://api.twitch.tv/helix/clips?broadcaster_id=44322889"
120    );
121
122    dbg!(CreateClipRequest::parse_response(Some(req), &uri, http_response).unwrap());
123}