zai_rs/model/moderation/
data.rs

1//! # Content Moderation API
2//!
3//! This module provides the content moderation client for analyzing text, image,
4//! audio, and video content for safety risks.
5
6use super::models::*;
7use crate::client::http::HttpClient;
8
9/// Content moderation client.
10///
11/// This client provides functionality to moderate content for safety risks,
12/// supporting text, image, audio, and video formats.
13///
14/// ## Examples
15///
16/// ```rust,ignore
17/// let api_key = "your-api-key".to_string();
18/// let moderation = Moderation::new_text("审核内容安全样例字符串。", api_key);
19/// let result = moderation.send().await?;
20/// ```
21pub struct Moderation {
22    /// API key for authentication
23    pub key: String,
24    /// Moderation request body
25    body: ModerationRequest,
26}
27
28impl Moderation {
29    /// Creates a new moderation request with text content.
30    ///
31    /// ## Arguments
32    ///
33    /// * `text` - The text content to moderate (max 2000 characters)
34    /// * `key` - API key for authentication
35    ///
36    /// ## Returns
37    ///
38    /// A new `Moderation` instance configured for text moderation.
39    pub fn new_text(text: impl Into<String>, key: String) -> Self {
40        let body = ModerationRequest::new_text(text);
41        Self { body, key }
42    }
43
44    /// Creates a new moderation request with multimedia content.
45    ///
46    /// ## Arguments
47    ///
48    /// * `content_type` - The type of multimedia content (image, audio, video)
49    /// * `url` - URL to the multimedia content
50    /// * `key` - API key for authentication
51    ///
52    /// ## Returns
53    ///
54    /// A new `Moderation` instance configured for multimedia moderation.
55    pub fn new_multimedia(content_type: MediaType, url: impl Into<String>, key: String) -> Self {
56        let body = ModerationRequest::new_multimedia(content_type, url);
57        Self { body, key }
58    }
59
60    /// Gets mutable access to the request body for further customization.
61    pub fn body_mut(&mut self) -> &mut ModerationRequest {
62        &mut self.body
63    }
64
65    /// Validates the moderation request parameters.
66    pub fn validate(&self) -> anyhow::Result<()> {
67        self.body
68            .validate()
69            .map_err(|e: validator::ValidationErrors| anyhow::anyhow!(e))?;
70        Ok(())
71    }
72
73    /// Sends the moderation request and returns the structured response.
74    ///
75    /// This method automatically validates the request before sending.
76    ///
77    /// ## Returns
78    ///
79    /// A `ModerationResponse` containing the moderation results and usage statistics.
80    pub async fn send(&self) -> anyhow::Result<ModerationResponse> {
81        self.validate()?;
82        let resp: reqwest::Response = self.post().await?;
83        let parsed = resp.json::<ModerationResponse>().await?;
84        Ok(parsed)
85    }
86}
87
88impl HttpClient for Moderation {
89    type Body = ModerationRequest;
90    type ApiUrl = &'static str;
91    type ApiKey = String;
92
93    /// Returns the Zhipu AI moderation API endpoint URL.
94    fn api_url(&self) -> &Self::ApiUrl {
95        &"https://open.bigmodel.cn/api/paas/v4/moderations"
96    }
97
98    fn api_key(&self) -> &Self::ApiKey {
99        &self.key
100    }
101
102    fn body(&self) -> &Self::Body {
103        &self.body
104    }
105}