From f9c48db17578421f8caa44d732541baf2c842133 Mon Sep 17 00:00:00 2001 From: Radical Date: Tue, 20 May 2025 02:01:32 +0200 Subject: [PATCH 1/3] build: remove unused serde_json dependency --- Cargo.lock | 1 - Cargo.toml | 1 - 2 files changed, 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1f914d7..3a03be6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -70,7 +70,6 @@ dependencies = [ "log", "reqwest", "serde", - "serde_json", "thiserror", "tokio", "url", diff --git a/Cargo.toml b/Cargo.toml index d31cf20..86d66a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,6 @@ 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" From f3f3478ae0beeeffba033a221bf780c532426ef1 Mon Sep 17 00:00:00 2001 From: Radical Date: Tue, 20 May 2025 02:01:49 +0200 Subject: [PATCH 2/3] fix: remove unneeded string conversions --- src/edge_storage.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/edge_storage.rs b/src/edge_storage.rs index 7b6d9bc..d9bbe32 100644 --- a/src/edge_storage.rs +++ b/src/edge_storage.rs @@ -144,9 +144,9 @@ impl<'a> Storage { .await?; if response.status().as_u16() == 401 { - return Err(Error::Authentication(String::from(response.text().await?))) + return Err(Error::Authentication(response.text().await?)) } else if response.status().as_u16() == 400 { - return Err(Error::BadRequest(String::from(response.text().await?))) + return Err(Error::BadRequest(response.text().await?)) } Ok(()) @@ -181,9 +181,9 @@ impl<'a> Storage { .await?; if response.status().as_u16() == 401 { - return Err(Error::Authentication(String::from(response.text().await?))) + return Err(Error::Authentication(response.text().await?)) } else if response.status().as_u16() == 404 { - return Err(Error::NotFound(String::from(response.text().await?))) + return Err(Error::NotFound(response.text().await?)) } Ok(response.bytes().await?) @@ -212,9 +212,9 @@ impl<'a> Storage { .await?; if response.status().as_u16() == 401 { - return Err(Error::Authentication(String::from(response.text().await?))) + return Err(Error::Authentication(response.text().await?)) } else if response.status().as_u16() == 400 { - return Err(Error::BadRequest(String::from(response.text().await?))) + return Err(Error::BadRequest(response.text().await?)) } Ok(()) @@ -245,9 +245,9 @@ impl<'a> Storage { .await?; if response.status().as_u16() == 401 { - return Err(Error::Authentication(String::from(response.text().await?))) + return Err(Error::Authentication(response.text().await?)) } else if response.status().as_u16() == 400 { - return Err(Error::BadRequest(String::from(response.text().await?))) + return Err(Error::BadRequest(response.text().await?)) } Ok(response.json().await?) From be326f52ca91141f14b51c1d95c364bcd3018f90 Mon Sep 17 00:00:00 2001 From: Radical Date: Tue, 20 May 2025 02:02:12 +0200 Subject: [PATCH 3/3] style: format code formats code according to `cargo fmt` standards --- src/edge_storage.rs | 88 +++++++++++++++++++++++++-------------------- src/lib.rs | 33 ++++++++--------- 2 files changed, 67 insertions(+), 54 deletions(-) diff --git a/src/edge_storage.rs b/src/edge_storage.rs index d9bbe32..495eca6 100644 --- a/src/edge_storage.rs +++ b/src/edge_storage.rs @@ -95,20 +95,24 @@ pub struct Storage { impl<'a> Storage { /// Sets endpoint and storage zone used by Edge Storage API - /// + /// /// ``` /// use bunny_api_tokio::{Client, error::Error, edge_storage::Endpoint}; - /// + /// /// #[tokio::main] /// async fn main() -> Result<(), Error> { /// let mut client = Client::new("api_key").await?; - /// + /// /// client.storage.init(Endpoint::Frankfurt, "MyStorageZone"); - /// + /// /// Ok(()) /// } /// ``` - pub fn init>(&mut self, endpoint: Endpoint, storage_zone: T) -> Result<(), Error> { + pub fn init>( + &mut self, + endpoint: Endpoint, + storage_zone: T, + ) -> Result<(), Error> { let endpoint: Url = endpoint.try_into()?; let storage_zone = String::from("/") + storage_zone.as_ref() + "/"; @@ -117,120 +121,126 @@ impl<'a> Storage { } /// Uploads a file to the Storage Zone - /// + /// /// ``` /// use bunny_api_tokio::{Client, error::Error, edge_storage::Endpoint}; /// use tokio::fs; - /// + /// /// #[tokio::main] /// async fn main() -> Result<(), Error> { /// let mut client = Client::new("api_key").await?; - /// + /// /// client.storage.init(Endpoint::Frankfurt, "MyStorageZone"); - /// + /// /// let file_bytes = fs::read("path/to/file.png").await?; - /// + /// /// // Will put a file in STORAGE_ZONE/images/file.png /// client.storage.upload("/images/file.png", file_bytes).await?; - /// + /// /// Ok(()) /// } /// ``` pub async fn upload>(&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(response.text().await?)); } else if response.status().as_u16() == 400 { - return Err(Error::BadRequest(response.text().await?)) + return Err(Error::BadRequest(response.text().await?)); } Ok(()) } /// Downloads a file from the Storage Zone - /// + /// /// ``` /// use bunny_api_tokio::{Client, error::Error, edge_storage::Endpoint}; /// use tokio::fs; /// use tokio::io::AsyncWriteExt; - /// + /// /// #[tokio::main] /// async fn main() -> Result<(), Error> { /// let mut client = Client::new("api_key").await?; - /// + /// /// client.storage.init(Endpoint::Frankfurt, "MyStorageZone"); - /// + /// /// // Will download the file STORAGE_ZONE/images/file.png /// let contents = client.storage.download("/images/file.png").await?; - /// + /// /// let mut file = fs::File::create("file.png").await?; /// file.write_all(contents).await?; - /// + /// /// Ok(()) /// } /// ``` pub async fn download>(&self, path: T) -> Result { - 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(response.text().await?)); } else if response.status().as_u16() == 404 { - return Err(Error::NotFound(response.text().await?)) + return Err(Error::NotFound(response.text().await?)); } Ok(response.bytes().await?) } /// Deletes a file from the Storage Zone - /// + /// /// ``` /// use bunny_api_tokio::{Client, error::Error, edge_storage::Endpoint}; - /// + /// /// #[tokio::main] /// async fn main() -> Result<(), Error> { /// let mut client = Client::new("api_key").await?; - /// + /// /// client.storage.init(Endpoint::Frankfurt, "MyStorageZone"); - /// + /// /// // Will delete the file STORAGE_ZONE/images/file.png /// client.storage.delete("/images/file.png").await?; - /// + /// /// Ok(()) /// } /// ``` pub async fn delete>(&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(response.text().await?)); } else if response.status().as_u16() == 400 { - return Err(Error::BadRequest(response.text().await?)) + return Err(Error::BadRequest(response.text().await?)); } Ok(()) } /// Lists files on the Storage Zone - /// + /// /// ``` /// use bunny_api_tokio::{Client, error::Error, edge_storage::Endpoint}; - /// + /// /// #[tokio::main] /// async fn main() -> Result<(), Error> { /// let mut client = Client::new("api_key").await?; - /// + /// /// client.storage.init(Endpoint::Frankfurt, "MyStorageZone"); - /// + /// /// // Will list the files in STORAGE_ZONE/images/ /// let files = client.storage.list("/images/").await?; /// @@ -240,14 +250,16 @@ impl<'a> Storage { /// } /// ``` pub async fn list>(&self, path: T) -> Result, 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(response.text().await?)); } else if response.status().as_u16() == 400 { - return Err(Error::BadRequest(response.text().await?)) + return Err(Error::BadRequest(response.text().await?)); } Ok(response.json().await?) diff --git a/src/lib.rs b/src/lib.rs index d6f6708..e545ba9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,48 +1,51 @@ //! This library provides access to the Bunny API asynchronously using tokio, it's not fully implemented but PRs are welcome. -//! +//! //! # Getting started //! 1. add package to your project using cargo -//! +//! //! `$ cargo add bunny-api-tokio` -//! +//! //! 2. Start coding -//! +//! //! ``` //! use bunny_api_tokio::{Client, error::Error}; -//! +//! //! #[tokio::main] //! async fn main() -> Result<(), Error> { //! let mut client = Client::new("api_key").await?; -//! +//! //! Ok(()) //! } //! ``` #![deny(missing_docs)] -use std::sync::Arc; -use reqwest::{header::{HeaderMap, HeaderValue}, Client as RClient}; use error::Error; +use reqwest::{ + Client as RClient, + header::{HeaderMap, HeaderValue}, +}; +use std::sync::Arc; use url::Url; -pub mod error; pub mod edge_storage; +pub mod error; /// 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 { /// Creates a new Client using the supplied `api_key` - /// + /// /// ``` /// use bunny_api_tokio::{Client, error::Error}; - /// + /// /// #[tokio::main] /// async fn main() -> Result<(), Error> { /// let mut client = Client::new("api_key").await?; - /// + /// /// Ok(()) /// } /// ``` @@ -50,9 +53,7 @@ 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 {