use serde_json::json;
use crate::client::{GitFoxClient, Method, Query};
use crate::error::Result;
use crate::models::{Membership, PublicKey, Space, SpaceSecret, SystemConfig};
use crate::pipeline::urlencode;
pub struct SpacesApi<'a> {
client: &'a GitFoxClient,
}
impl<'a> SpacesApi<'a> {
pub(crate) fn new(client: &'a GitFoxClient) -> Self {
Self { client }
}
pub async fn memberships(&self, page: u32, limit: u32) -> Result<Vec<Membership>> {
let mut q = Query::new();
q.push("sort", "identifier")
.push("order", "asc")
.push("page", page)
.push("limit", limit);
self.client
.get_json(&q.apply("/api/v1/user/memberships"))
.await
}
pub async fn get(&self, space: &str) -> Result<Space> {
self.client
.get_json(&format!("/api/v1/spaces/{}", encode_ref(space)))
.await
}
pub async fn secrets(
&self,
space: &str,
query: Option<&str>,
page: u32,
limit: u32,
) -> Result<Vec<SpaceSecret>> {
let mut q = Query::new();
q.push_opt("query", query)
.push("page", page)
.push("limit", limit);
self.client
.get_json(&q.apply(&format!("/api/v1/spaces/{}/secrets", encode_ref(space))))
.await
}
pub async fn secret(&self, space: &str, identifier: &str) -> Result<SpaceSecret> {
self.client.get_json(&secret_path(space, identifier)).await
}
pub async fn create_secret(
&self,
space: &str,
identifier: &str,
data: &str,
description: Option<&str>,
) -> Result<SpaceSecret> {
let mut body = json!({ "space_ref": space, "identifier": identifier, "data": data });
if let Some(description) = description {
body["description"] = json!(description);
}
self.client
.request(Method::POST, "/api/v1/secrets", Some(&body), &[])
.await?
.deserialize()
}
pub async fn update_secret(
&self,
space: &str,
identifier: &str,
data: &str,
description: Option<&str>,
) -> Result<SpaceSecret> {
let mut body = json!({ "data": data });
if let Some(description) = description {
body["description"] = json!(description);
}
self.client
.request(
Method::PATCH,
&secret_path(space, identifier),
Some(&body),
&[],
)
.await?
.deserialize()
}
pub async fn delete_secret(&self, space: &str, identifier: &str) -> Result<()> {
self.client
.request(Method::DELETE, &secret_path(space, identifier), None, &[])
.await
.map(|_| ())
}
pub async fn public_keys(&self, page: u32, limit: u32) -> Result<Vec<PublicKey>> {
let mut q = Query::new();
q.push("sort", "created")
.push("order", "asc")
.push("page", page)
.push("limit", limit);
self.client.get_json(&q.apply("/api/v1/user/keys")).await
}
pub async fn add_public_key(&self, identifier: &str, content: &str) -> Result<PublicKey> {
self.client
.request(
Method::POST,
"/api/v1/user/keys",
Some(&json!({ "identifier": identifier, "content": content, "usage": "auth" })),
&[],
)
.await?
.deserialize()
}
pub async fn delete_public_key(&self, identifier: &str) -> Result<()> {
self.client
.request(
Method::DELETE,
&format!("/api/v1/user/keys/{}", urlencode(identifier)),
None,
&[],
)
.await
.map(|_| ())
}
pub async fn system_config(&self) -> Result<SystemConfig> {
self.client.get_json("/api/v1/system/config").await
}
}
pub fn encode_ref(reference: &str) -> String {
reference
.trim_matches('/')
.split('/')
.map(urlencode)
.collect::<Vec<_>>()
.join("%2F")
}
fn secret_path(space: &str, identifier: &str) -> String {
format!(
"/api/v1/secrets/{}%2F{}",
encode_ref(space),
urlencode(identifier)
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_secret_reference_encodes_the_space_and_the_name_as_one_segment() {
assert_eq!(
secret_path("ai-repos", "deploy-host"),
"/api/v1/secrets/ai-repos%2Fdeploy-host"
);
assert_eq!(
secret_path("org/team", "TOKEN"),
"/api/v1/secrets/org%2Fteam%2FTOKEN"
);
}
}