d_id/endpoints/video/
animations.rs

1// File: animations.rs
2// Path: src/endpoints/video/animations.rs
3
4use super::*;
5
6const ANIMATIONS_PATH: &str = "/animations";
7
8
9#[derive(Serialize, Deserialize, Debug)]
10pub struct AnimationRequestBody {
11    source_url: String,
12    #[serde(skip_serializing_if = "String::is_empty")]
13    driver_url: String,
14    #[serde(skip_serializing_if = "String::is_empty")]
15    result_url: String,
16    #[serde(skip_serializing_if = "String::is_empty")]
17    webhook: String,
18    #[serde(skip_serializing_if = "String::is_empty")]
19    user_data: String,
20    #[serde(skip_serializing_if = "Option::is_none")]
21    face: Option<Face>,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    config: Option<Config>,
24}
25
26impl AnimationRequestBody {
27    pub async fn create_animation(&self) -> Result<PostAnimationResponse> {
28        let c = ClientBuilder::new()?
29            .method(POST)?
30            .path(ANIMATIONS_PATH)?
31            .header(CONTENT_TYPE, APPLICATION_JSON)?
32            .build()?;
33
34        let body = serde_json::to_string(&self)?;
35
36        let resp = c.send_request(Full::<Bytes>::new(body.into())).await?;
37
38        let animation_resp = serde_json::from_slice::<PostAnimationResponse>(&resp.as_ref())?;
39
40        Ok(animation_resp)
41    }
42
43}
44
45#[derive(Serialize, Deserialize, Debug)]
46pub struct AnimationRequestBodyBuilder {
47    source_url: Option<String>,
48    driver_url: Option<String>,
49    result_url: Option<String>,
50    webhook: Option<String>,
51    user_data: Option<String>,
52    face: Option<Face>,
53    config: Option<Config>,
54}
55
56impl AnimationRequestBodyBuilder {
57    pub fn new() -> Self {
58        Self {
59            source_url: None,
60            driver_url: None,
61            result_url: None,
62            webhook: None,
63            user_data: None,
64            face: None,
65            config: None,
66        }
67    }
68
69    pub fn source_url(mut self, source_url: String) -> Self {
70        self.source_url = Some(source_url);
71        self
72    }
73
74    pub fn driver_url(mut self, driver_url: String) -> Self {
75        self.driver_url = Some(driver_url);
76        self
77    }
78
79    pub fn result_url(mut self, result_url: String) -> Self {
80        self.result_url = Some(result_url);
81        self
82    }
83
84    pub fn webhook(mut self, webhook: String) -> Self {
85        self.webhook = Some(webhook);
86        self
87    }
88
89    pub fn user_data(mut self, user_data: String) -> Self {
90        self.user_data = Some(user_data);
91        self
92    }
93
94    pub fn face(mut self, face: Face) -> Self {
95        self.face = Some(face);
96        self
97    }
98
99    pub fn config(mut self, config: Config) -> Self {
100        self.config = Some(config);
101        self
102    }
103
104    pub fn build(self) -> Result<AnimationRequestBody> {
105        let source_url = self.source_url.ok_or(RequestBodyBuildError::SourceUrlNotSet)?;
106
107        Ok(AnimationRequestBody {
108            source_url,
109            driver_url: self.driver_url.unwrap_or_default(),
110            result_url: self.result_url.unwrap_or_default(),
111            webhook: self.webhook.unwrap_or_default(),
112            user_data: self.user_data.unwrap_or_default(),
113            face: self.face,
114            config: self.config,
115        })
116    }
117}
118
119#[derive(Serialize, Deserialize, Debug)]
120pub struct PostAnimationResponse {
121    pub id: String,
122    pub object: String,
123    pub status: String,
124    pub created_by: String,
125    pub created_at: String,
126}
127
128#[derive(Serialize, Deserialize, Debug)]
129pub struct Animation {
130    pub id: String,
131    pub user_id: String,
132    pub source_url: String,
133    //pub driver_url: String,
134    pub status: String,
135    //pub created_by: String,
136    //pub created_at: String,
137    //pub started_at: String,
138    pub modified_at: String,
139    #[serde(default)]
140    pub result_url: String,
141    #[serde(default)]
142    pub error: Option<AnimationError>,
143    //pub webhook: String,
144    //pub config: Config,
145}
146
147#[derive(Serialize, Deserialize, Debug)]
148pub struct GetAnimationsResponse {
149    pub animations: Vec<Animation>,
150}
151
152
153pub async fn get_animation(animation_id: &str) -> Result<Animation> {
154    let c = ClientBuilder::new()?
155        .method(GET)?
156        .path(&format!("{}/{}", ANIMATIONS_PATH, animation_id))?
157        .header(CONTENT_TYPE, APPLICATION_JSON)?
158        .build()?;
159
160    let resp = c.send_request(Empty::<Bytes>::new()).await?;
161
162    let animation_resp = serde_json::from_slice::<Animation>(&resp.as_ref())?;
163
164    Ok(animation_resp)
165}
166
167pub async fn get_animations() -> Result<GetAnimationsResponse> {
168    let c = ClientBuilder::new()?
169        .method(GET)?
170        .path(ANIMATIONS_PATH)?
171        .header(ACCEPT, APPLICATION_JSON)?
172        .build()?;
173
174    let resp = c.send_request(Empty::<Bytes>::new()).await?;
175
176    let animations = serde_json::from_slice::<GetAnimationsResponse>(&resp.as_ref())?;
177
178    Ok(animations)
179}
180
181pub async fn delete_animation(animation_id: &str) -> Result<()> {
182    let c = ClientBuilder::new()?
183        .method(DELETE)?
184        .path(&format!("{}/{}", ANIMATIONS_PATH, animation_id))?
185        .header(CONTENT_TYPE, APPLICATION_JSON)?
186        .build()?;
187
188    let _resp = c.send_request(Empty::<Bytes>::new()).await?;
189
190
191    Ok(())
192}
193
194#[derive(Serialize, Deserialize, Debug)]
195pub struct AnimationError {
196    pub kind: String,
197    pub description: String,
198}