Skip to main content

zai_rs/model/moderation/
data.rs

1//! Content-moderation request builder.
2
3use super::models::*;
4use crate::client::ZaiClient;
5
6/// Validated content-moderation request.
7///
8/// Credentials and transport are supplied by the [`ZaiClient`] passed to
9/// [`send_via`](Self::send_via).
10pub struct Moderation {
11    /// Moderation request body.
12    body: ModerationRequest,
13}
14
15impl Moderation {
16    /// Create a text-moderation request.
17    ///
18    /// The service accepts at most 2,000 Unicode scalar values.
19    pub fn new_text(text: impl Into<String>) -> Self {
20        let body = ModerationRequest::new_text(text);
21        Self { body }
22    }
23
24    /// Create a multimedia-moderation request for `url`.
25    pub fn new_multimedia(content_type: MediaType, url: impl Into<String>) -> Self {
26        let body = ModerationRequest::new_multimedia(content_type, url);
27        Self { body }
28    }
29
30    /// Create a moderation request for multiple structured items.
31    pub fn new_items(items: Vec<ModerationItem>) -> Self {
32        Self {
33            body: ModerationRequest::new_items(items),
34        }
35    }
36
37    /// Mutably borrow the request body.
38    pub fn body_mut(&mut self) -> &mut ModerationRequest {
39        &mut self.body
40    }
41
42    /// Validate the request body constraints before sending.
43    pub fn validate(&self) -> crate::ZaiResult<()> {
44        self.body
45            .validate()
46            .map_err(crate::client::error::ZaiError::from)?;
47        Ok(())
48    }
49
50    /// Validate and send the request through `client`.
51    ///
52    /// This method automatically validates the request before sending.
53    ///
54    pub async fn send_via(&self, client: &ZaiClient) -> crate::ZaiResult<ModerationResponse> {
55        self.validate()?;
56        let route = crate::client::routes::MODERATION_CHECK;
57        let url = client.endpoints().resolve_route(route, &[])?;
58        client
59            .send_json::<_, ModerationResponse>(route.method(), url, &self.body)
60            .await
61    }
62}