/*
 * Orvanta API
 *
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * The version of the OpenAPI document: 1.0.6
 * 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 [`check_if_native_triggers_service_exists`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CheckIfNativeTriggersServiceExistsError {
    UnknownValue(serde_json::Value),
}

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

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

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

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

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

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

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


pub async fn check_if_native_triggers_service_exists(configuration: &configuration::Configuration, workspace: &str, service_name: models::NativeServiceName) -> Result<bool, Error<CheckIfNativeTriggersServiceExistsError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/w/{workspace}/native_triggers/integrations/{service_name}/exists", local_var_configuration.base_path, workspace=crate::apis::urlencode(workspace), service_name=service_name.to_string());
    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<CheckIfNativeTriggersServiceExistsError> = 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 check_instance_sharing_available(configuration: &configuration::Configuration, workspace: &str, service_name: models::NativeServiceName) -> Result<bool, Error<CheckInstanceSharingAvailableError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/w/{workspace}/native_triggers/integrations/{service_name}/instance_sharing_available", local_var_configuration.base_path, workspace=crate::apis::urlencode(workspace), service_name=service_name.to_string());
    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<CheckInstanceSharingAvailableError> = 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 create_native_trigger_service(configuration: &configuration::Configuration, workspace: &str, service_name: models::NativeServiceName, workspace_o_auth_config: models::WorkspaceOAuthConfig) -> Result<String, Error<CreateNativeTriggerServiceError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/w/{workspace}/native_triggers/integrations/{service_name}/create", local_var_configuration.base_path, workspace=crate::apis::urlencode(workspace), service_name=service_name.to_string());
    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, 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());
    };
    local_var_req_builder = local_var_req_builder.json(&workspace_o_auth_config);

    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<CreateNativeTriggerServiceError> = 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 delete_native_trigger_service(configuration: &configuration::Configuration, workspace: &str, service_name: models::NativeServiceName) -> Result<String, Error<DeleteNativeTriggerServiceError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/w/{workspace}/native_triggers/integrations/{service_name}/delete", local_var_configuration.base_path, workspace=crate::apis::urlencode(workspace), service_name=service_name.to_string());
    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<DeleteNativeTriggerServiceError> = 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 generate_instance_connect_url(configuration: &configuration::Configuration, workspace: &str, service_name: models::NativeServiceName, redirect_uri: models::RedirectUri) -> Result<String, Error<GenerateInstanceConnectUrlError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/w/{workspace}/native_triggers/integrations/{service_name}/generate_instance_connect_url", local_var_configuration.base_path, workspace=crate::apis::urlencode(workspace), service_name=service_name.to_string());
    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, 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());
    };
    local_var_req_builder = local_var_req_builder.json(&redirect_uri);

    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<GenerateInstanceConnectUrlError> = 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 generate_native_trigger_service_connect_url(configuration: &configuration::Configuration, workspace: &str, service_name: models::NativeServiceName, redirect_uri: models::RedirectUri) -> Result<String, Error<GenerateNativeTriggerServiceConnectUrlError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/w/{workspace}/native_triggers/integrations/{service_name}/generate_connect_url", local_var_configuration.base_path, workspace=crate::apis::urlencode(workspace), service_name=service_name.to_string());
    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, 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());
    };
    local_var_req_builder = local_var_req_builder.json(&redirect_uri);

    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<GenerateNativeTriggerServiceConnectUrlError> = 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 list_native_trigger_services(configuration: &configuration::Configuration, workspace: &str) -> Result<Vec<models::WorkspaceIntegrations>, Error<ListNativeTriggerServicesError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/w/{workspace}/native_triggers/integrations/list", 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());

    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<ListNativeTriggerServicesError> = 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 native_trigger_service_callback(configuration: &configuration::Configuration, workspace: &str, service_name: models::NativeServiceName, native_trigger_service_callback_request: models::NativeTriggerServiceCallbackRequest) -> Result<String, Error<NativeTriggerServiceCallbackError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/w/{workspace}/native_triggers/integrations/{service_name}/callback", local_var_configuration.base_path, workspace=crate::apis::urlencode(workspace), service_name=service_name.to_string());
    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, 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());
    };
    local_var_req_builder = local_var_req_builder.json(&native_trigger_service_callback_request);

    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<NativeTriggerServiceCallbackError> = 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))
    }
}

