mattermost-client 0.2.0

A sane Rust interface to the Mattermost API
Documentation
/*
 * Mattermost API Reference
 *
 * There is also a work-in-progress [Postman API reference](https://documenter.getpostman.com/view/4508214/RW8FERUn).
 *
 * The version of the OpenAPI document: 4.0.0
 * Contact: feedback@mattermost.com
 * Generated by: https://openapi-generator.tech
 */

use reqwest;

use super::ResponseContent;
use super::{Error, configuration};

/// struct for passing parameters to the method [`search_files`]
#[derive(Clone, Debug, Default)]
pub struct SearchFilesParams {
    /// Team GUID
    pub team_id: String,
    /// The search terms as inputed by the user. To search for files from a user include `from:someusername`, using a user's username. To search in a specific channel include `in:somechannel`, using the channel name (not the display name). To search for specific extensions included `ext:extension`.
    pub terms: String,
    /// Set to true if an Or search should be performed vs an And search.
    pub is_or_search: bool,
    /// Offset from UTC of user timezone for date searches.
    pub time_zone_offset: Option<i32>,
    /// Set to true if deleted channels should be included in the search. (archived channels)
    pub include_deleted_channels: Option<bool>,
    /// The page to select. (Only works with Elasticsearch)
    pub page: Option<i32>,
    /// The number of posts per page. (Only works with Elasticsearch)
    pub per_page: Option<i32>,
}

/// struct for typed errors of method [`search_files`]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum SearchFilesError {
    Status400(crate::openapi::models::AppError),
    Status401(crate::openapi::models::AppError),
    Status403(crate::openapi::models::AppError),
    UnknownValue(serde_json::Value),
}

/// Search for files in a team based on file name, extention and file content (if file content extraction is enabled and supported for the files). __Minimum server version__: 5.34 ##### Permissions Must be authenticated and have the `view_team` permission.
pub async fn search_files(
    configuration: &configuration::Configuration,
    params: SearchFilesParams,
) -> Result<crate::openapi::models::FileInfoList, Error<SearchFilesError>> {
    let local_var_configuration = configuration;

    // unbox the parameters
    let team_id = params.team_id;
    let terms = params.terms;
    let is_or_search = params.is_or_search;
    let time_zone_offset = params.time_zone_offset;
    let include_deleted_channels = params.include_deleted_channels;
    let page = params.page;
    let per_page = params.per_page;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!(
        "{}/api/v4/teams/{team_id}/files/search",
        local_var_configuration.base_path,
        team_id = super::urlencode(team_id)
    );
    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());
    };
    let mut local_var_form = reqwest::multipart::Form::new();
    local_var_form = local_var_form.text("terms", terms.to_string());
    local_var_form = local_var_form.text("is_or_search", is_or_search.to_string());
    if let Some(local_var_param_value) = time_zone_offset {
        local_var_form = local_var_form.text("time_zone_offset", local_var_param_value.to_string());
    }
    if let Some(local_var_param_value) = include_deleted_channels {
        local_var_form = local_var_form.text(
            "include_deleted_channels",
            local_var_param_value.to_string(),
        );
    }
    if let Some(local_var_param_value) = page {
        local_var_form = local_var_form.text("page", local_var_param_value.to_string());
    }
    if let Some(local_var_param_value) = per_page {
        local_var_form = local_var_form.text("per_page", local_var_param_value.to_string());
    }
    local_var_req_builder = local_var_req_builder.multipart(local_var_form);

    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_entity: Option<SearchFilesError> =
            serde_json::from_str(&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))
    }
}