use reqwest::Method;
use serde::Deserialize;
use crate::{
client::{Client, Traced},
error::SdkError,
};
pub mod models;
use models::{CreateFileSystemRequest, FileSystem};
#[derive(Debug, Default, Deserialize)]
struct FileSystemsPage {
#[serde(default)]
items: Vec<FileSystem>,
#[serde(default)]
pagination: Option<Pagination>,
}
#[derive(Debug, Default, Deserialize)]
struct Pagination {
#[serde(default)]
next: Option<String>,
}
fn next_request_path(next: &str) -> String {
if let Some(idx) = next.find("://") {
let after = &next[idx + 3..];
match after.find('/') {
Some(slash) => after[slash..].to_string(),
None => "/".to_string(),
}
} else if next.starts_with('/') {
next.to_string()
} else {
format!("/{next}")
}
}
#[derive(Clone)]
pub struct FileSystemsClient {
client: Client,
organization_id: String,
project_id: String,
}
impl FileSystemsClient {
pub fn new(
client: Client,
organization_id: impl Into<String>,
project_id: impl Into<String>,
) -> Self {
Self {
client,
organization_id: organization_id.into(),
project_id: project_id.into(),
}
}
fn endpoint(&self) -> String {
format!(
"/platform/v1/organizations/{}/projects/{}/file-systems",
self.organization_id, self.project_id
)
}
pub async fn create(
&self,
request: &CreateFileSystemRequest,
) -> Result<Traced<FileSystem>, SdkError> {
let req = self
.client
.build_post_json_request(Method::POST, &self.endpoint(), request)?;
self.client.execute_json(req).await
}
pub async fn list(&self) -> Result<Traced<Vec<FileSystem>>, SdkError> {
let mut path = format!("{}?pageSize=100", self.endpoint());
let mut items: Vec<FileSystem> = Vec::new();
loop {
let req = self.client.build_get_json_request(&path, None)?;
let traced = self.client.execute_json::<FileSystemsPage>(req).await?;
let trace_id = traced.trace_id.clone();
let page = traced.into_inner();
items.extend(page.items);
match page.pagination.and_then(|p| p.next) {
Some(next) if !next.is_empty() => path = next_request_path(&next),
_ => return Ok(Traced::new(trace_id, items)),
}
}
}
pub async fn delete(&self, file_system_id: &str) -> Result<Traced<()>, SdkError> {
let encoded = urlencoding::encode(file_system_id);
let path = format!("{}/{}", self.endpoint(), encoded);
let req = self.client.request(Method::DELETE, &path).build()?;
self.client
.execute_traced(req)
.await
.map(|traced| traced.map(|_| ()))
}
}
#[cfg(test)]
mod tests {
use super::next_request_path;
#[test]
fn next_request_path_keeps_absolute_path_and_query() {
let next =
"/platform/v1/organizations/org/projects/proj/file-systems?pageSize=100&next=abc";
assert_eq!(next_request_path(next), next);
}
#[test]
fn next_request_path_reduces_absolute_url_to_path_and_query() {
let next = "https://api.tensorlake.ai/platform/v1/organizations/org/projects/proj/file-systems?next=abc";
assert_eq!(
next_request_path(next),
"/platform/v1/organizations/org/projects/proj/file-systems?next=abc"
);
}
#[test]
fn next_request_path_prefixes_bare_relative_links() {
assert_eq!(
next_request_path("file-systems?next=abc"),
"/file-systems?next=abc"
);
}
}