1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! An example to customize HTTP client for APIs.
//!
//! ```shell
//! $ cargo run --example customize_http_client --features custom_client -- --email <email> --password <password>
//! ```
use clap::Parser;
#[derive(Parser)]
struct Arguments {
#[arg(short, long)]
email: String,
#[arg(short, long)]
password: String,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
#[cfg(not(feature = "custom_client"))]
{
return Err(anyhow::anyhow!(
"Feature \"custom_client\" is not enabled.",
));
}
#[cfg(feature = "custom_client")]
{
use std::time::Duration;
use fars::ApiKey;
use fars::Client;
use fars::Config;
use fars::Email;
use fars::Password;
// Parse the command line arguments.
let arguments = Arguments::parse();
// Read API key from the environment variable.
let api_key = ApiKey::new(std::env::var("FIREBASE_API_KEY")?);
// Create a custom reqwest client with timeout.
let client = fars::reqwest::ClientBuilder::new()
.timeout(Duration::from_secs(60))
.connect_timeout(Duration::from_secs(10))
.build()?;
// Customize HTTP client.
let client = Client::custom(client);
// Create a config.
let config = Config::custom(api_key, client);
// Get a session by signing in with email and password.
let session = config
.sign_in_with_email_password(
Email::new(arguments.email.clone()),
Password::new(arguments.password.clone()),
)
.await?;
println!(
"Succeeded to sign in with email/password: {:?}",
session
);
Ok(())
}
}