keygen_rs/
keygen_client.rs1use crate::api_version::ApiContractVersion;
2use crate::client::{Client, ClientOptions};
3use crate::config::{get_config, KeygenConfig};
4use crate::errors::Error;
5use crate::license::LicenseService;
6#[cfg(feature = "license-key")]
7use crate::license::{LicenseValidationRequest, LicenseValidationResult};
8#[cfg(feature = "token")]
9use crate::profile::CurrentProfile;
10use crate::service::ServiceInfo;
11use std::sync::Arc;
12
13#[derive(Clone, Debug)]
14pub struct KeygenClient {
15 pub(crate) transport: Arc<Client>,
16 config: Arc<KeygenConfig>,
17}
18
19impl KeygenClient {
20 pub fn builder() -> KeygenClientBuilder {
21 KeygenClientBuilder::default()
22 }
23
24 pub fn new(config: KeygenConfig) -> Result<Self, Error> {
25 let transport = Arc::new(Client::new(ClientOptions::from(config.clone()))?);
26 Ok(Self {
27 transport,
28 config: Arc::new(config),
29 })
30 }
31
32 pub fn from_global_config() -> Result<Self, Error> {
33 Self::new(get_config()?)
34 }
35
36 pub fn api_contract_version(&self) -> ApiContractVersion {
37 self.config.api_version
38 }
39
40 pub fn config(&self) -> &KeygenConfig {
41 &self.config
42 }
43
44 pub(crate) fn config_arc(&self) -> Arc<KeygenConfig> {
45 Arc::clone(&self.config)
46 }
47
48 pub(crate) fn transport_arc(&self) -> Arc<Client> {
49 Arc::clone(&self.transport)
50 }
51
52 pub fn licenses(&self) -> LicenseService<'_> {
53 LicenseService::new(self)
54 }
55
56 pub async fn service_info(&self) -> Result<ServiceInfo, Error> {
57 crate::service::get_service_info_with_client(&self.transport).await
58 }
59
60 #[cfg(feature = "token")]
62 pub async fn current_profile(&self) -> Result<CurrentProfile, Error> {
63 crate::profile::get_current_profile_with_client(&self.transport).await
64 }
65
66 #[cfg(feature = "license-key")]
67 pub async fn validate(
68 &self,
69 request: &LicenseValidationRequest,
70 ) -> Result<LicenseValidationResult, Error> {
71 let mut request = request.clone();
72 if request.scope.product.is_none() {
73 request.scope.product = Some(self.config.product.clone());
74 }
75 self.licenses()
76 .validate_key(
77 self.config
78 .license_key
79 .as_deref()
80 .ok_or(Error::LicenseKeyMissing)?,
81 &request,
82 )
83 .await
84 }
85
86 #[must_use = "verification result should be checked"]
87 #[cfg(feature = "license-key")]
88 pub fn verify(
89 &self,
90 scheme: crate::license::SchemeCode,
91 signed_key: &str,
92 ) -> Result<Vec<u8>, Error> {
93 self.licenses().verify(scheme, signed_key)
94 }
95}
96
97#[derive(Clone, Debug, Default)]
98pub struct KeygenClientBuilder {
99 config: KeygenConfig,
100}
101
102impl KeygenClientBuilder {
103 pub fn account(mut self, account: impl Into<String>) -> Self {
104 self.config.account = account.into();
105 self
106 }
107
108 pub fn api_url(mut self, api_url: impl Into<String>) -> Self {
109 self.config.api_url = api_url.into();
110 self
111 }
112
113 pub fn api_contract_version(mut self, api_version: ApiContractVersion) -> Self {
114 self.config.api_version = api_version;
115 self
116 }
117
118 pub fn api_prefix(mut self, api_prefix: impl Into<String>) -> Self {
119 self.config.api_prefix = api_prefix.into();
120 self
121 }
122
123 pub fn environment(mut self, environment: impl Into<String>) -> Self {
124 self.config.environment = Some(environment.into());
125 self
126 }
127
128 pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
129 self.config.user_agent = Some(user_agent.into());
130 self
131 }
132
133 #[cfg(feature = "license-key")]
134 pub fn product(mut self, product: impl Into<String>) -> Self {
135 self.config.product = product.into();
136 self
137 }
138
139 #[cfg(feature = "license-key")]
140 pub fn package(mut self, package: impl Into<String>) -> Self {
141 self.config.package = package.into();
142 self
143 }
144
145 #[cfg(feature = "license-key")]
146 pub fn license_key(mut self, license_key: impl Into<String>) -> Self {
147 self.config.license_key = Some(license_key.into());
148 self
149 }
150
151 #[cfg(feature = "license-key")]
152 pub fn public_key(mut self, public_key: impl Into<String>) -> Self {
153 self.config.public_key = Some(public_key.into());
154 self
155 }
156
157 #[cfg(feature = "license-key")]
158 pub fn platform(mut self, platform: impl Into<String>) -> Self {
159 self.config.platform = Some(platform.into());
160 self
161 }
162
163 #[cfg(feature = "license-key")]
164 pub fn max_clock_drift(mut self, max_clock_drift: i64) -> Self {
165 self.config.max_clock_drift = Some(max_clock_drift);
166 self
167 }
168
169 #[cfg(feature = "license-key")]
170 pub fn verify_keygen_signature(mut self, verify: bool) -> Self {
171 self.config.verify_keygen_signature = Some(verify);
172 self
173 }
174
175 #[cfg(feature = "token")]
176 pub fn token(mut self, token: impl Into<String>) -> Self {
177 self.config.token = Some(token.into());
178 self
179 }
180
181 pub fn build(self) -> Result<KeygenClient, Error> {
182 KeygenClient::new(self.config)
183 }
184}