volcengine_rust_sdk/volcengine/client/
client.rs

1/*
2 * @Author: Jerry.Yang
3 * @Date: 2024-10-17 15:07:52
4 * @LastEditors: Jerry.Yang
5 * @LastEditTime: 2025-02-05 14:18:01
6 * @Description: Defines the `Client` and its builder `ClientBuilder` to encapsulate client configuration,
7 * client information, and request handlers for interacting with the Volcengine API.
8 */
9use super::client_info;
10use crate::volcengine::client::config;
11use crate::volcengine::error::error;
12use crate::volcengine::request::handles;
13
14// `Client` struct that holds the client information, configuration, and request handler.
15//
16// This struct is the main client object used to interact with Volcengine services. It contains all the
17// necessary data to authenticate, configure, and send requests to the Volcengine API.
18//
19// # Fields:
20// - `client_info`: Holds the client information such as identification details, credentials, etc.
21// - `config`: Contains the configuration data that defines the client's behavior and connection settings.
22// - `handles`: Manages the request handlers responsible for handling communication with the API.
23#[derive(Debug, Clone)]
24pub struct Client {
25    pub client_info: client_info::ClientInfo, // Client information required for authentication and operations
26    pub config: config::Config,               // Configuration data to set up the client
27    pub handles: handles::Handles, // Request handler responsible for managing API requests
28}
29
30// `Client` implementation providing a `builder` function for constructing `ClientBuilder`
31// The `builder` method allows for a more flexible and readable way to construct a `Client` by chaining method calls
32impl Client {
33    // Creates a new `ClientBuilder` instance, which is used to incrementally set up the `Client` fields
34    // before calling `build` to create the final `Client` instance.
35    //
36    // # Returns:
37    // Returns a new `ClientBuilder` object that allows for configuring and building a `Client` object.
38    pub fn builder() -> ClientBuilder {
39        ClientBuilder {
40            client_info: None, // Optional client information, defaults to None
41            config: None,      // Optional configuration, defaults to None
42            handles: None,     // Optional request handler, defaults to None
43        }
44    }
45}
46
47// `ClientBuilder` struct to incrementally build a `Client` object.
48//
49// The `ClientBuilder` struct provides a builder pattern to facilitate the creation of a `Client` object.
50// Each method allows setting a specific field of the `Client` and returns the builder for method chaining.
51//
52// # Fields:
53// - `client_info`: Optional client information for the API client, may be set using `with_client_info`.
54// - `config`: Optional configuration for the client, set via `with_config`.
55// - `handles`: Optional request handler, set using `with_handles`.
56pub struct ClientBuilder {
57    pub client_info: Option<client_info::ClientInfo>, // Optional client information
58    pub config: Option<config::Config>,               // Optional configuration
59    pub handles: Option<handles::Handles>,            // Optional request handler
60}
61
62// `ClientBuilder` implementation to add fields to the `Client` struct and build it with validation.
63//
64// The `ClientBuilder` provides methods for setting each field of the `Client`. These methods are chainable,
65// allowing for a fluent API. The `build` method performs validation to ensure all required fields are set before
66// returning the final `Client` instance.
67//
68// # Methods:
69// - `with_client_info`: Sets the `client_info` field in the builder.
70// - `with_config`: Sets the `config` field in the builder.
71// - `with_handles`: Sets the `handles` field in the builder.
72// - `build`: Finalizes the builder, ensuring that all required fields are set, and returns the constructed `Client` instance.
73impl ClientBuilder {
74    // Sets the `client_info` field for the builder.
75    // The `client_info` represents the information about the client, such as authentication details.
76    //
77    // # Arguments:
78    // - `client_info`: A reference to a `ClientInfo` struct that holds the client's information.
79    //
80    // # Returns:
81    // Returns the builder instance (`Self`) to allow for method chaining.
82    pub fn with_client_info(mut self, client_info: &client_info::ClientInfo) -> Self {
83        self.client_info = Some(client_info.clone());
84        self
85    }
86
87    // Sets the `config` field for the builder.
88    // The `config` field holds the configuration data needed for the client, including connection settings.
89    //
90    // # Arguments:
91    // - `config`: A reference to a `Config` struct that contains the configuration for the client.
92    //
93    // # Returns:
94    // Returns the builder instance (`Self`) to allow for method chaining.
95    pub fn with_config(mut self, config: &config::Config) -> Self {
96        self.config = Some(config.clone());
97        self
98    }
99
100    // Sets the `handles` field for the builder.
101    // The `handles` field manages the request handlers that facilitate the communication with the Volcengine API.
102    //
103    // # Arguments:
104    // - `handles`: A reference to a `Handles` struct that contains request handlers.
105    //
106    // # Returns:
107    // Returns the builder instance (`Self`) to allow for method chaining.
108    pub fn with_handles(mut self, handles: &handles::Handles) -> Self {
109        self.handles = Some(handles.clone());
110        self
111    }
112
113    // Builds the `Client` instance from the builder, performing necessary validation on the required fields.
114    // If any of the required fields (`client_info`, `config`, `handles`) are missing, an error is returned.
115    //
116    // # Returns:
117    // - `Ok(Client)`: If all required fields are set, returns the constructed `Client`.
118    // - `Err(error::Error)`: If any required field is missing, returns an error indicating which field is missing.
119    pub fn build(self) -> Result<Client, error::Error> {
120        // Validate that `client_info` is set
121        if self.client_info.is_none() {
122            return Err(error::Error::ErrUtilClientBuildClientNo(
123                "client_info".to_string(),
124            ));
125        }
126
127        // Validate that `config` is set
128        if self.config.is_none() {
129            return Err(error::Error::ErrUtilClientBuildClientNo(
130                "config".to_string(),
131            ));
132        }
133
134        // Validate that `handles` is set
135        if self.handles.is_none() {
136            return Err(error::Error::ErrUtilClientBuildClientNo(
137                "handles".to_string(),
138            ));
139        }
140
141        // Return the fully constructed `Client` object with the set fields
142        Ok(Client {
143            client_info: self.client_info.unwrap(),
144            config: self.config.unwrap(),
145            handles: self.handles.unwrap(),
146        })
147    }
148}