orb_billing/config.rs
1// Copyright Materialize, Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License in the LICENSE file at the
6// root of this repository, or online at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use std::time::Duration;
17
18use once_cell::sync::Lazy;
19use reqwest::Url;
20
21use crate::Client;
22
23pub static DEFAULT_ENDPOINT: Lazy<Url> = Lazy::new(|| {
24 "https://api.billwithorb.com/v1"
25 .parse()
26 .expect("url known to be valid")
27});
28
29/// Configures the required parameters of a [`Client`].
30pub struct ClientConfig {
31 /// The API key to authenticate with.
32 pub api_key: String,
33}
34
35/// A builder for a [`Client`].
36pub struct ClientBuilder {
37 endpoint: Url,
38}
39
40impl Default for ClientBuilder {
41 fn default() -> ClientBuilder {
42 ClientBuilder {
43 endpoint: DEFAULT_ENDPOINT.clone(),
44 }
45 }
46}
47
48impl ClientBuilder {
49 /// Creates a [`Client`] that incorporates the optional parameters
50 /// configured on the builder and the specified required parameters.
51 pub fn build(self, config: ClientConfig) -> Client {
52 let inner = reqwest::ClientBuilder::new()
53 .redirect(reqwest::redirect::Policy::none())
54 .timeout(Duration::from_secs(60))
55 .build()
56 .unwrap();
57 Client {
58 inner,
59 api_key: config.api_key,
60 endpoint: self.endpoint,
61 }
62 }
63}
64
65/// Parameters for a list operation.
66#[derive(Debug, Clone)]
67pub struct ListParams {
68 pub(crate) page_size: u64,
69}
70
71impl Default for ListParams {
72 fn default() -> ListParams {
73 ListParams::DEFAULT
74 }
75}
76
77impl ListParams {
78 /// The default list parameters.
79 ///
80 /// Exposed as a constant for use in constant evaluation contexts.
81 pub const DEFAULT: ListParams = ListParams { page_size: 20 };
82
83 /// Sets the page size for the list operation.
84 ///
85 /// The page size only affects the size of each HTTP response. It does not
86 /// change the observable output of the API.
87 ///
88 /// The default page size is 20. The maximum page size is 500.
89 ///
90 /// See the [Orb API pagination documentation][orb-docs] for details.
91 ///
92 /// [orb-docs]: https://docs.withorb.com/docs/orb-docs/05ccf93502e54-pagination
93 pub const fn page_size(mut self, page_size: u64) -> Self {
94 self.page_size = page_size;
95 self
96 }
97}