Skip to main content

tapis_authenticator/
client.rs

1use crate::apis::{
2    admin_api, clients_api, configuration, health_check_api, metadata_api, profiles_api,
3    tokens_api, Error,
4};
5use crate::models;
6use http::header::{HeaderMap, HeaderValue};
7use reqwest::{Client, Request, Response};
8use reqwest_middleware::{ClientBuilder, Middleware, Next, Result as MiddlewareResult};
9use std::sync::Arc;
10
11#[derive(Debug)]
12struct LoggingMiddleware;
13
14#[async_trait::async_trait]
15impl Middleware for LoggingMiddleware {
16    async fn handle(
17        &self,
18        req: Request,
19        extensions: &mut http::Extensions,
20        next: Next<'_>,
21    ) -> MiddlewareResult<Response> {
22        let method = req.method().clone();
23        let url = req.url().clone();
24        println!("Tapis SDK request: {} {}", method, url);
25        next.run(req, extensions).await
26    }
27}
28
29#[derive(Clone)]
30pub struct TapisAuthenticator {
31    config: Arc<configuration::Configuration>,
32    pub admin: AdminClient,
33    pub clients: ClientsClient,
34    pub health_check: HealthCheckClient,
35    pub metadata: MetadataClient,
36    pub profiles: ProfilesClient,
37    pub tokens: TokensClient,
38}
39
40impl TapisAuthenticator {
41    pub fn new(
42        base_url: &str,
43        jwt_token: Option<&str>,
44    ) -> Result<Self, Box<dyn std::error::Error>> {
45        let mut headers = HeaderMap::new();
46        if let Some(token) = jwt_token {
47            headers.insert("X-Tapis-Token", HeaderValue::from_str(token)?);
48        }
49
50        let reqwest_client = Client::builder().default_headers(headers).build()?;
51
52        let client = ClientBuilder::new(reqwest_client)
53            .with(LoggingMiddleware)
54            .build();
55
56        let mut config = configuration::Configuration::default();
57        config.base_path = base_url.to_string();
58        config.client = client;
59
60        let config = Arc::new(config);
61
62        Ok(Self {
63            config: config.clone(),
64            admin: AdminClient {
65                config: config.clone(),
66            },
67            clients: ClientsClient {
68                config: config.clone(),
69            },
70            health_check: HealthCheckClient {
71                config: config.clone(),
72            },
73            metadata: MetadataClient {
74                config: config.clone(),
75            },
76            profiles: ProfilesClient {
77                config: config.clone(),
78            },
79            tokens: TokensClient {
80                config: config.clone(),
81            },
82        })
83    }
84
85    pub fn config(&self) -> &configuration::Configuration {
86        &self.config
87    }
88}
89
90#[derive(Clone)]
91pub struct AdminClient {
92    config: Arc<configuration::Configuration>,
93}
94
95impl AdminClient {
96    pub async fn get_config(
97        &self,
98    ) -> Result<models::GetConfig200Response, Error<admin_api::GetConfigError>> {
99        admin_api::get_config(&self.config).await
100    }
101
102    pub async fn update_config(
103        &self,
104        new_tenant_config: models::NewTenantConfig,
105    ) -> Result<models::GetConfig200Response, Error<admin_api::UpdateConfigError>> {
106        admin_api::update_config(&self.config, new_tenant_config).await
107    }
108}
109
110#[derive(Clone)]
111pub struct ClientsClient {
112    config: Arc<configuration::Configuration>,
113}
114
115impl ClientsClient {
116    pub async fn create_client(
117        &self,
118        new_client: models::NewClient,
119    ) -> Result<models::CreateClient201Response, Error<clients_api::CreateClientError>> {
120        clients_api::create_client(&self.config, new_client).await
121    }
122
123    pub async fn delete_client(
124        &self,
125        client_id: &str,
126    ) -> Result<models::DeleteClient200Response, Error<clients_api::DeleteClientError>> {
127        clients_api::delete_client(&self.config, client_id).await
128    }
129
130    pub async fn get_client(
131        &self,
132        client_id: &str,
133    ) -> Result<models::CreateClient201Response, Error<clients_api::GetClientError>> {
134        clients_api::get_client(&self.config, client_id).await
135    }
136
137    pub async fn list_clients(
138        &self,
139        limit: Option<i32>,
140        offset: Option<i32>,
141    ) -> Result<models::ListClients200Response, Error<clients_api::ListClientsError>> {
142        clients_api::list_clients(&self.config, limit, offset).await
143    }
144
145    pub async fn update_client(
146        &self,
147        client_id: &str,
148        update_client: models::UpdateClient,
149    ) -> Result<models::CreateClient201Response, Error<clients_api::UpdateClientError>> {
150        clients_api::update_client(&self.config, client_id, update_client).await
151    }
152}
153
154#[derive(Clone)]
155pub struct HealthCheckClient {
156    config: Arc<configuration::Configuration>,
157}
158
159impl HealthCheckClient {
160    pub async fn hello(
161        &self,
162    ) -> Result<models::BasicResponse, Error<health_check_api::HelloError>> {
163        health_check_api::hello(&self.config).await
164    }
165
166    pub async fn ready(
167        &self,
168    ) -> Result<models::BasicResponse, Error<health_check_api::ReadyError>> {
169        health_check_api::ready(&self.config).await
170    }
171}
172
173#[derive(Clone)]
174pub struct MetadataClient {
175    config: Arc<configuration::Configuration>,
176}
177
178impl MetadataClient {
179    pub async fn get_server_metadata(
180        &self,
181    ) -> Result<models::GetServerMetadata200Response, Error<metadata_api::GetServerMetadataError>>
182    {
183        metadata_api::get_server_metadata(&self.config).await
184    }
185}
186
187#[derive(Clone)]
188pub struct ProfilesClient {
189    config: Arc<configuration::Configuration>,
190}
191
192impl ProfilesClient {
193    pub async fn get_profile(
194        &self,
195        username: &str,
196    ) -> Result<models::GetUserinfo200Response, Error<profiles_api::GetProfileError>> {
197        profiles_api::get_profile(&self.config, username).await
198    }
199
200    pub async fn get_userinfo(
201        &self,
202    ) -> Result<models::GetUserinfo200Response, Error<profiles_api::GetUserinfoError>> {
203        profiles_api::get_userinfo(&self.config).await
204    }
205
206    pub async fn list_profiles(
207        &self,
208        limit: Option<i32>,
209        offset: Option<i32>,
210    ) -> Result<models::ListProfiles200Response, Error<profiles_api::ListProfilesError>> {
211        profiles_api::list_profiles(&self.config, limit, offset).await
212    }
213}
214
215#[derive(Clone)]
216pub struct TokensClient {
217    config: Arc<configuration::Configuration>,
218}
219
220impl TokensClient {
221    pub async fn create_token(
222        &self,
223        new_token: models::NewToken,
224    ) -> Result<models::CreateToken201Response, Error<tokens_api::CreateTokenError>> {
225        tokens_api::create_token(&self.config, new_token).await
226    }
227
228    pub async fn create_v2_token(
229        &self,
230        v2_token: models::V2Token,
231    ) -> Result<models::CreateV2Token200Response, Error<tokens_api::CreateV2TokenError>> {
232        tokens_api::create_v2_token(&self.config, v2_token).await
233    }
234
235    pub async fn generate_device_code(
236        &self,
237        new_device_code: models::NewDeviceCode,
238    ) -> Result<models::GenerateDeviceCode200Response, Error<tokens_api::GenerateDeviceCodeError>>
239    {
240        tokens_api::generate_device_code(&self.config, new_device_code).await
241    }
242
243    pub async fn revoke_token(
244        &self,
245        revoke_token_request: models::RevokeTokenRequest,
246    ) -> Result<models::BasicResponse, Error<tokens_api::RevokeTokenError>> {
247        tokens_api::revoke_token(&self.config, revoke_token_request).await
248    }
249}