Skip to main content

zai_rs/model/gen_video_async/
data.rs

1use serde::Serialize;
2use validator::Validate;
3
4use super::{
5    super::traits::*,
6    video_request::{Fps, ImageUrl, VideoBody, VideoDuration, VideoQuality, VideoSize},
7};
8use crate::client::http::HttpClient;
9
10/// Video generation request structure
11/// Handles HTTP requests for video generation API
12pub struct VideoGenRequest<N>
13where
14    N: ModelName + VideoGen + Serialize,
15{
16    /// API key for authentication
17    pub key: String,
18    /// Request Body
19    body: VideoBody<N>,
20}
21
22impl<N> VideoGenRequest<N>
23where
24    N: ModelName + VideoGen + Serialize,
25{
26    /// Create a new video generation request
27    ///
28    /// # Arguments
29    /// * `model` - Video generation model implementing VideoGen trait
30    /// * `body` - Video generation parameters and configuration
31    /// * `key` - API key for authentication
32    pub fn new(model: N, key: String) -> Self {
33        let body = VideoBody::new(model);
34        Self { key, body }
35    }
36
37    /// Set the prompt for video generation
38    pub fn with_prompt(mut self, prompt: impl Into<String>) -> Self {
39        self.body = self.body.with_prompt(prompt);
40        self
41    }
42
43    /// Set the quality mode (speed or quality)
44    pub fn with_quality(mut self, quality: VideoQuality) -> Self {
45        self.body = self.body.with_quality(quality);
46        self
47    }
48
49    /// Enable/disable audio generation
50    pub fn with_audio(mut self, with_audio: bool) -> Self {
51        self.body = self.body.with_audio(with_audio);
52        self
53    }
54
55    /// Enable/disable watermark
56    pub fn with_watermark_enabled(mut self, watermark_enabled: bool) -> Self {
57        self.body = self.body.with_watermark_enabled(watermark_enabled);
58        self
59    }
60
61    /// Set image URL(s) for video generation
62    pub fn with_image_url(mut self, image_url: ImageUrl) -> Self {
63        self.body = self.body.with_image_url(image_url);
64        self
65    }
66
67    /// Set video resolution size
68    pub fn with_size(mut self, size: VideoSize) -> Self {
69        self.body = self.body.with_size(size);
70        self
71    }
72
73    /// Set video frame rate (30 or 60 FPS)
74    pub fn with_fps(mut self, fps: Fps) -> Self {
75        self.body = self.body.with_fps(fps);
76        self
77    }
78
79    /// Set video duration (5 or 10 seconds)
80    pub fn with_duration(mut self, duration: VideoDuration) -> Self {
81        self.body = self.body.with_duration(duration);
82        self
83    }
84
85    /// Set custom request ID
86    pub fn with_request_id(mut self, request_id: String) -> Self {
87        self.body = self.body.with_request_id(request_id);
88        self
89    }
90
91    /// Set user ID for policy enforcement
92    pub fn with_user_id(mut self, user_id: String) -> Self {
93        self.body = self.body.with_user_id(user_id);
94        self
95    }
96}
97
98impl<N> VideoGenRequest<N>
99where
100    N: ModelName + VideoGen + Serialize,
101{
102    /// Validate request parameters for video generation
103    pub fn validate(&self) -> crate::ZaiResult<()> {
104        self.body
105            .validate()
106            .map_err(crate::client::error::ZaiError::from)?;
107        Ok(())
108    }
109}
110
111impl<N> HttpClient for VideoGenRequest<N>
112where
113    N: ModelName + VideoGen + Serialize,
114{
115    type Body = VideoBody<N>;
116    /// API URL type
117    type ApiUrl = &'static str;
118    /// API key type
119    type ApiKey = String;
120
121    /// Get the API endpoint URL
122    fn api_url(&self) -> &Self::ApiUrl {
123        &"https://open.bigmodel.cn/api/paas/v4/videos/generations"
124    }
125
126    /// Get the API key for authentication
127    fn api_key(&self) -> &Self::ApiKey {
128        &self.key
129    }
130
131    /// Get the request body containing video generation parameters
132    fn body(&self) -> &Self::Body {
133        &self.body
134    }
135}