use crate::common::define::{
AsyncResponseFn, BaseRequest, BaseResponse, HttpBuilder, HttpFn, RequestFn,
};
use bytes::Bytes;
use reqwest::{Method, Response};
use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct ApiStorageTruncateRequest {
#[serde(rename = "Path")]
pub path: Option<String>,
}
impl ApiStorageTruncateRequest {
pub fn new() -> Self {
Self::default()
}
pub fn with_path(mut self, path: String) -> Self {
self.path = Some(path);
self
}
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct ApiStorageTruncateResponse {}
impl HttpBuilder for ApiStorageTruncateRequest {
type Response = BaseResponse<ApiStorageTruncateResponse>;
fn builder(self) -> HttpFn<Self::Response> {
Box::new(move || {
let request_fn: RequestFn = Box::new(move || BaseRequest {
method: Method::POST,
uri: "/api/storage/truncate".to_string(),
body: Bytes::from(serde_json::to_vec(&self).unwrap()),
..Default::default()
});
let response_fn: AsyncResponseFn<Self::Response> =
Box::new(|response: Response| Box::pin(async move { Ok(response.json().await?) }));
(request_fn, response_fn)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::common::client::OpenApiClient;
use crate::common::config::{EndpointType, OpenApiConfig};
use tracing::info;
#[tokio::test]
async fn test_api_storage_truncate() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
dotenvy::dotenv()?;
let config = OpenApiConfig::new().load_from_env()?;
let user_id = config.user_id.clone();
let mut client = OpenApiClient::new(config).with_endpoint_type(EndpointType::Cloud);
let http_fn = ApiStorageTruncateRequest::new()
.with_path(format!("/{}/runner.py", user_id))
.builder();
let response = client.send(http_fn).await?;
info!("response: {:#?}", response);
Ok(())
}
}