mz_cloud_api/
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 Materialize
11//! cloud [`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//!
18use std::sync::Arc;
19use std::sync::LazyLock;
20use std::time::Duration;
21
22use url::Url;
23
24use crate::client::Client;
25
26/// The default endpoint the client will use to issue the requests.
27pub static DEFAULT_ENDPOINT: LazyLock<Url> =
28    LazyLock::new(|| "https://api.cloud.materialize.com".parse().unwrap());
29
30/// The header used by the Region API to specify which API version this client supports
31pub static API_VERSION_HEADER: &str = "X-Materialize-Api-Version";
32
33/// Configures the required parameters of a [`Client`].
34pub struct ClientConfig {
35    /// The authorization client to get the authorization token.
36    pub auth_client: Arc<mz_frontegg_client::client::Client>,
37}
38
39/// A builder for a [`Client`].
40pub struct ClientBuilder {
41    /// Endpoint to issue the requests from the client.
42    endpoint: Url,
43}
44
45impl Default for ClientBuilder {
46    /// Returns a [`ClientBuilder`] using the default endpoint.
47    fn default() -> ClientBuilder {
48        ClientBuilder {
49            endpoint: DEFAULT_ENDPOINT.clone(),
50        }
51    }
52}
53
54impl ClientBuilder {
55    /// Overrides the default endpoint.
56    pub fn endpoint(mut self, url: Url) -> ClientBuilder {
57        self.endpoint = url;
58        self
59    }
60
61    /// Creates a [`Client`] that incorporates the optional parameters
62    /// configured on the builder and the specified required parameters.
63    pub fn build(self, config: ClientConfig) -> Client {
64        let inner = reqwest::ClientBuilder::new()
65            .redirect(reqwest::redirect::Policy::none())
66            .timeout(Duration::from_secs(60))
67            .build()
68            .unwrap();
69
70        Client {
71            auth_client: config.auth_client,
72            endpoint: self.endpoint,
73            inner,
74        }
75    }
76}