use super::models::*;
use crate::client::http::HttpClient;
pub struct Moderation {
pub key: String,
body: ModerationRequest,
}
impl Moderation {
pub fn new_text(text: impl Into<String>, key: String) -> Self {
let body = ModerationRequest::new_text(text);
Self { body, key }
}
pub fn new_multimedia(content_type: MediaType, url: impl Into<String>, key: String) -> Self {
let body = ModerationRequest::new_multimedia(content_type, url);
Self { body, key }
}
pub fn body_mut(&mut self) -> &mut ModerationRequest {
&mut self.body
}
pub fn validate(&self) -> crate::ZaiResult<()> {
self.body
.validate()
.map_err(crate::client::error::ZaiError::from)?;
Ok(())
}
pub async fn send(&self) -> crate::ZaiResult<ModerationResponse> {
self.validate()?;
let resp: reqwest::Response = self.post().await?;
let parsed = resp.json::<ModerationResponse>().await?;
Ok(parsed)
}
}
impl HttpClient for Moderation {
type Body = ModerationRequest;
type ApiUrl = &'static str;
type ApiKey = String;
fn api_url(&self) -> &Self::ApiUrl {
&"https://open.bigmodel.cn/api/paas/v4/moderations"
}
fn api_key(&self) -> &Self::ApiKey {
&self.key
}
fn body(&self) -> &Self::Body {
&self.body
}
}