twapi_v2/api/
post_2_media_subtitles_create.rs1use crate::responses::subtitles::Subtitles;
2use crate::{
3 api::{Authentication, TwapiOptions, apply_options, 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(
87 apply_options(builder, &self.twapi_options),
88 "POST",
89 &url,
90 &[],
91 )
92 }
93
94 pub async fn execute(
95 self,
96 authentication: &impl Authentication,
97 ) -> Result<(Response, Headers), Error> {
98 execute_twitter(self.build(authentication)).await
99 }
100}
101
102#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
103pub struct Response {
104 pub data: Subtitles,
105 #[serde(flatten)]
106 pub extra: std::collections::HashMap<String, serde_json::Value>,
107}
108
109impl Response {
110 pub fn is_empty_extra(&self) -> bool {
111 let res = self.extra.is_empty() && self.data.is_empty_extra();
112 if !res {
113 println!("Response {:?}", self.extra);
114 }
115 res
116 }
117}