async_dashscope/
config.rs

1// https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation - text-generation
2// https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation - image-generation
3// https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation - 音频理解
4// https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation  - 录音文件识别
5// https://dashscope.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis - 创意海报生成API参考
6
7use derive_builder::Builder;
8use reqwest::header::AUTHORIZATION;
9use secrecy::{ExposeSecret as _, SecretString};
10
11pub const DASHSCOPE_API_BASE: &str = "https://dashscope.aliyuncs.com/api/v1";
12
13#[derive(Debug, Builder)]
14pub struct Config {
15    api_base: String,
16    api_key: SecretString,
17}
18
19impl Config {
20    pub fn url(&self, path: &str) -> String {
21        format!("{}{}", self.api_base, path)
22    }
23    pub fn headers(&self) -> reqwest::header::HeaderMap {
24        let mut headers = reqwest::header::HeaderMap::new();
25        headers.insert("Content-Type", "application/json".parse().unwrap());
26        headers.insert(
27            AUTHORIZATION,
28            format!("Bearer {}", self.api_key.expose_secret())
29                .parse()
30                .unwrap(),
31        );
32        headers
33    }
34}
35
36impl Default for Config {
37    fn default() -> Self {
38        Self {
39            api_base: DASHSCOPE_API_BASE.to_string(),
40            api_key: std::env::var("DASHSCOPE_API_KEY")
41                .unwrap_or_else(|_| "".to_string())
42                .into(),
43        }
44    }
45}