Skip to main content

mesa_dev/resources/
content.rs

1//! Content operations (files and directories).
2//!
3//! Access via [`MesaClient::content`](crate::MesaClient::content):
4//!
5//! ```rust,no_run
6//! # use mesa_dev::{Mesa, MesaError};
7//! # async fn run() -> Result<(), MesaError> {
8//! # let client = Mesa::new("key");
9//! let root = client.content("my-org", "my-repo").get(None, None).await?;
10//! # Ok(())
11//! # }
12//! ```
13
14use http::Method;
15
16use crate::client::MesaClient;
17use crate::error::MesaError;
18use crate::http_client::HttpClient;
19use crate::models::Content;
20
21/// Operations on repository content.
22pub struct ContentResource<'c, C: HttpClient> {
23    client: &'c MesaClient<C>,
24    org: String,
25    repo: String,
26}
27
28impl<'c, C: HttpClient> ContentResource<'c, C> {
29    pub(crate) fn new(client: &'c MesaClient<C>, org: String, repo: String) -> Self {
30        Self { client, org, repo }
31    }
32
33    /// Get file or directory content.
34    ///
35    /// - `path`: file or directory path within the repo (optional, defaults to root).
36    /// - `ref_`: branch name or commit SHA (optional, defaults to the default branch).
37    ///
38    /// # Errors
39    ///
40    /// Returns [`MesaError`] if the API request fails.
41    pub async fn get(&self, path: Option<&str>, ref_: Option<&str>) -> Result<Content, MesaError> {
42        let url_path = format!("/{}/{}/content", self.org, self.repo);
43        let mut query = Vec::new();
44        if let Some(p) = path {
45            query.push(("path", p));
46        }
47        if let Some(r) = ref_ {
48            query.push(("ref", r));
49        }
50        self.client
51            .request(Method::GET, &url_path, &query, None::<&()>)
52            .await
53    }
54}