Skip to main content

twapi_v2/api/
post_2_media_subtitles_create.rs

1use crate::responses::subtitles::Subtitles;
2use crate::{
3    api::{Authentication, TwapiOptions, execute_twitter, make_url},
4    error::Error,
5    headers::Headers,
6};
7use reqwest::RequestBuilder;
8use serde::{Deserialize, Serialize};
9
10const URL: &str = "/2/media/subtitles/create";
11
12#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Clone, Default)]
13pub enum MediaCategory {
14    #[serde(rename = "amplify_video")]
15    #[default]
16    AmplifyVideo,
17    #[serde(rename = "tweet_gif")]
18    TweetGif,
19    #[serde(rename = "tweet_image")]
20    TweetImage,
21    #[serde(rename = "tweet_video")]
22    TweetVideo,
23    #[serde(rename = "subtitles")]
24    Subtitles,
25}
26
27impl std::fmt::Display for MediaCategory {
28    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
29        match self {
30            Self::AmplifyVideo => write!(f, "amplify_video"),
31            Self::TweetGif => write!(f, "tweet_gif"),
32            Self::TweetImage => write!(f, "tweet_image"),
33            Self::TweetVideo => write!(f, "tweet_video"),
34            Self::Subtitles => write!(f, "subtitles"),
35        }
36    }
37}
38
39#[derive(Serialize, Deserialize, Debug, Default, Clone)]
40pub struct Subtitle {
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub display_name: Option<String>,
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub language_code: Option<String>,
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub media_id: Option<String>,
47}
48
49#[derive(Serialize, Deserialize, Debug, Default, Clone)]
50pub struct SubtitleInfo {
51    #[serde(skip_serializing_if = "Option::is_none")]
52    pub subtitles: Option<Vec<Subtitle>>,
53}
54
55#[derive(Serialize, Deserialize, Debug, Default, Clone)]
56pub struct Body {
57    pub media_category: MediaCategory,
58    pub media_id: String,
59    #[serde(skip_serializing_if = "Option::is_none")]
60    pub subtitle_info: Option<SubtitleInfo>,
61}
62
63#[derive(Debug, Clone, Default)]
64pub struct Api {
65    body: Body,
66    twapi_options: Option<TwapiOptions>,
67}
68
69impl Api {
70    pub fn new(body: Body) -> Self {
71        Self {
72            body,
73            ..Default::default()
74        }
75    }
76
77    pub fn twapi_options(mut self, value: TwapiOptions) -> Self {
78        self.twapi_options = Some(value);
79        self
80    }
81
82    pub fn build(&self, authentication: &impl Authentication) -> RequestBuilder {
83        let client = reqwest::Client::new();
84        let url = make_url(&self.twapi_options, URL);
85        let builder = client.post(&url).json(&self.body);
86        authentication.execute(builder, "POST", &url, &[])
87    }
88
89    pub async fn execute(
90        &self,
91        authentication: &impl Authentication,
92    ) -> Result<(Response, Headers), Error> {
93        execute_twitter(|| self.build(authentication), &self.twapi_options).await
94    }
95}
96
97#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
98pub struct Response {
99    pub data: Subtitles,
100    #[serde(flatten)]
101    pub extra: std::collections::HashMap<String, serde_json::Value>,
102}
103
104impl Response {
105    pub fn is_empty_extra(&self) -> bool {
106        let res = self.extra.is_empty() && self.data.is_empty_extra();
107        if !res {
108            println!("Response {:?}", self.extra);
109        }
110        res
111    }
112}