ubi-rs 0.1.6

A Rust cli and library for ubicloud API
Documentation
use crate::client::HTTPClient;
use reqwest::header::HeaderValue;
use std::sync::Arc;

/// The `UbiClient` struct represents a client for interacting with the Ubicloud API.
pub struct UbiClient {
    http_client: Arc<HTTPClient>,
    token: String,

    pub kc: crate::commands::kc::KCClient,
    pub project: crate::commands::project::Project,
}

impl UbiClient {
    /// Creates a new `UbiClient` instance with the provided base URL, version, and token.
    /// # Arguments
    /// * `base_url` - The base URL of the Ubicloud API.
    /// * `version` - The version of the Ubicloud API.
    /// * `token` - The authentication token.
    /// # Returns
    /// A new `UbiClient` instance.
    /// # Example
    /// ```rust
    /// let client = UbiClient::new("https://api.ubicloud.com", "", "your_token_here");
    /// ```
    fn inner_client(base_url: &str, version: &str, token: &str) -> UbiClient {
        // Create headers with content-type set to application/json
        let mut headers = reqwest::header::HeaderMap::new();
        headers.insert(
            "content-type",
            HeaderValue::from_str("application/json").unwrap(),
        );
        headers.insert(
            "Authorization",
            HeaderValue::from_str(&format!("Bearer {}", token)).unwrap(),
        );

        // Build the HTTP client with default headers
        let client = reqwest::ClientBuilder::new()
            .default_headers(headers)
            .build()
            .unwrap();

        let client = Arc::new(HTTPClient::new(base_url, client, version));
        UbiClient {
            http_client: client.clone(),
            token: token.to_string(),
            kc: crate::commands::kc::KCClient::new(client.clone()),
            project: crate::commands::project::Project::new(client.clone()),
        }
    }

    /// Creates a new `UbiClient` instance with the provided base URL, version, and token.
    /// # Arguments
    /// * `base_url` - The base URL of the Ubicloud API.
    /// * `version` - The version of the Ubicloud API.
    /// * `token` - The authentication token.
    /// # Returns
    /// A new `UbiClient` instance.
    /// # Example
    /// ```rust
    /// let client = UbiClient::new("https://api.ubicloud.com", "", "your_token_here");
    /// ```
    pub fn new(base_url: &str, version: &str, token: &str) -> UbiClient {
        UbiClient::inner_client(base_url, version, token)
    }
}