gitea_sdk/api/repos/contents/
get.rs

1use build_it::Builder;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4
5use crate::{model::repos::Entry, Result};
6
7#[derive(Debug, Serialize, Deserialize, Builder)]
8#[build_it(into)]
9pub struct GetContentsRepoBuilder {
10    /// The owner of the repository.
11    #[skip]
12    #[serde(skip)]
13    owner: String,
14    /// The name of the repository.
15    #[skip]
16    #[serde(skip)]
17    repo: String,
18    /// The entry filepath can be a directory or file.
19    #[skip]
20    #[serde(skip)]
21    filepath: String,
22    /// The name of the commit/branch/tag. Default the repository’s default branch (usually master)
23    #[serde(skip_serializing_if = "Option::is_none")]
24    #[build_it(rename = "refs")]
25    r#ref: Option<String>,
26}
27
28impl GetContentsRepoBuilder {
29    pub fn new(owner: impl ToString, repo: impl ToString, filepath: impl ToString) -> Self {
30        Self {
31            owner: owner.to_string(),
32            repo: repo.to_string(),
33            filepath: filepath.to_string(),
34            r#ref: None,
35        }
36    }
37
38    /// Send the request to fetch given repository's file path contents.
39    pub async fn send(&self, client: &crate::Client) -> Result<Vec<Entry>> {
40        let owner = &self.owner;
41        let repo = &self.repo;
42        let filepath = &self.filepath;
43        let req = client
44            .get(format!("repos/{owner}/{repo}/contents/{filepath}"))
45            .query(self)
46            .build()?;
47        let res = client.make_request(req).await?;
48
49        let res_body: Value = res.json().await?;
50
51        // Response can either be Entry or Entry[], put in Vec<> to handle both cases
52        let entries = serde_json::from_value::<Vec<Entry>>(res_body.clone()).or_else(|_| {
53            serde_json::from_value(res_body.clone()).map(|single_entry| vec![single_entry])
54        })?;
55
56        Ok(entries)
57    }
58}