async_dashscope/operation/
image2image.rs

1use crate::{Client, error::Result};
2pub use output::*;
3pub use param::*;
4use secrecy::ExposeSecret;
5
6mod output;
7mod param;
8
9const IMAGE2IMAGE_PATH: &str = "/services/aigc/image2image/image-synthesis";
10
11pub struct Image2Image<'a> {
12    client: &'a Client,
13}
14
15impl<'a> Image2Image<'a> {
16    pub fn new(client: &'a Client) -> Self {
17        Self { client }
18    }
19
20    /// 调用图像到图像转换服务
21    ///
22    /// 此方法会将输入图像上传到OSS,然后发送异步请求到DashScope的图像转换服务。
23    ///
24    /// # 参数
25    /// * `request` - 图像转换请求参数,包含输入图像和转换参数
26    ///
27    /// # 返回值
28    /// 返回 `Result<Image2ImageOutput>`,包含任务ID和初始状态信息
29    ///
30    /// # 错误
31    /// 可能返回以下错误:
32    /// - 文件上传失败
33    /// - API请求失败
34    /// - 参数验证失败
35    ///
36    /// # 注意事项
37    /// - 此方法会启用异步模式(X-DashScope-Async头)
38    /// - 上传的文件会自动清理,无需手动处理
39    pub async fn call(&self, request: Image2imageParam) -> Result<Image2ImageOutput> {
40        // 检查参数
41        // let validators = check_model_parameters(&request.model);
42        // for valid in validators {
43        //     valid.validate(&request)?;
44        // }
45        let request = request
46            .upload_file_to_oss(self.client.config().api_key().expose_secret())
47            .await?;
48
49        let mut headers = self.client.config().headers();
50        headers.insert("X-DashScope-Async", "enable".parse().unwrap());
51
52        // 发送POST请求到生成服务,并等待结果
53        self.client
54            .post_with_headers(IMAGE2IMAGE_PATH, request, headers)
55            .await
56    }
57}