use crate::ZaiResult;
use crate::client::ZaiClient;
use crate::services::applications::response::{
ApplicationConversationCreateResponse, ApplicationFileStatsResponse,
ApplicationFileUploadResponse, ApplicationHistoryResponse, ApplicationInvokeResponse,
ApplicationSliceInfoResponse, ApplicationVariablesResponse,
};
pub struct ApplicationFileStatsRequest {
pub body: serde_json::Value,
}
impl ApplicationFileStatsRequest {
pub fn new(body: serde_json::Value) -> Self {
Self { body }
}
pub async fn send_via(&self, client: &ZaiClient) -> ZaiResult<ApplicationFileStatsResponse> {
let route = crate::client::routes::APPLICATIONS_FILE_STATS;
let url = client.endpoints().resolve_route(route, &[])?;
client
.send_json::<_, ApplicationFileStatsResponse>(route.method(), url, &self.body)
.await
}
}
pub struct ApplicationFileUploadRequest {
pub files: Vec<(String, Vec<u8>)>,
pub body: serde_json::Value,
}
impl ApplicationFileUploadRequest {
pub fn new(files: Vec<(String, Vec<u8>)>, body: serde_json::Value) -> Self {
Self { files, body }
}
pub async fn send_via(&self, client: &ZaiClient) -> ZaiResult<ApplicationFileUploadResponse> {
let route = crate::client::routes::APPLICATIONS_UPLOAD_FILE;
let url = client.endpoints().resolve_route(route, &[])?;
let mut factory = crate::client::transport::multipart::MultipartBodyFactory::new();
if let serde_json::Value::Object(map) = &self.body {
for (key, value) in map {
let text = match value {
serde_json::Value::String(s) => s.clone(),
other => other.to_string(),
};
factory = factory.field(key.clone(), text)?;
}
}
for (filename, bytes) in &self.files {
factory = factory.bytes_named(
"files",
filename.clone(),
"application/octet-stream",
bytes.clone(),
)?;
}
client
.send_multipart::<ApplicationFileUploadResponse>(route.method(), url, &factory)
.await
}
}
pub struct ApplicationSliceInfoRequest {
pub body: serde_json::Value,
}
impl ApplicationSliceInfoRequest {
pub fn new(body: serde_json::Value) -> Self {
Self { body }
}
pub async fn send_via(&self, client: &ZaiClient) -> ZaiResult<ApplicationSliceInfoResponse> {
let route = crate::client::routes::APPLICATIONS_SLICE_INFO;
let url = client.endpoints().resolve_route(route, &[])?;
client
.send_json::<_, ApplicationSliceInfoResponse>(route.method(), url, &self.body)
.await
}
}
pub struct ApplicationConversationCreateRequest {
pub app_id: String,
pub body: serde_json::Value,
}
impl ApplicationConversationCreateRequest {
pub fn new(app_id: impl Into<String>, body: serde_json::Value) -> Self {
Self {
app_id: app_id.into(),
body,
}
}
pub async fn send_via(
&self,
client: &ZaiClient,
) -> ZaiResult<ApplicationConversationCreateResponse> {
let route = crate::client::routes::APPLICATIONS_CREATE_CONVERSATION;
let url = client.endpoints().resolve_route(route, &[&self.app_id])?;
client
.send_json::<_, ApplicationConversationCreateResponse>(route.method(), url, &self.body)
.await
}
}
pub struct ApplicationVariablesRequest {
pub app_id: String,
}
impl ApplicationVariablesRequest {
pub fn new(app_id: impl Into<String>) -> Self {
Self {
app_id: app_id.into(),
}
}
pub async fn send_via(&self, client: &ZaiClient) -> ZaiResult<ApplicationVariablesResponse> {
let route = crate::client::routes::APPLICATIONS_VARIABLES;
let url = client.endpoints().resolve_route(route, &[&self.app_id])?;
client
.send_empty::<ApplicationVariablesResponse>(route.method(), url)
.await
}
}
pub struct ApplicationHistoryRequest {
pub app_id: String,
pub conversation_id: String,
}
impl ApplicationHistoryRequest {
pub fn new(app_id: impl Into<String>, conversation_id: impl Into<String>) -> Self {
Self {
app_id: app_id.into(),
conversation_id: conversation_id.into(),
}
}
pub async fn send_via(&self, client: &ZaiClient) -> ZaiResult<ApplicationHistoryResponse> {
let route = crate::client::routes::APPLICATIONS_HISTORY;
let url = client
.endpoints()
.resolve_route(route, &[&self.app_id, &self.conversation_id])?;
client
.send_empty::<ApplicationHistoryResponse>(route.method(), url)
.await
}
}
pub struct ApplicationInvokeRequest {
pub body: serde_json::Value,
}
impl ApplicationInvokeRequest {
pub fn new(body: serde_json::Value) -> Self {
Self { body }
}
pub async fn send_via(&self, client: &ZaiClient) -> ZaiResult<ApplicationInvokeResponse> {
let route = crate::client::routes::APPLICATIONS_INVOKE;
let url = client.endpoints().resolve_route(route, &[])?;
client
.send_json::<_, ApplicationInvokeResponse>(route.method(), url, &self.body)
.await
}
}