opensearch-client 0.3.1

Strongly typed OpenSearch Client
Documentation
/*
 * opensearch-client
 *
 * Rust Client for OpenSearch
 *
 * The version of the OpenAPI document: 3.1.0
 * Contact: alberto.paro@gmail.com
 * Generated by Paro OpenAPI Generator
 */
use bon::bon;

use super::{configuration, Error};
use crate::apis::ContentType;
use crate::*;
use crate::{apis::ResponseContent, models};
use reqwest;
use serde::{de::Error as OtherError, Deserialize, Serialize};
use std::sync::Arc;

#[async_trait]
pub trait InsightsApi: Send + Sync {
    /// GET /_insights/top_queries///
    /// Retrieves the top queries based on the given metric type (latency, CPU, or memory).
    async fn top_queries(
        &self,
        params: TopQueriesParams,
    ) -> Result<crate::insights::TopQueriesResponse, Error>;
}

pub struct InsightsApiClient {
    configuration: Arc<crate::Configuration>,
}

impl InsightsApiClient {
    pub fn new(configuration: Arc<crate::Configuration>) -> Self {
        Self { configuration }
    }
}

/// Struct for passing parameters to the method [`top_queries`]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "bon", derive(::bon::Builder))]
pub struct TopQueriesParams {
    /// No description available
    pub error_trace: Option<bool>,
    /// No description available
    pub filter_path: Option<common::FilterPath>,
    /// No description available
    pub human: Option<bool>,
    /// No description available
    pub pretty: Option<bool>,
    /// No description available
    pub source: Option<String>,
    /// No description available
    pub r#type: String,
}

#[async_trait]
impl InsightsApi for InsightsApiClient {
    ///
    /// Retrieves the top queries based on the given metric type (latency, CPU, or memory).
    async fn top_queries(
        &self,
        params: TopQueriesParams,
    ) -> Result<crate::insights::TopQueriesResponse, Error> {
        let TopQueriesParams {
            error_trace,
            filter_path,
            human,
            pretty,
            source,
            r#type,
        } = params;

        let local_var_configuration = &self.configuration;

        let local_var_client = &local_var_configuration.client;

        let local_var_uri_str = format!(
            "{}_insights/top_queries",
            local_var_configuration.base_path
        );
        let mut local_var_req_builder =
            local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());

        if let Some(ref local_var_str) = filter_path {
            local_var_req_builder =
                local_var_req_builder.query(&[("filter_path", &local_var_str.to_string())]);
        }
        local_var_req_builder = local_var_req_builder.query(&[("type", &r#type.to_string())]);
        if let Some(ref local_var_str) = pretty {
            local_var_req_builder =
                local_var_req_builder.query(&[("pretty", &local_var_str.to_string())]);
        }
        if let Some(ref local_var_str) = error_trace {
            local_var_req_builder =
                local_var_req_builder.query(&[("error_trace", &local_var_str.to_string())]);
        }
        if let Some(ref local_var_str) = source {
            local_var_req_builder =
                local_var_req_builder.query(&[("source", &local_var_str.to_string())]);
        }
        if let Some(ref local_var_str) = human {
            local_var_req_builder =
                local_var_req_builder.query(&[("human", &local_var_str.to_string())]);
        }

        let local_var_req = local_var_req_builder.build()?;
        let local_var_resp = local_var_client.execute(local_var_req).await?;

        let local_var_status = local_var_resp.status();
        let local_var_content = local_var_resp.text().await?;

        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
            serde_json::from_str(&local_var_content).map_err(Error::from)
        } else {
            let local_var_error = ResponseContent {
                status: local_var_status,
                content: local_var_content,
            };
            Err(Error::ApiError(local_var_error))
        }
    }
}