// Code generated by oagen. DO NOT EDIT.

use crate::client::Retab;
#[allow(unused_imports)]
use crate::enums::*;
use crate::error::Error;
#[allow(unused_imports)]
use crate::models::*;
#[allow(unused_imports)]
use serde::Serialize;

pub struct TablesApi<'a> {
    pub(crate) client: &'a Retab,
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct ListParams {
    /// Only return tables belonging to this project.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub project_id: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct CreateParams {
    /// Request body sent with this call.
    ///
    /// Required.
    #[serde(skip)]
    pub body: CreateWorkflowTableUploadRequest,
}

impl CreateParams {
    /// Construct a new `CreateParams` with the required fields set.
    #[allow(deprecated)]
    pub fn new(body: CreateWorkflowTableUploadRequest) -> Self {
        Self { body }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct ReplaceParams {
    /// Request body sent with this call.
    ///
    /// Required.
    #[serde(skip)]
    pub body: ReplaceWorkflowTableUploadRequest,
}

impl ReplaceParams {
    /// Construct a new `ReplaceParams` with the required fields set.
    #[allow(deprecated)]
    pub fn new(body: ReplaceWorkflowTableUploadRequest) -> Self {
        Self { body }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct UpdateTableParams {
    /// Request body sent with this call.
    ///
    /// Required.
    #[serde(skip)]
    pub body: UpdateWorkflowTableRequest,
}

impl UpdateTableParams {
    /// Construct a new `UpdateTableParams` with the required fields set.
    #[allow(deprecated)]
    pub fn new(body: UpdateWorkflowTableRequest) -> Self {
        Self { body }
    }
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct ProfileParams {
    /// Defaults to `[]`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub select: Option<Vec<String>>,
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct QueryParams {
    /// Request body sent with this call.
    #[serde(skip)]
    pub body: Option<QueryWorkflowTableRequest>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ValidateParams {
    /// Request body sent with this call.
    ///
    /// Required.
    #[serde(skip)]
    pub body: WorkflowTableValidationRequest,
}

impl ValidateParams {
    /// Construct a new `ValidateParams` with the required fields set.
    #[allow(deprecated)]
    pub fn new(body: WorkflowTableValidationRequest) -> Self {
        Self { body }
    }
}

impl<'a> TablesApi<'a> {
    /// Table.List
    pub async fn list(&self, params: ListParams) -> Result<WorkflowTableListResponse, Error> {
        self.list_with_options(params, None).await
    }

    /// Variant of [`Self::list`] that accepts per-request [`crate::RequestOptions`].
    pub async fn list_with_options(
        &self,
        params: ListParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<WorkflowTableListResponse, Error> {
        let path = "/v1/tables".to_string();
        let method = http::Method::GET;
        self.client
            .request_with_query_opts(method, &path, &params, options)
            .await
    }

    /// Table.Create
    pub async fn create(&self, params: CreateParams) -> Result<WorkflowTableListResponse, Error> {
        self.create_with_options(params, None).await
    }

    /// Variant of [`Self::create`] that accepts per-request [`crate::RequestOptions`].
    pub async fn create_with_options(
        &self,
        params: CreateParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<WorkflowTableListResponse, Error> {
        let path = "/v1/tables".to_string();
        let method = http::Method::POST;
        let __query = serde_json::to_value(&params)
            .map_err(|e| Error::Builder(format!("query encode failed: {e}")))?;
        let body = params.body;
        let mut __form_fields: Vec<crate::client::MultipartField> = Vec::new();
        __form_fields.push(crate::client::MultipartField::Text {
            name: "name".to_string(),
            value: body.name.to_string(),
        });
        __form_fields.push(crate::client::MultipartField::File {
            name: "file".to_string(),
            bytes: body.file,
        });
        if let Some(__v) = body.column_schema_overrides {
            __form_fields.push(crate::client::MultipartField::Text {
                name: "column_schema_overrides".to_string(),
                value: __v.to_string(),
            });
        }
        if let Some(__v) = body.project_id {
            __form_fields.push(crate::client::MultipartField::Text {
                name: "project_id".to_string(),
                value: __v.to_string(),
            });
        }
        self.client
            .request_multipart_opts(method, &path, &__query, __form_fields, options)
            .await
    }

    /// Table.Get
    pub async fn get(&self, table_id: &str) -> Result<WorkflowTableResponse, Error> {
        self.get_with_options(table_id, None).await
    }

    /// Variant of [`Self::get`] that accepts per-request [`crate::RequestOptions`].
    pub async fn get_with_options(
        &self,
        table_id: &str,
        options: Option<&crate::RequestOptions>,
    ) -> Result<WorkflowTableResponse, Error> {
        let table_id = crate::client::path_segment(table_id);
        let path = format!("/v1/tables/{table_id}");
        let method = http::Method::GET;
        self.client
            .request_with_query_opts(method, &path, &(), options)
            .await
    }

    /// Table.Replace
    pub async fn replace(
        &self,
        table_id: &str,
        params: ReplaceParams,
    ) -> Result<WorkflowTableListResponse, Error> {
        self.replace_with_options(table_id, params, None).await
    }

    /// Variant of [`Self::replace`] that accepts per-request [`crate::RequestOptions`].
    pub async fn replace_with_options(
        &self,
        table_id: &str,
        params: ReplaceParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<WorkflowTableListResponse, Error> {
        let table_id = crate::client::path_segment(table_id);
        let path = format!("/v1/tables/{table_id}");
        let method = http::Method::PUT;
        let __query = serde_json::to_value(&params)
            .map_err(|e| Error::Builder(format!("query encode failed: {e}")))?;
        let body = params.body;
        let mut __form_fields: Vec<crate::client::MultipartField> = Vec::new();
        __form_fields.push(crate::client::MultipartField::File {
            name: "file".to_string(),
            bytes: body.file,
        });
        if let Some(__v) = body.column_schema_overrides {
            __form_fields.push(crate::client::MultipartField::Text {
                name: "column_schema_overrides".to_string(),
                value: __v.to_string(),
            });
        }
        self.client
            .request_multipart_opts(method, &path, &__query, __form_fields, options)
            .await
    }

    /// Table.Update
    pub async fn update_table(
        &self,
        table_id: &str,
        params: UpdateTableParams,
    ) -> Result<WorkflowTableListResponse, Error> {
        self.update_table_with_options(table_id, params, None).await
    }

    /// Variant of [`Self::update_table`] that accepts per-request [`crate::RequestOptions`].
    pub async fn update_table_with_options(
        &self,
        table_id: &str,
        params: UpdateTableParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<WorkflowTableListResponse, Error> {
        let table_id = crate::client::path_segment(table_id);
        let path = format!("/v1/tables/{table_id}");
        let method = http::Method::PATCH;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }

    /// Table.Delete
    pub async fn delete(&self, table_id: &str) -> Result<(), Error> {
        self.delete_with_options(table_id, None).await
    }

    /// Variant of [`Self::delete`] that accepts per-request [`crate::RequestOptions`].
    pub async fn delete_with_options(
        &self,
        table_id: &str,
        options: Option<&crate::RequestOptions>,
    ) -> Result<(), Error> {
        let table_id = crate::client::path_segment(table_id);
        let path = format!("/v1/tables/{table_id}");
        let method = http::Method::DELETE;
        self.client
            .request_with_query_opts_empty(method, &path, &(), options)
            .await
    }

    /// Table.Download
    pub async fn download(&self, table_id: &str) -> Result<Vec<u8>, Error> {
        self.download_with_options(table_id, None).await
    }

    /// Variant of [`Self::download`] that accepts per-request [`crate::RequestOptions`].
    pub async fn download_with_options(
        &self,
        table_id: &str,
        options: Option<&crate::RequestOptions>,
    ) -> Result<Vec<u8>, Error> {
        let table_id = crate::client::path_segment(table_id);
        let path = format!("/v1/tables/{table_id}/download");
        let method = http::Method::GET;
        self.client
            .request_with_query_opts(method, &path, &(), options)
            .await
    }

    /// Table.Get Profile
    pub async fn profile(
        &self,
        table_id: &str,
        params: ProfileParams,
    ) -> Result<WorkflowTableProfileResponse, Error> {
        self.profile_with_options(table_id, params, None).await
    }

    /// Variant of [`Self::profile`] that accepts per-request [`crate::RequestOptions`].
    pub async fn profile_with_options(
        &self,
        table_id: &str,
        params: ProfileParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<WorkflowTableProfileResponse, Error> {
        let table_id = crate::client::path_segment(table_id);
        let path = format!("/v1/tables/{table_id}/profile");
        let method = http::Method::GET;
        self.client
            .request_with_query_opts(method, &path, &params, options)
            .await
    }

    /// Table.Query
    pub async fn query(
        &self,
        table_id: &str,
        params: QueryParams,
    ) -> Result<WorkflowTableRowsResponse, Error> {
        self.query_with_options(table_id, params, None).await
    }

    /// Variant of [`Self::query`] that accepts per-request [`crate::RequestOptions`].
    pub async fn query_with_options(
        &self,
        table_id: &str,
        params: QueryParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<WorkflowTableRowsResponse, Error> {
        let table_id = crate::client::path_segment(table_id);
        let path = format!("/v1/tables/{table_id}/query");
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, params.body.as_ref(), options)
            .await
    }

    /// Table.Get Schema
    pub async fn schema(&self, table_id: &str) -> Result<WorkflowTableSchemaResponse, Error> {
        self.schema_with_options(table_id, None).await
    }

    /// Variant of [`Self::schema`] that accepts per-request [`crate::RequestOptions`].
    pub async fn schema_with_options(
        &self,
        table_id: &str,
        options: Option<&crate::RequestOptions>,
    ) -> Result<WorkflowTableSchemaResponse, Error> {
        let table_id = crate::client::path_segment(table_id);
        let path = format!("/v1/tables/{table_id}/schema");
        let method = http::Method::GET;
        self.client
            .request_with_query_opts(method, &path, &(), options)
            .await
    }

    /// Table.Validate
    pub async fn validate(
        &self,
        table_id: &str,
        params: ValidateParams,
    ) -> Result<WorkflowTableValidationResponse, Error> {
        self.validate_with_options(table_id, params, None).await
    }

    /// Variant of [`Self::validate`] that accepts per-request [`crate::RequestOptions`].
    pub async fn validate_with_options(
        &self,
        table_id: &str,
        params: ValidateParams,
        options: Option<&crate::RequestOptions>,
    ) -> Result<WorkflowTableValidationResponse, Error> {
        let table_id = crate::client::path_segment(table_id);
        let path = format!("/v1/tables/{table_id}/validate");
        let method = http::Method::POST;
        self.client
            .request_with_body_opts(method, &path, &params, Some(&params.body), options)
            .await
    }
}
