From 25f5e9d82392b6efd43bf95573c31741b51467bf Mon Sep 17 00:00:00 2001 From: Mathias Pius Date: Fri, 10 May 2024 14:17:12 +0200 Subject: [PATCH] Add `AsyncRobot::new_with_default_client(username, password)` constructor Fixes #10 --- Cargo.toml | 2 +- src/api/server/mod.rs | 12 ++++-------- src/client.rs | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a44cc20..e0bcc8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "hrobot" description = "Unofficial Hetzner Robot API client" keywords = ["hetzner", "robot", "api", "client", "async"] repository = "https://github.com/MathiasPius/hrobot-rs" -version = "6.0.0" +version = "6.1.0" license = "MIT" edition = "2021" diff --git a/src/api/server/mod.rs b/src/api/server/mod.rs index a9d028a..c5139d7 100644 --- a/src/api/server/mod.rs +++ b/src/api/server/mod.rs @@ -149,12 +149,12 @@ impl AsyncRobot { /// /// # Example /// ```rust,no_run - /// # use hrobot::api::server::{ServerId, Cancelled}; + /// # use hrobot::api::server::{ServerId, Cancel}; /// # use time::{Date, Month}; /// # #[tokio::main] /// # async fn main() { /// let robot = hrobot::AsyncRobot::default(); - /// robot.cancel_server(ServerId(1234567), Cancelled { + /// robot.cancel_server(ServerId(1234567), Cancel { /// date: Some(Date::from_calendar_date(2023, Month::June, 10).unwrap()), /// reason: Some("Server no longer necessary due to project ending".to_string()), /// reserved: false @@ -176,15 +176,11 @@ impl AsyncRobot { /// /// # Example /// ```rust,no_run - /// # use hrobot::api::server::{ServerId, Cancellation}; + /// # use hrobot::api::server::ServerId; /// # #[tokio::main] /// # async fn main() { /// let robot = hrobot::AsyncRobot::default(); - /// let cancellation = robot.withdraw_server_cancellation(ServerId(1234567)).await.unwrap(); - /// assert!(matches!( - /// cancellation, - /// Cancellation::Cancellable(_) - /// )); + /// robot.withdraw_server_cancellation(ServerId(1234567)).await.unwrap(); /// # } /// ``` pub async fn withdraw_server_cancellation(&self, server_number: ServerId) -> Result<(), Error> { diff --git a/src/client.rs b/src/client.rs index f614f13..c4aa2ae 100644 --- a/src/client.rs +++ b/src/client.rs @@ -116,6 +116,28 @@ mod r#async { } } + /// Construct a new [`AsyncRobot`], using the default [`hyper_util::client::legacy::Client`] + /// and the provided username and password. + /// + /// # Example + /// Construct an [`AsyncRobot`] using a given username and password + /// ```rust + /// # #[tokio::main] + /// # async fn main() { + /// let robot = hrobot::AsyncRobot::new_with_default_client("#ws+username", "p@ssw0rd"); + /// # } + /// ``` + pub fn new_with_default_client(username: &str, password: &str) -> Self { + let https: HttpsConnector = hyper_rustls::HttpsConnectorBuilder::new() + .with_webpki_roots() + .https_only() + .enable_http1() + .build(); + let client = Client::builder(TokioExecutor::new()).build(https); + + Self::new(client, username, password) + } + /// Shorthand for authenticating and sending the request. #[tracing::instrument] pub(crate) async fn go(