rusty_box/client/
box_client.rs1#[derive(Debug)]
5pub struct BoxClient<'a> {
6 pub auth: Box<dyn Auth<'a> + 'static>,
7 pub http: HttpClient,
8}
9
10impl<'a> BoxClient<'a> {
11 pub fn new(auth: Box<dyn Auth<'a> + 'static>) -> Self {
12 Self {
13 auth,
14 http: HttpClient::default(),
15 }
16 }
17
18 pub async fn headers(&mut self) -> Result<Headers, BoxAPIError> {
20 let mut headers = Headers::new();
21
22 headers.insert("Accept".to_string(), "application/json".to_string());
23 headers.insert("Content-Type".to_string(), "application/json".to_string());
24 headers.insert("User-Agent".to_string(), self.auth.user_agent());
25
26 let auth_headers = self.auth.auth_header().await?;
27
28 headers.extend(auth_headers);
29 Ok(headers)
30 }
31}
32
33#[cfg(test)]
34use crate::auth::auth_ccg::{CCGAuth, SubjectType};
35use crate::auth::Auth;
36
37use super::{
38 client_error::BoxAPIError,
39 http_client::{Headers, HttpClient},
40};
41
42#[tokio::test]
43async fn test_create_client_ccg() {
44 dotenv::from_filename(".ccg.env").ok();
45 let config = crate::config::Config::new();
46 let client_id = std::env::var("CLIENT_ID").expect("CLIENT_ID must be set");
47 let client_secret = std::env::var("CLIENT_SECRET").expect("CLIENT_SECRET must be set");
48 let env_subject_type = std::env::var("BOX_SUBJECT_TYPE").expect("BOX_SUBJECT_TYPE must be set");
49 let box_subject_type = match env_subject_type.as_str() {
50 "user" => SubjectType::User,
51 "enterprise" => SubjectType::Enterprise,
52 _ => panic!("BOX_SUBJECT_TYPE must be either 'user' or 'enterprise'"),
53 };
54
55 let box_subject_id = std::env::var("BOX_SUBJECT_ID").expect("BOX_SUBJECT_ID must be set");
56
57 let auth = CCGAuth::new(
58 config,
59 client_id,
60 client_secret,
61 box_subject_type,
62 box_subject_id,
63 );
64
65 let mut client = BoxClient::new(Box::new(auth));
66 let access_token = client.auth.access_token().await;
67 assert!(access_token.is_ok());
69}