mz_frontegg_auth/client.rs
1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use std::time::Duration;
11
12use reqwest_retry::RetryTransientMiddleware;
13use reqwest_retry::policies::ExponentialBackoff;
14
15pub mod tokens;
16
17/// Client for Frontegg auth requests.
18///
19/// Internally the client will attempt to de-dupe requests, e.g. if a single user tries to connect
20/// many clients at once, we'll de-dupe the authentication requests.
21#[derive(Clone, Debug)]
22pub struct Client {
23 pub client: reqwest_middleware::ClientWithMiddleware,
24}
25
26impl Default for Client {
27 fn default() -> Self {
28 // Re-use the envd defaults until there's a reason to use something else. This is a separate
29 // function so it's clear that envd can always set its own policies and nothing should
30 // change them, but also we probably want to re-use them for now.
31 Self::environmentd_default()
32 }
33}
34
35impl Client {
36 /// The environmentd Client. Do not change these without a review from the surfaces team.
37 pub fn environmentd_default() -> Self {
38 let retry_policy = ExponentialBackoff::builder()
39 .retry_bounds(Duration::from_millis(200), Duration::from_secs(2))
40 .backoff_exponent(2)
41 .build_with_total_retry_duration(Duration::from_secs(30));
42
43 let client = reqwest::Client::builder()
44 .timeout(Duration::from_secs(5))
45 .build()
46 .expect("must build Client");
47 let client = reqwest_middleware::ClientBuilder::new(client)
48 .with(RetryTransientMiddleware::new_with_policy(retry_policy))
49 .build();
50
51 Self { client }
52 }
53}