/*
 * Orvanta API
 *
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * The version of the OpenAPI document: 1.0.2
 * Contact: contact@orvanta.cloud
 * Generated by: https://openapi-generator.tech
 */


use reqwest;
use serde::{Deserialize, Serialize};
use crate::{apis::ResponseContent, models};
use super::{Error, configuration};


/// struct for typed errors of method [`clear_index`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ClearIndexError {
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`count_search_logs_index`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CountSearchLogsIndexError {
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`get_index_disk_storage_sizes`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetIndexDiskStorageSizesError {
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`get_index_storage_sizes`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetIndexStorageSizesError {
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`get_indexer_status`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetIndexerStatusError {
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`search_jobs_index`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SearchJobsIndexError {
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`search_logs_index`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SearchLogsIndexError {
    UnknownValue(serde_json::Value),
}


pub async fn clear_index(configuration: &configuration::Configuration, idx_name: &str) -> Result<String, Error<ClearIndexError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/indexer/delete/{idx_name}", local_var_configuration.base_path, idx_name=crate::apis::urlencode(idx_name));
    let mut local_var_req_builder = local_var_client.request(reqwest::Method::DELETE, local_var_uri_str.as_str());

    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }
    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
    };

    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() {
        crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<ClearIndexError> = crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

pub async fn count_search_logs_index(configuration: &configuration::Configuration, search_query: &str, min_ts: Option<String>, max_ts: Option<String>) -> Result<models::CountSearchLogsIndex200Response, Error<CountSearchLogsIndexError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

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

    local_var_req_builder = local_var_req_builder.query(&[("search_query", &search_query.to_string())]);
    if let Some(ref local_var_str) = min_ts {
        local_var_req_builder = local_var_req_builder.query(&[("min_ts", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_str) = max_ts {
        local_var_req_builder = local_var_req_builder.query(&[("max_ts", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }
    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
    };

    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() {
        crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<CountSearchLogsIndexError> = crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

pub async fn get_index_disk_storage_sizes(configuration: &configuration::Configuration, ) -> Result<models::GetIndexDiskStorageSizes200Response, Error<GetIndexDiskStorageSizesError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/srch/index/storage/disk", 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_user_agent) = local_var_configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }
    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
    };

    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() {
        crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<GetIndexDiskStorageSizesError> = crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

pub async fn get_index_storage_sizes(configuration: &configuration::Configuration, ) -> Result<models::GetIndexStorageSizes200Response, Error<GetIndexStorageSizesError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/indexer/storage", 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_user_agent) = local_var_configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }
    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
    };

    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() {
        crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<GetIndexStorageSizesError> = crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

pub async fn get_indexer_status(configuration: &configuration::Configuration, ) -> Result<models::GetIndexerStatus200Response, Error<GetIndexerStatusError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/indexer/status", 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_user_agent) = local_var_configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }
    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
    };

    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() {
        crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<GetIndexerStatusError> = crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

pub async fn search_jobs_index(configuration: &configuration::Configuration, workspace: &str, search_query: &str, pagination_offset: Option<i32>) -> Result<models::SearchJobsIndex200Response, Error<SearchJobsIndexError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/srch/w/{workspace}/index/search/job", local_var_configuration.base_path, workspace=crate::apis::urlencode(workspace));
    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());

    local_var_req_builder = local_var_req_builder.query(&[("search_query", &search_query.to_string())]);
    if let Some(ref local_var_str) = pagination_offset {
        local_var_req_builder = local_var_req_builder.query(&[("pagination_offset", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }
    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
    };

    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() {
        crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<SearchJobsIndexError> = crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

pub async fn search_logs_index(configuration: &configuration::Configuration, search_query: &str, mode: &str, hostname: &str, worker_group: Option<&str>, min_ts: Option<String>, max_ts: Option<String>) -> Result<models::SearchLogsIndex200Response, Error<SearchLogsIndexError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

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

    local_var_req_builder = local_var_req_builder.query(&[("search_query", &search_query.to_string())]);
    local_var_req_builder = local_var_req_builder.query(&[("mode", &mode.to_string())]);
    if let Some(ref local_var_str) = worker_group {
        local_var_req_builder = local_var_req_builder.query(&[("worker_group", &local_var_str.to_string())]);
    }
    local_var_req_builder = local_var_req_builder.query(&[("hostname", &hostname.to_string())]);
    if let Some(ref local_var_str) = min_ts {
        local_var_req_builder = local_var_req_builder.query(&[("min_ts", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_str) = max_ts {
        local_var_req_builder = local_var_req_builder.query(&[("max_ts", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }
    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
    };

    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() {
        crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<SearchLogsIndexError> = crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

