Basic gitea client.

So far it only supports listing the contents of a repo.
This commit is contained in:
Tom Alexander
2024-07-25 21:01:44 -04:00
parent 3e3acbab7d
commit 15e1f4dbce
4 changed files with 501 additions and 15 deletions

68
src/gitea_client/mod.rs Normal file
View File

@@ -0,0 +1,68 @@
use serde::Deserialize;
use tracing::debug;
#[derive(Debug, Clone)]
pub(crate) struct GiteaClient {
api_root: String,
token: String,
http_client: reqwest::Client,
}
impl GiteaClient {
pub(crate) fn new<R: Into<String>, T: Into<String>>(api_root: R, token: T) -> GiteaClient {
GiteaClient {
api_root: api_root.into(),
token: token.into(),
http_client: reqwest::Client::new(),
}
}
pub(crate) async fn get_tree<O: AsRef<str>, R: AsRef<str>, C: AsRef<str>>(
&self,
owner: O,
repo: R,
commit: C,
) -> Result<(), Box<dyn std::error::Error>> {
let url = format!(
"{api_root}/v1/repos/{owner}/{repo}/git/trees/{commit}?recursive=true&per_page=10",
api_root = self.api_root,
owner = owner.as_ref(),
repo = repo.as_ref(),
commit = commit.as_ref()
);
let response = self
.http_client
.get(url)
.header("Authorization", format!("token {}", self.token))
.send()
.await?;
let response = response.error_for_status()?;
let body = response.text().await?;
debug!("Response: {}", body);
let parsed_body: ResponseGetTree = serde_json::from_str(body.as_str())?;
println!("Response: {:#?}", parsed_body);
Ok(())
}
}
/// A single API response for GetTree containing only one page.
#[derive(Debug, Deserialize)]
struct ResponseGetTree {
sha: String,
url: String,
tree: Vec<ResponseObjectReference>,
truncated: bool,
page: u64,
total_count: u64,
}
#[derive(Debug, Deserialize)]
struct ResponseObjectReference {
path: String,
mode: String,
#[serde(rename = "type")]
object_type: String,
size: u64,
sha: String,
url: String,
}