Compare commits

..

No commits in common. "be326f52ca91141f14b51c1d95c364bcd3018f90" and "0c88ac87d91887d8eee4bc7c57882922cd522f8e" have entirely different histories.

4 changed files with 56 additions and 67 deletions

1
Cargo.lock generated
View file

@ -70,6 +70,7 @@ dependencies = [
"log",
"reqwest",
"serde",
"serde_json",
"thiserror",
"tokio",
"url",

View file

@ -13,6 +13,7 @@ bytes = "1.10.1"
log = "0.4.27"
reqwest = { version = "0.12.15", features = ["json"] }
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
thiserror = "2.0.12"
tokio = "1.45.0"
url = "2.5.4"

View file

@ -108,11 +108,7 @@ impl<'a> Storage {
/// Ok(())
/// }
/// ```
pub fn init<T: AsRef<str>>(
&mut self,
endpoint: Endpoint,
storage_zone: T,
) -> Result<(), Error> {
pub fn init<T: AsRef<str>>(&mut self, endpoint: Endpoint, storage_zone: T) -> Result<(), Error> {
let endpoint: Url = endpoint.try_into()?;
let storage_zone = String::from("/") + storage_zone.as_ref() + "/";
@ -141,18 +137,16 @@ impl<'a> Storage {
/// }
/// ```
pub async fn upload<T: AsRef<str>>(&self, path: T, file: Bytes) -> Result<(), Error> {
let response = self
.reqwest
.put(self.url.join(path.as_ref())?)
let response = self.reqwest.put(self.url.join(path.as_ref())?)
.header("Content-Type", "application/octet-stream")
.body(file)
.send()
.await?;
if response.status().as_u16() == 401 {
return Err(Error::Authentication(response.text().await?));
return Err(Error::Authentication(String::from(response.text().await?)))
} else if response.status().as_u16() == 400 {
return Err(Error::BadRequest(response.text().await?));
return Err(Error::BadRequest(String::from(response.text().await?)))
}
Ok(())
@ -181,17 +175,15 @@ impl<'a> Storage {
/// }
/// ```
pub async fn download<T: AsRef<str>>(&self, path: T) -> Result<Bytes, Error> {
let response = self
.reqwest
.get(self.url.join(path.as_ref())?)
let response = self.reqwest.get(self.url.join(path.as_ref())?)
.header("accept", "*/*")
.send()
.await?;
if response.status().as_u16() == 401 {
return Err(Error::Authentication(response.text().await?));
return Err(Error::Authentication(String::from(response.text().await?)))
} else if response.status().as_u16() == 404 {
return Err(Error::NotFound(response.text().await?));
return Err(Error::NotFound(String::from(response.text().await?)))
}
Ok(response.bytes().await?)
@ -215,16 +207,14 @@ impl<'a> Storage {
/// }
/// ```
pub async fn delete<T: AsRef<str>>(&self, path: T) -> Result<(), Error> {
let response = self
.reqwest
.delete(self.url.join(path.as_ref())?)
let response = self.reqwest.delete(self.url.join(path.as_ref())?)
.send()
.await?;
if response.status().as_u16() == 401 {
return Err(Error::Authentication(response.text().await?));
return Err(Error::Authentication(String::from(response.text().await?)))
} else if response.status().as_u16() == 400 {
return Err(Error::BadRequest(response.text().await?));
return Err(Error::BadRequest(String::from(response.text().await?)))
}
Ok(())
@ -250,16 +240,14 @@ impl<'a> Storage {
/// }
/// ```
pub async fn list<T: AsRef<str>>(&self, path: T) -> Result<Vec<ListFile>, Error> {
let response = self
.reqwest
.get(self.url.join(path.as_ref())?)
let response = self.reqwest.get(self.url.join(path.as_ref())?)
.send()
.await?;
if response.status().as_u16() == 401 {
return Err(Error::Authentication(response.text().await?));
return Err(Error::Authentication(String::from(response.text().await?)))
} else if response.status().as_u16() == 400 {
return Err(Error::BadRequest(response.text().await?));
return Err(Error::BadRequest(String::from(response.text().await?)))
}
Ok(response.json().await?)

View file

@ -19,21 +19,18 @@
//! ```
#![deny(missing_docs)]
use error::Error;
use reqwest::{
Client as RClient,
header::{HeaderMap, HeaderValue},
};
use std::sync::Arc;
use reqwest::{header::{HeaderMap, HeaderValue}, Client as RClient};
use error::Error;
use url::Url;
pub mod edge_storage;
pub mod error;
pub mod edge_storage;
/// API Client for bunny
pub struct Client {
/// Used to interact with the Edge Storage API
pub storage: edge_storage::Storage,
pub storage: edge_storage::Storage
}
impl Client {
@ -53,7 +50,9 @@ impl Client {
let mut headers = HeaderMap::new();
headers.append("AccessKey", HeaderValue::from_str(api_key.as_ref())?);
let reqwest = Arc::new(RClient::builder().default_headers(headers).build()?);
let reqwest = Arc::new(RClient::builder()
.default_headers(headers)
.build()?);
Ok(Self {
storage: edge_storage::Storage {