stripe_client_core/
config.rs1use std::fmt;
2use std::fmt::{Display, Formatter};
3
4use stripe_shared::version::VERSION;
5use stripe_shared::{AccountId, ApiVersion, ApplicationId};
6
7use crate::RequestStrategy;
8
9#[derive(Clone)]
18pub struct SharedConfigBuilder {
19 pub stripe_version: ApiVersion,
21 pub app_info_str: Option<String>,
23 pub client_id: Option<ApplicationId>,
25 pub account_id: Option<AccountId>,
27 pub request_strategy: Option<RequestStrategy>,
29 pub secret: String,
31 pub api_base: Option<String>,
33}
34
35impl SharedConfigBuilder {
36 pub fn new(secret: impl Into<String>) -> Self {
38 let secret = secret.into();
39
40 if secret.trim() != secret || !secret.starts_with("sk_") {
43 tracing::warn!("suspiciously formatted secret key")
44 }
45
46 Self {
47 stripe_version: VERSION,
48 app_info_str: None,
49 client_id: None,
50 account_id: None,
51 request_strategy: None,
52 secret,
53 api_base: None,
54 }
55 }
56
57 pub fn client_id(mut self, client_id: ApplicationId) -> Self {
60 self.client_id = Some(client_id);
61 self
62 }
63
64 pub fn account_id(mut self, account_id: AccountId) -> Self {
68 self.account_id = Some(account_id);
69 self
70 }
71
72 pub fn request_strategy(mut self, strategy: RequestStrategy) -> Self {
74 self.request_strategy = Some(strategy);
75 self
76 }
77
78 pub fn url(mut self, url: impl Into<String>) -> Self {
80 self.api_base = Some(url.into());
81 self
82 }
83
84 pub fn app_info(
86 mut self,
87 name: impl Into<String>,
88 version: Option<String>,
89 url: Option<String>,
90 ) -> Self {
91 self.app_info_str = Some(AppInfo { name: name.into(), url, version }.to_string());
92 self
93 }
94}
95
96struct AppInfo {
97 name: String,
98 url: Option<String>,
99 version: Option<String>,
100}
101
102impl Display for AppInfo {
103 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
107 let name = &self.name;
108 match (&self.version, &self.url) {
109 (Some(a), Some(b)) => write!(f, "{name}/{a} ({b})"),
110 (Some(a), None) => write!(f, "{name}/{a}"),
111 (None, Some(b)) => write!(f, "{name} ({b})"),
112 _ => f.write_str(name),
113 }
114 }
115}
116
117impl fmt::Debug for SharedConfigBuilder {
119 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
120 let mut builder = f.debug_struct("SharedConfigBuilder");
121 builder.field("request_strategy", &self.request_strategy);
122 builder.field("client_id", &self.client_id);
123 builder.field("account_id", &self.account_id);
124 builder.field("app_info_str", &self.app_info_str);
125 if let Some(api_base) = &self.api_base {
126 builder.field("api_base", api_base);
127 }
128 builder.field("stripe_version", &self.stripe_version);
129 builder.finish()
130 }
131}
132
133#[derive(Debug)]
135#[non_exhaustive]
136pub struct ConfigOverride {
137 pub account_id: Option<AccountId>,
139 pub request_strategy: Option<RequestStrategy>,
141}
142
143impl ConfigOverride {
144 pub(crate) fn new() -> Self {
145 Self { account_id: None, request_strategy: None }
146 }
147}