mz_frontegg_client/
config.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
10//! This module defines the configuration and builder structs for the Frontegg
11//! [`Client`].
12//!
13//! The main type exported from this module is the [`ClientBuilder`] struct,
14//! which is used to configure and build instances of the [`Client`] struct. The
15//! [`Client`] struct provides methods for interacting with various admin APIs,
16//! such as creating and managing users, or listing passwords.
17//!
18//! # Note
19//!
20//! This module default endpoint is intended to run against Materialize and is
21//! not guaranteed to work for other services.
22
23use std::sync::LazyLock;
24use std::time::Duration;
25
26use reqwest::Url;
27
28use crate::client::{Authentication, Client};
29
30/// The default endpoint the client will use to issue the requests. Currently
31/// points to Materialize admin endpoint.
32pub static DEFAULT_ENDPOINT: LazyLock<Url> = LazyLock::new(|| {
33    "https://admin.cloud.materialize.com"
34        .parse()
35        .expect("url known to be valid")
36});
37
38/// Configures the required parameters of a [`Client`].
39pub struct ClientConfig {
40    /// A singular, legitimate app password that will remain in use to identify
41    /// the user throughout the client's existence.
42    pub authentication: Authentication,
43}
44
45/// A builder for a [`Client`].
46pub struct ClientBuilder {
47    /// Endpoint to issue the requests from the client.
48    endpoint: Url,
49}
50
51impl Default for ClientBuilder {
52    /// The default option points to the Materialize admin endpoint.
53    fn default() -> ClientBuilder {
54        ClientBuilder {
55            endpoint: DEFAULT_ENDPOINT.clone(),
56        }
57    }
58}
59
60impl ClientBuilder {
61    /// Overrides the default endpoint.
62    pub fn endpoint(mut self, endpoint: Url) -> ClientBuilder {
63        self.endpoint = endpoint;
64        self
65    }
66
67    /// Creates a [`Client`] that incorporates the optional parameters
68    /// configured on the builder and the specified required parameters.
69    pub fn build(self, config: ClientConfig) -> Client {
70        let inner = reqwest::ClientBuilder::new()
71            .redirect(reqwest::redirect::Policy::none())
72            .timeout(Duration::from_secs(60))
73            .build()
74            .unwrap();
75        Client {
76            inner,
77            authentication: config.authentication,
78            endpoint: self.endpoint,
79            auth: Default::default(),
80        }
81    }
82}