Skip to main content

zai_rs/model/gen_video_async/
data.rs

1use std::sync::Arc;
2
3use serde::Serialize;
4use validator::Validate;
5
6use super::{
7    super::traits::*,
8    video_request::{Fps, ImageUrl, VideoBody, VideoDuration, VideoQuality, VideoSize},
9};
10use crate::client::{
11    endpoints::{ApiBase, EndpointConfig, paths},
12    http::{HttpClient, HttpClientConfig},
13};
14
15/// Video generation request structure
16/// Handles HTTP requests for video generation API
17pub struct VideoGenRequest<N>
18where
19    N: ModelName + VideoGen + Serialize,
20{
21    /// API key for authentication
22    pub key: String,
23    url: String,
24    endpoint_config: EndpointConfig,
25    api_base: ApiBase,
26    http_config: Arc<HttpClientConfig>,
27    /// Request Body
28    body: VideoBody<N>,
29}
30
31impl<N> VideoGenRequest<N>
32where
33    N: ModelName + VideoGen + Serialize,
34{
35    /// Create a new video generation request
36    ///
37    /// # Arguments
38    /// * `model` - Video generation model implementing VideoGen trait
39    /// * `body` - Video generation parameters and configuration
40    /// * `key` - API key for authentication
41    pub fn new(model: N, key: String) -> Self {
42        let body = VideoBody::new(model);
43        let endpoint_config = EndpointConfig::default();
44        let api_base = ApiBase::PaasV4;
45        let url = endpoint_config.url(&api_base, paths::VIDEOS_GENERATIONS);
46        Self {
47            key,
48            url,
49            endpoint_config,
50            api_base,
51            http_config: Arc::new(HttpClientConfig::default()),
52            body,
53        }
54    }
55
56    fn rebuild_url(&mut self) {
57        self.url = self
58            .endpoint_config
59            .url(&self.api_base, paths::VIDEOS_GENERATIONS);
60    }
61
62    pub fn with_base_url(mut self, base: impl Into<String>) -> Self {
63        self.api_base = ApiBase::Custom(base.into());
64        self.rebuild_url();
65        self
66    }
67
68    pub fn with_endpoint_config(mut self, endpoint_config: EndpointConfig) -> Self {
69        self.endpoint_config = endpoint_config;
70        self.rebuild_url();
71        self
72    }
73
74    pub fn with_http_config(mut self, config: HttpClientConfig) -> Self {
75        self.http_config = Arc::new(config);
76        self
77    }
78
79    /// Set the prompt for video generation
80    pub fn with_prompt(mut self, prompt: impl Into<String>) -> Self {
81        self.body = self.body.with_prompt(prompt);
82        self
83    }
84
85    /// Set the quality mode (speed or quality)
86    pub fn with_quality(mut self, quality: VideoQuality) -> Self {
87        self.body = self.body.with_quality(quality);
88        self
89    }
90
91    /// Enable/disable audio generation
92    pub fn with_audio(mut self, with_audio: bool) -> Self {
93        self.body = self.body.with_audio(with_audio);
94        self
95    }
96
97    /// Enable/disable watermark
98    pub fn with_watermark_enabled(mut self, watermark_enabled: bool) -> Self {
99        self.body = self.body.with_watermark_enabled(watermark_enabled);
100        self
101    }
102
103    /// Set image URL(s) for video generation
104    pub fn with_image_url(mut self, image_url: ImageUrl) -> Self {
105        self.body = self.body.with_image_url(image_url);
106        self
107    }
108
109    /// Set video resolution size
110    pub fn with_size(mut self, size: VideoSize) -> Self {
111        self.body = self.body.with_size(size);
112        self
113    }
114
115    /// Set video frame rate (30 or 60 FPS)
116    pub fn with_fps(mut self, fps: Fps) -> Self {
117        self.body = self.body.with_fps(fps);
118        self
119    }
120
121    /// Set video duration (5 or 10 seconds)
122    pub fn with_duration(mut self, duration: VideoDuration) -> Self {
123        self.body = self.body.with_duration(duration);
124        self
125    }
126
127    /// Set custom request ID
128    pub fn with_request_id(mut self, request_id: String) -> Self {
129        self.body = self.body.with_request_id(request_id);
130        self
131    }
132
133    /// Set user ID for policy enforcement
134    pub fn with_user_id(mut self, user_id: String) -> Self {
135        self.body = self.body.with_user_id(user_id);
136        self
137    }
138}
139
140impl<N> VideoGenRequest<N>
141where
142    N: ModelName + VideoGen + Serialize,
143{
144    /// Validate request parameters for video generation
145    pub fn validate(&self) -> crate::ZaiResult<()> {
146        self.body
147            .validate()
148            .map_err(crate::client::error::ZaiError::from)?;
149        Ok(())
150    }
151}
152
153impl<N> HttpClient for VideoGenRequest<N>
154where
155    N: ModelName + VideoGen + Serialize,
156{
157    type Body = VideoBody<N>;
158    /// API URL type
159    type ApiUrl = String;
160    /// API key type
161    type ApiKey = String;
162
163    /// Get the API endpoint URL
164    fn api_url(&self) -> &Self::ApiUrl {
165        &self.url
166    }
167
168    /// Get the API key for authentication
169    fn api_key(&self) -> &Self::ApiKey {
170        &self.key
171    }
172
173    /// Get the request body containing video generation parameters
174    fn body(&self) -> &Self::Body {
175        &self.body
176    }
177
178    fn http_config(&self) -> Arc<HttpClientConfig> {
179        Arc::clone(&self.http_config)
180    }
181}