zai_rs/model/gen_video_async/
data.rs

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