volcengine_rust_sdk/service/clb/
service_clb.rs

1/*
2 * @Author: Jerry.Yang
3 * @Date: 2024-11-12 17:23:37
4 * @LastEditors: Jerry.Yang
5 * @LastEditTime: 2025-02-05 14:44:03
6 * @Description: service clb
7 */
8use crate::service::clb::api_describe_load_balancers;
9use crate::service::clb::Clb;
10use crate::service::clb::ServiceClb;
11use crate::volcengine::client::client;
12use crate::volcengine::client::client_info;
13use crate::volcengine::client::config as client_config;
14use crate::volcengine::common;
15use crate::volcengine::error::error;
16use crate::volcengine::request::handles;
17use crate::volcengine::session::session;
18
19/**
20 * @description: `ServiceClb` trait implementation for `Clb` struct
21 *
22 * This implementation defines the methods required to create and interact with the CLB service
23 * through the `ServiceClb` trait. The trait includes functionality for initializing a new `Clb`
24 * instance and sending requests to the CLB service, such as describing load balancers.
25 *
26 * - `new_clb`: Creates a new instance of `Clb` with a valid client configuration.
27 * - `new_describe_load_balancers`: Sends a request to describe load balancers using the API defined in
28 *   `api_describe_load_balancers::ApiDescribeLoadBalancersClb`.
29 *
30 * The `Clb` struct will use the `client::Client`, `client_info::ClientInfo`, and `handles::Handles`
31 * to make requests to the API and parse the responses appropriately.
32 *
33 * @date: 2024-11-13 10:54:42
34 * @return: ServiceClb trait methods implementation for Clb
35 */
36impl ServiceClb for Clb {
37    /// `new_clb` initializes a new instance of the `Clb` struct.
38    ///
39    /// This method creates a `Clb` service by setting up the client configuration,
40    /// client information, and the necessary handles for making requests.
41    /// The method returns a `Clb` instance on success or an error if the configuration fails.
42    ///
43    /// # Arguments
44    /// - `session`: The session object used to retrieve the necessary client configuration for the CLB service.
45    ///
46    /// # Returns
47    /// - `Ok(Clb)`: A new `Clb` instance if the initialization succeeds.
48    /// - `Err(error::Error)`: Returns an error if the configuration or client setup fails.
49    fn new_clb(session: session::Session) -> Result<Self, error::Error> {
50        // Create a new client configuration from the session.
51        let client_config = session.new_client_config(client_config::ClientServiceName::Clb);
52
53        // Construct client info using the client configuration and common API version.
54        let client_info = client_info::ClientInfo::builder()
55            .with_service_name(client_config::ClientServiceName::Clb)
56            .with_api_version(common::COMMON_VERSION)
57            .with_signing_region(&client_config.signing_region)
58            .build()?;
59
60        // Initialize handles (to be used for request management).
61        let request_handles = handles::Handles {};
62
63        // Build the client using the client info, configuration, and request handles.
64        let client = client::Client::builder()
65            .with_client_info(&client_info)
66            .with_config(&client_config)
67            .with_handles(&request_handles)
68            .build()?;
69
70        // Return the initialized `Clb` instance.
71        Ok(Clb { client: client })
72    }
73
74    /// `new_describe_load_balancers` sends a request to the CLB service to describe load balancers.
75    ///
76    /// This method builds the request to the `DescribeLoadBalancers` API, sends the request asynchronously,
77    /// and parses the response into a structured format.
78    /// It returns the parsed response on success, or an error if the request or response handling fails.
79    ///
80    /// # Arguments
81    /// - `&self`: The reference to the `Clb` instance calling this method.
82    /// - `request`: The request struct (`DescribeLoadBalancersReq`) containing parameters for the load balancer query.
83    ///
84    /// # Returns
85    /// - `Ok(DescribeLoadBalancersResp)`: A successful response containing details of the load balancers.
86    /// - `Err(error::Error)`: An error if the request fails or the response cannot be parsed.
87    async fn new_describe_load_balancers(
88        &self,
89        request: volcengine_sdk_protobuf::protobuf::lb_instance::DescribeLoadBalancersReq,
90    ) -> Result<
91        volcengine_sdk_protobuf::protobuf::lb_instance::DescribeLoadBalancersResp,
92        error::Error,
93    > {
94        // Call the `ApiDescribeLoadBalancersClb` API method to handle the request.
95        api_describe_load_balancers::ApiDescribeLoadBalancersClb
96            .new_describe_load_balancers_api(self, request)
97            .await
98    }
99}