ubi_rs/ubiclient.rs
1use crate::client::HTTPClient;
2use reqwest::header::HeaderValue;
3use std::sync::Arc;
4
5/// The `UbiClient` struct represents a client for interacting with the Ubicloud API.
6pub struct UbiClient {
7 http_client: Arc<HTTPClient>,
8 token: String,
9
10 pub kc: crate::commands::kc::KCClient,
11 pub project: crate::commands::project::Project,
12}
13
14impl UbiClient {
15 /// Creates a new `UbiClient` instance with the provided base URL, version, and token.
16 /// # Arguments
17 /// * `base_url` - The base URL of the Ubicloud API.
18 /// * `version` - The version of the Ubicloud API.
19 /// * `token` - The authentication token.
20 /// # Returns
21 /// A new `UbiClient` instance.
22 /// # Example
23 /// ```rust
24 /// let client = UbiClient::new("https://api.ubicloud.com", "", "your_token_here");
25 /// ```
26 fn inner_client(base_url: &str, version: &str, token: &str) -> UbiClient {
27 // Create headers with content-type set to application/json
28 let mut headers = reqwest::header::HeaderMap::new();
29 headers.insert(
30 "content-type",
31 HeaderValue::from_str("application/json").unwrap(),
32 );
33 headers.insert(
34 "Authorization",
35 HeaderValue::from_str(&format!("Bearer {}", token)).unwrap(),
36 );
37
38 // Build the HTTP client with default headers
39 let client = reqwest::ClientBuilder::new()
40 .default_headers(headers)
41 .build()
42 .unwrap();
43
44 let client = Arc::new(HTTPClient::new(base_url, client, version));
45 UbiClient {
46 http_client: client.clone(),
47 token: token.to_string(),
48 kc: crate::commands::kc::KCClient::new(client.clone()),
49 project: crate::commands::project::Project::new(client.clone()),
50 }
51 }
52
53 /// Creates a new `UbiClient` instance with the provided base URL, version, and token.
54 /// # Arguments
55 /// * `base_url` - The base URL of the Ubicloud API.
56 /// * `version` - The version of the Ubicloud API.
57 /// * `token` - The authentication token.
58 /// # Returns
59 /// A new `UbiClient` instance.
60 /// # Example
61 /// ```rust
62 /// let client = UbiClient::new("https://api.ubicloud.com", "", "your_token_here");
63 /// ```
64 pub fn new(base_url: &str, version: &str, token: &str) -> UbiClient {
65 UbiClient::inner_client(base_url, version, token)
66 }
67}