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