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

Commit c3d88d4

Browse files
committed
refactor: Requests and API
Each request should now implement one of the http RequestWithBody or RequestNoBody traits. This should transparently enable the use the sync or async execution. The API has also been refactored so that the http client is no longer a part of the session object.
1 parent c59a787 commit c3d88d4

24 files changed

+1253
-803
lines changed

Cargo.toml

Lines changed: 18 additions & 4 deletions
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.6.0"
4+
version = "0.7.0"
55
edition = "2021"
66
license = "AGPL-3.0-only"
77
description = "Unofficial implemention of proton REST API in rust"
@@ -16,23 +16,37 @@ categories = ["api-bindings"]
1616
[dependencies]
1717
go-srp = {path = "go-srp", version = "0.1.3"}
1818
thiserror = "1"
19-
serde = "1"
19+
serde = {version ="1", features=["derive"]}
2020
serde_repr = "0.1"
2121
serde_json = "1"
2222
base64 = "0.21"
2323
secrecy = "0.8"
2424
anyhow = "1.0"
2525
bytes = "1.4"
2626
log = "0.4"
27+
ureq = {version="2.6", optional=true}
28+
29+
30+
[features]
31+
default = []
32+
http-ureq = ["dep:ureq"]
33+
http-reqwest = ["dep:reqwest"]
34+
2735

2836
[dependencies.reqwest]
2937
version = "0.11"
3038
default-features = false
3139
features = ["json", "deflate", "stream", "cookies", "multipart", "rustls","rustls-tls", "socks"]
40+
optional = true
3241

33-
[target.'cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))'.dependencies]
42+
[dev-dependencies]
43+
env_logger = "0.10"
3444
tokio = {version ="1", features = ["full"]}
3545

36-
3746
[[example]]
3847
name = "user_id"
48+
required-features = ["http-reqwest"]
49+
50+
[[example]]
51+
name = "user_id_sync"
52+
required-features = ["http-ureq"]

examples/user_id.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use proton_api_rs::{tokio, ClientBuilder, ClientLoginState};
1+
use proton_api_rs::{http, ping_async};
2+
use proton_api_rs::{Session, SessionType};
3+
pub use tokio;
24
use tokio::io::{AsyncBufReadExt, AsyncWriteExt};
35

46
#[tokio::main(worker_threads = 1)]
@@ -7,18 +9,24 @@ async fn main() {
79
let user_password = std::env::var("PAPI_USER_PASSWORD").unwrap();
810
let app_version = std::env::var("PAPI_APP_VERSION").unwrap();
911

10-
let builder = ClientBuilder::new().app_version(&app_version);
12+
let client = http::ClientBuilder::new()
13+
.app_version(&app_version)
14+
.build::<http::reqwest_client::ReqwestClient>()
15+
.unwrap();
1116

12-
builder.ping().await.unwrap();
17+
ping_async(&client).await.unwrap();
1318

14-
let client = match builder.login(&user_email, &user_password).await.unwrap() {
15-
ClientLoginState::Authenticated(c) => c,
19+
let session = match Session::login_async(&client, &user_email, &user_password)
20+
.await
21+
.unwrap()
22+
{
23+
SessionType::Authenticated(c) => c,
1624

17-
ClientLoginState::AwaitingTotp(mut t) => {
25+
SessionType::AwaitingTotp(mut t) => {
1826
let mut stdout = tokio::io::stdout();
1927
let mut line_reader = tokio::io::BufReader::new(tokio::io::stdin()).lines();
20-
let client = {
21-
let mut client = None;
28+
let session = {
29+
let mut session = None;
2230
for _ in 0..3 {
2331
stdout
2432
.write_all("Please Input TOTP:".as_bytes())
@@ -33,9 +41,9 @@ async fn main() {
3341

3442
let totp = line.trim_end_matches('\n');
3543

36-
match t.submit_totp(totp).await {
44+
match t.submit_totp_async(&client, totp).await {
3745
Ok(ac) => {
38-
client = Some(ac);
46+
session = Some(ac);
3947
break;
4048
}
4149
Err((et, e)) => {
@@ -46,19 +54,19 @@ async fn main() {
4654
}
4755
}
4856

49-
client
57+
session
5058
};
5159

52-
let Some(c) = client else {
60+
let Some(c) = session else {
5361
eprintln!("Failed to pass TOTP 2FA auth");
5462
return;
5563
};
5664
c
5765
}
5866
};
5967

60-
let user = client.get_user().await.unwrap();
68+
let user = session.get_user_async(&client).await.unwrap();
6169
println!("User ID is {}", user.id);
6270

63-
client.logout().await.unwrap();
71+
session.logout_async(&client).await.unwrap();
6472
}

examples/user_id_sync.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use proton_api_rs::clientv2::{ping, Session, SessionType};
2+
use proton_api_rs::http;
3+
use std::io::{BufRead, Write};
4+
5+
fn main() {
6+
env_logger::init();
7+
8+
let user_email = std::env::var("PAPI_USER_EMAIL").unwrap();
9+
let user_password = std::env::var("PAPI_USER_PASSWORD").unwrap();
10+
let app_version = std::env::var("PAPI_APP_VERSION").unwrap();
11+
12+
let client = http::ClientBuilder::new()
13+
.app_version(&app_version)
14+
.build::<http::ureq_client::UReqClient>()
15+
.unwrap();
16+
17+
ping(&client).unwrap();
18+
19+
let session = match Session::login(&client, &user_email, &user_password).unwrap() {
20+
SessionType::Authenticated(s) => s,
21+
SessionType::AwaitingTotp(mut t) => {
22+
let mut line_reader = std::io::BufReader::new(std::io::stdin());
23+
let session = {
24+
let mut session = None;
25+
for _ in 0..3 {
26+
std::io::stdout()
27+
.write_all("Please Input TOTP:".as_bytes())
28+
.unwrap();
29+
std::io::stdout().flush().unwrap();
30+
31+
let mut line = String::new();
32+
if let Err(e) = line_reader.read_line(&mut line) {
33+
eprintln!("Failed to read totp {e}");
34+
return;
35+
};
36+
37+
let totp = line.trim_end_matches('\n');
38+
39+
match t.submit_totp(&client, totp) {
40+
Ok(ac) => {
41+
session = Some(ac);
42+
break;
43+
}
44+
Err((et, e)) => {
45+
t = et;
46+
eprintln!("Failed to submit totp: {e}");
47+
continue;
48+
}
49+
}
50+
}
51+
52+
session
53+
};
54+
55+
let Some(c) = session else {
56+
eprintln!("Failed to pass TOTP 2FA auth");
57+
return;
58+
};
59+
c
60+
}
61+
};
62+
63+
let user = session.get_user(&client).unwrap();
64+
println!("User ID is {}", user.id);
65+
66+
session.logout(&client).unwrap();
67+
}

src/client/api.rs

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)