Skip to content

Commit

Permalink
Add AsyncRobot::new_with_default_client(username, password) constru…
Browse files Browse the repository at this point in the history
…ctor

Fixes #10
  • Loading branch information
MathiasPius committed May 10, 2024
1 parent 35a03f7 commit 25f5e9d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
12 changes: 4 additions & 8 deletions src/api/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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> {
Expand Down
22 changes: 22 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpConnector> = 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<Response: DeserializeOwned + Send + 'static>(
Expand Down

0 comments on commit 25f5e9d

Please sign in to comment.