Skip to content
This repository was archived by the owner on Jul 6, 2024. It is now read-only.

Commit 3827591

Browse files
committed
feat: Add server ping check
1 parent 7d667c2 commit 3827591

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "proton-api-rs"
33
authors = ["Leander Beernaert <lbb-dev@pm.me>"]
4-
version = "0.3.0"
4+
version = "0.4.0"
55
edition = "2021"
66
license = "AGPL-3.0-only"
77
description = "Unofficial implemention of proton REST API in rust"

examples/user_id.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ async fn main() {
77
let user_password = std::env::var("PAPI_USER_PASSWORD").unwrap();
88
let app_version = std::env::var("PAPI_APP_VERSION").unwrap();
99

10-
let client = match ClientBuilder::new()
11-
.app_version(&app_version)
12-
.login(&user_email, &user_password)
13-
.await
14-
.unwrap()
15-
{
10+
let builder = ClientBuilder::new().app_version(&app_version);
11+
12+
builder.ping().await.unwrap();
13+
14+
let client = match builder.login(&user_email, &user_password).await.unwrap() {
1615
ClientLoginState::Authenticated(c) => c,
1716

1817
ClientLoginState::AwaitingTotp(mut t) => {

src/client/client_builder.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::client::types::{
55
use crate::client::{HttpClientBuilder, X_PM_UID_HEADER};
66
use crate::domain::{SecretString, UserUid};
77
use crate::{impl_error_conversion, Client, RequestError};
8+
use anyhow::anyhow;
89
use go_srp::SRPAuth;
910
use secrecy::ExposeSecret;
1011
use std::time::Duration;
@@ -214,6 +215,17 @@ impl ClientBuilder {
214215

215216
Ok(Client { http_client, user })
216217
}
218+
219+
/// Check connectivity
220+
pub async fn ping(&self) -> Result<(), RequestError> {
221+
let http_client = self.0.clone().build()?;
222+
let r = http_client.get("tests/ping").execute().await?;
223+
if r.status_code() != 200 {
224+
return Err(RequestError::Other(anyhow!("Service not pingable")));
225+
}
226+
227+
Ok(())
228+
}
217229
}
218230

219231
#[derive(Debug)]

0 commit comments

Comments
 (0)