Compare commits
No commits in common. "be326f52ca91141f14b51c1d95c364bcd3018f90" and "0c88ac87d91887d8eee4bc7c57882922cd522f8e" have entirely different histories.
be326f52ca
...
0c88ac87d9
4 changed files with 56 additions and 67 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -70,6 +70,7 @@ dependencies = [
|
||||||
"log",
|
"log",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"tokio",
|
"tokio",
|
||||||
"url",
|
"url",
|
||||||
|
|
|
@ -13,6 +13,7 @@ bytes = "1.10.1"
|
||||||
log = "0.4.27"
|
log = "0.4.27"
|
||||||
reqwest = { version = "0.12.15", features = ["json"] }
|
reqwest = { version = "0.12.15", features = ["json"] }
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
|
serde_json = "1.0.140"
|
||||||
thiserror = "2.0.12"
|
thiserror = "2.0.12"
|
||||||
tokio = "1.45.0"
|
tokio = "1.45.0"
|
||||||
url = "2.5.4"
|
url = "2.5.4"
|
||||||
|
|
|
@ -95,24 +95,20 @@ pub struct Storage {
|
||||||
|
|
||||||
impl<'a> Storage {
|
impl<'a> Storage {
|
||||||
/// Sets endpoint and storage zone used by Edge Storage API
|
/// Sets endpoint and storage zone used by Edge Storage API
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use bunny_api_tokio::{Client, error::Error, edge_storage::Endpoint};
|
/// use bunny_api_tokio::{Client, error::Error, edge_storage::Endpoint};
|
||||||
///
|
///
|
||||||
/// #[tokio::main]
|
/// #[tokio::main]
|
||||||
/// async fn main() -> Result<(), Error> {
|
/// async fn main() -> Result<(), Error> {
|
||||||
/// let mut client = Client::new("api_key").await?;
|
/// let mut client = Client::new("api_key").await?;
|
||||||
///
|
///
|
||||||
/// client.storage.init(Endpoint::Frankfurt, "MyStorageZone");
|
/// client.storage.init(Endpoint::Frankfurt, "MyStorageZone");
|
||||||
///
|
///
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn init<T: AsRef<str>>(
|
pub fn init<T: AsRef<str>>(&mut self, endpoint: Endpoint, storage_zone: T) -> Result<(), Error> {
|
||||||
&mut self,
|
|
||||||
endpoint: Endpoint,
|
|
||||||
storage_zone: T,
|
|
||||||
) -> Result<(), Error> {
|
|
||||||
let endpoint: Url = endpoint.try_into()?;
|
let endpoint: Url = endpoint.try_into()?;
|
||||||
let storage_zone = String::from("/") + storage_zone.as_ref() + "/";
|
let storage_zone = String::from("/") + storage_zone.as_ref() + "/";
|
||||||
|
|
||||||
|
@ -121,126 +117,120 @@ impl<'a> Storage {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Uploads a file to the Storage Zone
|
/// Uploads a file to the Storage Zone
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use bunny_api_tokio::{Client, error::Error, edge_storage::Endpoint};
|
/// use bunny_api_tokio::{Client, error::Error, edge_storage::Endpoint};
|
||||||
/// use tokio::fs;
|
/// use tokio::fs;
|
||||||
///
|
///
|
||||||
/// #[tokio::main]
|
/// #[tokio::main]
|
||||||
/// async fn main() -> Result<(), Error> {
|
/// async fn main() -> Result<(), Error> {
|
||||||
/// let mut client = Client::new("api_key").await?;
|
/// let mut client = Client::new("api_key").await?;
|
||||||
///
|
///
|
||||||
/// client.storage.init(Endpoint::Frankfurt, "MyStorageZone");
|
/// client.storage.init(Endpoint::Frankfurt, "MyStorageZone");
|
||||||
///
|
///
|
||||||
/// let file_bytes = fs::read("path/to/file.png").await?;
|
/// let file_bytes = fs::read("path/to/file.png").await?;
|
||||||
///
|
///
|
||||||
/// // Will put a file in STORAGE_ZONE/images/file.png
|
/// // Will put a file in STORAGE_ZONE/images/file.png
|
||||||
/// client.storage.upload("/images/file.png", file_bytes).await?;
|
/// client.storage.upload("/images/file.png", file_bytes).await?;
|
||||||
///
|
///
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn upload<T: AsRef<str>>(&self, path: T, file: Bytes) -> Result<(), Error> {
|
pub async fn upload<T: AsRef<str>>(&self, path: T, file: Bytes) -> Result<(), Error> {
|
||||||
let response = self
|
let response = self.reqwest.put(self.url.join(path.as_ref())?)
|
||||||
.reqwest
|
|
||||||
.put(self.url.join(path.as_ref())?)
|
|
||||||
.header("Content-Type", "application/octet-stream")
|
.header("Content-Type", "application/octet-stream")
|
||||||
.body(file)
|
.body(file)
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
if response.status().as_u16() == 401 {
|
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 {
|
} else if response.status().as_u16() == 400 {
|
||||||
return Err(Error::BadRequest(response.text().await?));
|
return Err(Error::BadRequest(String::from(response.text().await?)))
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Downloads a file from the Storage Zone
|
/// Downloads a file from the Storage Zone
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use bunny_api_tokio::{Client, error::Error, edge_storage::Endpoint};
|
/// use bunny_api_tokio::{Client, error::Error, edge_storage::Endpoint};
|
||||||
/// use tokio::fs;
|
/// use tokio::fs;
|
||||||
/// use tokio::io::AsyncWriteExt;
|
/// use tokio::io::AsyncWriteExt;
|
||||||
///
|
///
|
||||||
/// #[tokio::main]
|
/// #[tokio::main]
|
||||||
/// async fn main() -> Result<(), Error> {
|
/// async fn main() -> Result<(), Error> {
|
||||||
/// let mut client = Client::new("api_key").await?;
|
/// let mut client = Client::new("api_key").await?;
|
||||||
///
|
///
|
||||||
/// client.storage.init(Endpoint::Frankfurt, "MyStorageZone");
|
/// client.storage.init(Endpoint::Frankfurt, "MyStorageZone");
|
||||||
///
|
///
|
||||||
/// // Will download the file STORAGE_ZONE/images/file.png
|
/// // Will download the file STORAGE_ZONE/images/file.png
|
||||||
/// let contents = client.storage.download("/images/file.png").await?;
|
/// let contents = client.storage.download("/images/file.png").await?;
|
||||||
///
|
///
|
||||||
/// let mut file = fs::File::create("file.png").await?;
|
/// let mut file = fs::File::create("file.png").await?;
|
||||||
/// file.write_all(contents).await?;
|
/// file.write_all(contents).await?;
|
||||||
///
|
///
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn download<T: AsRef<str>>(&self, path: T) -> Result<Bytes, Error> {
|
pub async fn download<T: AsRef<str>>(&self, path: T) -> Result<Bytes, Error> {
|
||||||
let response = self
|
let response = self.reqwest.get(self.url.join(path.as_ref())?)
|
||||||
.reqwest
|
|
||||||
.get(self.url.join(path.as_ref())?)
|
|
||||||
.header("accept", "*/*")
|
.header("accept", "*/*")
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
if response.status().as_u16() == 401 {
|
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 {
|
} 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?)
|
Ok(response.bytes().await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deletes a file from the Storage Zone
|
/// Deletes a file from the Storage Zone
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use bunny_api_tokio::{Client, error::Error, edge_storage::Endpoint};
|
/// use bunny_api_tokio::{Client, error::Error, edge_storage::Endpoint};
|
||||||
///
|
///
|
||||||
/// #[tokio::main]
|
/// #[tokio::main]
|
||||||
/// async fn main() -> Result<(), Error> {
|
/// async fn main() -> Result<(), Error> {
|
||||||
/// let mut client = Client::new("api_key").await?;
|
/// let mut client = Client::new("api_key").await?;
|
||||||
///
|
///
|
||||||
/// client.storage.init(Endpoint::Frankfurt, "MyStorageZone");
|
/// client.storage.init(Endpoint::Frankfurt, "MyStorageZone");
|
||||||
///
|
///
|
||||||
/// // Will delete the file STORAGE_ZONE/images/file.png
|
/// // Will delete the file STORAGE_ZONE/images/file.png
|
||||||
/// client.storage.delete("/images/file.png").await?;
|
/// client.storage.delete("/images/file.png").await?;
|
||||||
///
|
///
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn delete<T: AsRef<str>>(&self, path: T) -> Result<(), Error> {
|
pub async fn delete<T: AsRef<str>>(&self, path: T) -> Result<(), Error> {
|
||||||
let response = self
|
let response = self.reqwest.delete(self.url.join(path.as_ref())?)
|
||||||
.reqwest
|
|
||||||
.delete(self.url.join(path.as_ref())?)
|
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
if response.status().as_u16() == 401 {
|
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 {
|
} else if response.status().as_u16() == 400 {
|
||||||
return Err(Error::BadRequest(response.text().await?));
|
return Err(Error::BadRequest(String::from(response.text().await?)))
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Lists files on the Storage Zone
|
/// Lists files on the Storage Zone
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use bunny_api_tokio::{Client, error::Error, edge_storage::Endpoint};
|
/// use bunny_api_tokio::{Client, error::Error, edge_storage::Endpoint};
|
||||||
///
|
///
|
||||||
/// #[tokio::main]
|
/// #[tokio::main]
|
||||||
/// async fn main() -> Result<(), Error> {
|
/// async fn main() -> Result<(), Error> {
|
||||||
/// let mut client = Client::new("api_key").await?;
|
/// let mut client = Client::new("api_key").await?;
|
||||||
///
|
///
|
||||||
/// client.storage.init(Endpoint::Frankfurt, "MyStorageZone");
|
/// client.storage.init(Endpoint::Frankfurt, "MyStorageZone");
|
||||||
///
|
///
|
||||||
/// // Will list the files in STORAGE_ZONE/images/
|
/// // Will list the files in STORAGE_ZONE/images/
|
||||||
/// let files = client.storage.list("/images/").await?;
|
/// let files = client.storage.list("/images/").await?;
|
||||||
///
|
///
|
||||||
|
@ -250,16 +240,14 @@ impl<'a> Storage {
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn list<T: AsRef<str>>(&self, path: T) -> Result<Vec<ListFile>, Error> {
|
pub async fn list<T: AsRef<str>>(&self, path: T) -> Result<Vec<ListFile>, Error> {
|
||||||
let response = self
|
let response = self.reqwest.get(self.url.join(path.as_ref())?)
|
||||||
.reqwest
|
|
||||||
.get(self.url.join(path.as_ref())?)
|
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
if response.status().as_u16() == 401 {
|
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 {
|
} 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?)
|
Ok(response.json().await?)
|
||||||
|
|
33
src/lib.rs
33
src/lib.rs
|
@ -1,51 +1,48 @@
|
||||||
//! This library provides access to the Bunny API asynchronously using tokio, it's not fully implemented but PRs are welcome.
|
//! This library provides access to the Bunny API asynchronously using tokio, it's not fully implemented but PRs are welcome.
|
||||||
//!
|
//!
|
||||||
//! # Getting started
|
//! # Getting started
|
||||||
//! 1. add package to your project using cargo
|
//! 1. add package to your project using cargo
|
||||||
//!
|
//!
|
||||||
//! `$ cargo add bunny-api-tokio`
|
//! `$ cargo add bunny-api-tokio`
|
||||||
//!
|
//!
|
||||||
//! 2. Start coding
|
//! 2. Start coding
|
||||||
//!
|
//!
|
||||||
//! ```
|
//! ```
|
||||||
//! use bunny_api_tokio::{Client, error::Error};
|
//! use bunny_api_tokio::{Client, error::Error};
|
||||||
//!
|
//!
|
||||||
//! #[tokio::main]
|
//! #[tokio::main]
|
||||||
//! async fn main() -> Result<(), Error> {
|
//! async fn main() -> Result<(), Error> {
|
||||||
//! let mut client = Client::new("api_key").await?;
|
//! let mut client = Client::new("api_key").await?;
|
||||||
//!
|
//!
|
||||||
//! Ok(())
|
//! Ok(())
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
#![deny(missing_docs)]
|
#![deny(missing_docs)]
|
||||||
|
|
||||||
use error::Error;
|
|
||||||
use reqwest::{
|
|
||||||
Client as RClient,
|
|
||||||
header::{HeaderMap, HeaderValue},
|
|
||||||
};
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use reqwest::{header::{HeaderMap, HeaderValue}, Client as RClient};
|
||||||
|
use error::Error;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
pub mod edge_storage;
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
|
pub mod edge_storage;
|
||||||
|
|
||||||
/// API Client for bunny
|
/// API Client for bunny
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
/// Used to interact with the Edge Storage API
|
/// Used to interact with the Edge Storage API
|
||||||
pub storage: edge_storage::Storage,
|
pub storage: edge_storage::Storage
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
/// Creates a new Client using the supplied `api_key`
|
/// Creates a new Client using the supplied `api_key`
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use bunny_api_tokio::{Client, error::Error};
|
/// use bunny_api_tokio::{Client, error::Error};
|
||||||
///
|
///
|
||||||
/// #[tokio::main]
|
/// #[tokio::main]
|
||||||
/// async fn main() -> Result<(), Error> {
|
/// async fn main() -> Result<(), Error> {
|
||||||
/// let mut client = Client::new("api_key").await?;
|
/// let mut client = Client::new("api_key").await?;
|
||||||
///
|
///
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -53,7 +50,9 @@ impl Client {
|
||||||
let mut headers = HeaderMap::new();
|
let mut headers = HeaderMap::new();
|
||||||
headers.append("AccessKey", HeaderValue::from_str(api_key.as_ref())?);
|
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 {
|
Ok(Self {
|
||||||
storage: edge_storage::Storage {
|
storage: edge_storage::Storage {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue