summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCyborus <cyborus@cyborus.xyz>2023-12-15 17:44:21 +0100
committerCyborus <cyborus@cyborus.xyz>2023-12-15 17:44:21 +0100
commit2e50a93f1e395b2b7d4171be48426efe16214ba8 (patch)
tree94ea4ea83b8df979e56ce5a09cab1acced3065c5
parentMerge pull request 'add `admin` methods' (#27) from admin into main (diff)
downloadforgejo-api-2e50a93f1e395b2b7d4171be48426efe16214ba8.tar.xz
forgejo-api-2e50a93f1e395b2b7d4171be48426efe16214ba8.zip
add release download methods
-rw-r--r--Cargo.lock1
-rw-r--r--Cargo.toml1
-rw-r--r--src/lib.rs18
-rw-r--r--src/repository.rs28
4 files changed, 48 insertions, 0 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 61464a5..ba933cb 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -169,6 +169,7 @@ checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
name = "forgejo-api"
version = "0.1.0"
dependencies = [
+ "bytes",
"eyre",
"reqwest",
"serde",
diff --git a/Cargo.toml b/Cargo.toml
index eb21559..83ad83c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -14,6 +14,7 @@ url = { version = "2.4.0", features = ["serde"] }
serde = { version = "1.0.168", features = ["derive"] }
time = { version = "0.3.22", features = ["parsing", "serde", "formatting"] }
serde_json = "1.0.108"
+bytes = "1.5.0"
[dev-dependencies]
eyre = "0.6.9"
diff --git a/src/lib.rs b/src/lib.rs
index 21b0a45..4743e91 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -180,6 +180,24 @@ impl Forgejo {
status => Err(ForgejoError::UnexpectedStatusCode(status)),
}
}
+
+ /// Like `execute`, but returns a `String`.
+ async fn execute_opt_raw(&self, request: Request) -> Result<Option<bytes::Bytes>, ForgejoError> {
+ let response = self.client.execute(request).await?;
+ match response.status() {
+ status if status.is_success() => Ok(Some(response.bytes().await?)),
+ StatusCode::NOT_FOUND => Ok(None),
+ status if status.is_client_error() => Err(ForgejoError::ApiError(
+ status,
+ response
+ .json::<ErrorMessage>()
+ .await?
+ .message
+ .unwrap_or_else(|| String::from("[no message]")),
+ )),
+ status => Err(ForgejoError::UnexpectedStatusCode(status)),
+ }
+ }
/// Like `execute`, but returns a `String`.
async fn execute_str(&self, request: Request) -> Result<String, ForgejoError> {
diff --git a/src/repository.rs b/src/repository.rs
index 966b5e3..992f78a 100644
--- a/src/repository.rs
+++ b/src/repository.rs
@@ -207,6 +207,34 @@ impl Forgejo {
.await
}
+ pub async fn download_release_zip(&self, owner: &str, repo: &str, release: u64) -> Result<Option<bytes::Bytes>, ForgejoError> {
+ let release = self.get_release(owner, repo, release).await;
+ let release = match release {
+ Ok(Some(release)) => release,
+ Ok(None) => return Ok(None),
+ Err(e) => return Err(e),
+ };
+ let request = self.client.get(release.zipball_url).build()?;
+ self.execute_opt_raw(request).await
+ }
+
+ pub async fn download_release_tarball(&self, owner: &str, repo: &str, release: u64) -> Result<Option<bytes::Bytes>, ForgejoError> {
+ let release = self.get_release(owner, repo, release).await;
+ let release = match release {
+ Ok(Some(release)) => release,
+ Ok(None) => return Ok(None),
+ Err(e) => return Err(e),
+ };
+ let request = self.client.get(release.tarball_url).build()?;
+ self.execute_opt_raw(request).await
+ }
+
+ pub async fn download_release_attachment(&self, owner: &str, repo: &str, release: u64, attach: u64) -> Result<Option<bytes::Bytes>, ForgejoError> {
+ let release = self.get_release_attachment(owner, repo, release, attach).await?;
+ let request = self.client.get(release.browser_download_url).build()?;
+ self.execute_opt_raw(request).await
+ }
+
pub async fn get_tags(
&self,
owner: &str,