use crate::common::define::{
AsyncResponseFn, BaseRequest, BaseResponse, HttpBuilder, HttpFn, RequestFn,
};
use crate::model::file::FileInfo;
use reqwest::{Method, Response};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct ApiStorageStatRequest {
#[serde(rename = "Path")]
pub path: Option<String>,
}
impl ApiStorageStatRequest {
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 ApiStorageStatResponse {
#[serde(rename = "File")]
pub file: Option<FileInfo>,
}
impl HttpBuilder for ApiStorageStatRequest {
type Response = BaseResponse<ApiStorageStatResponse>;
fn builder(self) -> HttpFn<Self::Response> {
Box::new(move || {
let request_fn: RequestFn = Box::new(move || {
let mut queries = HashMap::new();
if let Some(path) = self.path {
queries.insert("Path".to_string(), path);
}
BaseRequest {
method: Method::GET,
uri: "/api/storage/stat".to_string(),
queries: Some(queries),
..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_stat() -> 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 = ApiStorageStatRequest::new()
.with_path(format!("/{}/runner.py", user_id))
.builder();
let response = client.send(http_fn).await?;
info!("response: {:#?}", response);
Ok(())
}
}