moq_api/
client.rs

1use url::Url;
2
3use crate::{ApiError, Origin};
4
5#[derive(Clone)]
6pub struct Client {
7    // The address of the moq-api server
8    url: Url,
9
10    client: reqwest::Client,
11}
12
13impl Client {
14    pub fn new(url: Url) -> Self {
15        let client = reqwest::Client::new();
16        Self { url, client }
17    }
18
19    pub async fn get_origin(&self, namespace: &str) -> Result<Option<Origin>, ApiError> {
20        let url = self.url.join(&format!("origin/{namespace}"))?;
21        let resp = self.client.get(url).send().await?;
22        if resp.status() == reqwest::StatusCode::NOT_FOUND {
23            return Ok(None);
24        }
25
26        let origin: Origin = resp.json().await?;
27        Ok(Some(origin))
28    }
29
30    pub async fn set_origin(&self, namespace: &str, origin: Origin) -> Result<(), ApiError> {
31        let url = self.url.join(&format!("origin/{namespace}"))?;
32
33        let resp = self.client.post(url).json(&origin).send().await?;
34        resp.error_for_status()?;
35
36        Ok(())
37    }
38
39    pub async fn delete_origin(&self, namespace: &str) -> Result<(), ApiError> {
40        let url = self.url.join(&format!("origin/{namespace}"))?;
41
42        let resp = self.client.delete(url).send().await?;
43        resp.error_for_status()?;
44
45        Ok(())
46    }
47
48    pub async fn patch_origin(&self, namespace: &str, origin: Origin) -> Result<(), ApiError> {
49        let url = self.url.join(&format!("origin/{namespace}"))?;
50
51        let resp = self.client.patch(url).json(&origin).send().await?;
52        resp.error_for_status()?;
53
54        Ok(())
55    }
56}