zai_rs/model/gen_video_async/
data.rs1use 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
15pub struct VideoGenRequest<N>
18where
19 N: ModelName + VideoGen + Serialize,
20{
21 pub key: String,
23 url: String,
24 endpoint_config: EndpointConfig,
25 api_base: ApiBase,
26 http_config: Arc<HttpClientConfig>,
27 body: VideoBody<N>,
29}
30
31impl<N> VideoGenRequest<N>
32where
33 N: ModelName + VideoGen + Serialize,
34{
35 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 pub fn with_prompt(mut self, prompt: impl Into<String>) -> Self {
81 self.body = self.body.with_prompt(prompt);
82 self
83 }
84
85 pub fn with_quality(mut self, quality: VideoQuality) -> Self {
87 self.body = self.body.with_quality(quality);
88 self
89 }
90
91 pub fn with_audio(mut self, with_audio: bool) -> Self {
93 self.body = self.body.with_audio(with_audio);
94 self
95 }
96
97 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 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 pub fn with_size(mut self, size: VideoSize) -> Self {
111 self.body = self.body.with_size(size);
112 self
113 }
114
115 pub fn with_fps(mut self, fps: Fps) -> Self {
117 self.body = self.body.with_fps(fps);
118 self
119 }
120
121 pub fn with_duration(mut self, duration: VideoDuration) -> Self {
123 self.body = self.body.with_duration(duration);
124 self
125 }
126
127 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 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 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 type ApiUrl = String;
160 type ApiKey = String;
162
163 fn api_url(&self) -> &Self::ApiUrl {
165 &self.url
166 }
167
168 fn api_key(&self) -> &Self::ApiKey {
170 &self.key
171 }
172
173 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}