ghl-sdk 0.5.2

Unofficial async Rust SDK for the GoHighLevel (HighLevel) API 2.0 — OAuth 2.0, Private Integration Tokens, rate-limit-aware retries, paginated streams
Documentation
// @generated by xtask/generate_services.py — do not edit by hand.
//! `medias` — typed methods for all 7 API v2 operations
//! in this module.
//!
//! Access via [`Ghl::medias`](crate::Ghl::medias).
//!
//! Request and response types come from [`ghl_models::v2::medias`](https://docs.rs/ghl-models/latest/ghl_models/v2/medias/); every endpoint is also documented in the
//! [`medias` API reference](https://github.com/Shahroz/ghl-rs/blob/main/docs/api/medias.md).
//!
//! Enable with `features = ["medias"]`.

#![allow(clippy::too_many_arguments)]

use crate::client::Ghl;
use crate::error::Result;
use ghl_models::v2::medias as models;

/// Typed access to the `medias` API v2 surface (7 operations). Obtained via
/// [`Ghl::medias`](crate::Ghl::medias).
#[derive(Debug, Clone)]
pub struct MediasService {
    pub(crate) client: Ghl,
}

impl MediasService {
    pub(crate) fn new(client: Ghl) -> Self {
        Self { client }
    }
}

/// Query parameters for [`MediasService::get_list_of_files_folders`].
#[derive(Debug, Clone, Default)]
pub struct GetListOfFilesFoldersParams {
    /// Number of files to skip in listing
    pub offset: Option<String>,
    /// Number of files to show in the listing
    pub limit: Option<String>,
    /// Field to sorting the file listing by
    /// Required by the API.
    pub sort_by: String,
    /// Direction in which file needs to be sorted
    /// Required by the API.
    pub sort_order: String,
    /// Type
    /// Required by the API.
    pub type_: String,
    /// Query text
    pub query: Option<String>,
    /// AltType
    /// Allowed values: `location`.
    /// Required by the API.
    pub alt_type: String,
    /// location Id
    /// Required by the API.
    pub alt_id: String,
    /// parent id or folder id
    pub parent_id: Option<String>,
    /// Fetch all files or folders
    pub fetch_all: Option<String>,
}

impl GetListOfFilesFoldersParams {
    /// Start from the parameters the API requires.
    pub fn new(
        sort_by: impl Into<String>,
        sort_order: impl Into<String>,
        type_: impl Into<String>,
        alt_type: impl Into<String>,
        alt_id: impl Into<String>,
    ) -> Self {
        Self {
            sort_by: sort_by.into(),
            sort_order: sort_order.into(),
            type_: type_.into(),
            alt_type: alt_type.into(),
            alt_id: alt_id.into(),
            ..Default::default()
        }
    }

    /// Number of files to skip in listing
    pub fn offset(mut self, v: impl Into<String>) -> Self {
        self.offset = Some(v.into());
        self
    }

    /// Number of files to show in the listing
    pub fn limit(mut self, v: impl Into<String>) -> Self {
        self.limit = Some(v.into());
        self
    }

    /// Query text
    pub fn query(mut self, v: impl Into<String>) -> Self {
        self.query = Some(v.into());
        self
    }

    /// parent id or folder id
    pub fn parent_id(mut self, v: impl Into<String>) -> Self {
        self.parent_id = Some(v.into());
        self
    }

    /// Fetch all files or folders
    pub fn fetch_all(mut self, v: impl Into<String>) -> Self {
        self.fetch_all = Some(v.into());
        self
    }

    fn to_query(&self) -> Vec<(String, String)> {
        let mut q: Vec<(String, String)> = vec![
            ("sortBy".into(), self.sort_by.clone()),
            ("sortOrder".into(), self.sort_order.clone()),
            ("type".into(), self.type_.clone()),
            ("altType".into(), self.alt_type.clone()),
            ("altId".into(), self.alt_id.clone()),
        ];
        if let Some(v) = &self.offset {
            q.push(("offset".into(), v.to_string()));
        }
        if let Some(v) = &self.limit {
            q.push(("limit".into(), v.to_string()));
        }
        if let Some(v) = &self.query {
            q.push(("query".into(), v.to_string()));
        }
        if let Some(v) = &self.parent_id {
            q.push(("parentId".into(), v.to_string()));
        }
        if let Some(v) = &self.fetch_all {
            q.push(("fetchAll".into(), v.to_string()));
        }
        q
    }
}

/// Query parameters for [`MediasService::delete_file_or_folder`].
#[derive(Debug, Clone, Default)]
pub struct DeleteFileOrFolderParams {
    /// AltType
    /// Allowed values: `location`.
    /// Required by the API.
    pub alt_type: String,
    /// location Id
    /// Required by the API.
    pub alt_id: String,
}

impl DeleteFileOrFolderParams {
    /// Start from the parameters the API requires.
    pub fn new(alt_type: impl Into<String>, alt_id: impl Into<String>) -> Self {
        Self {
            alt_type: alt_type.into(),
            alt_id: alt_id.into(),
        }
    }

    fn to_query(&self) -> Vec<(String, String)> {
        let q: Vec<(String, String)> = vec![
            ("altType".into(), self.alt_type.clone()),
            ("altId".into(), self.alt_id.clone()),
        ];
        q
    }
}

impl MediasService {
    /// Bulk Delete / Trash Files or Folders
    ///
    /// Soft-deletes or trashes multiple files and folders in a single request
    ///
    /// `PUT /medias/delete-files`
    pub async fn bulk_delete_trash_files_or_folders(
        &self,
        body: &models::DeleteMediaObjectsBodyParams,
    ) -> Result<serde_json::Value> {
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::PUT,
                "/medias/delete-files",
                &query,
                Some(body),
                Some("2021-07-28"),
            )
            .await
    }

    /// Get List of Files/ Folders
    ///
    /// Fetches list of files and folders from the media storage
    ///
    /// `GET /medias/files`
    ///
    /// Requires scope: `medias.readonly`.
    pub async fn get_list_of_files_folders(
        &self,
        params: &GetListOfFilesFoldersParams,
    ) -> Result<models::GetFilesResponseDTO> {
        let query = params.to_query();
        self.client
            .send_versioned(
                reqwest::Method::GET,
                "/medias/files",
                &query,
                None::<&()>,
                Some("2021-07-28"),
            )
            .await
    }

    /// Create Folder
    ///
    /// Creates a new folder in the media storage
    ///
    /// `POST /medias/folder`
    pub async fn create_folder(
        &self,
        body: &models::CreateFolderParams,
    ) -> Result<models::FolderDTO> {
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::POST,
                "/medias/folder",
                &query,
                Some(body),
                Some("2021-07-28"),
            )
            .await
    }

    /// Bulk Update Files/ Folders
    ///
    /// Updates metadata or status of multiple files and folders
    ///
    /// `PUT /medias/update-files`
    pub async fn bulk_update_files_folders(
        &self,
        body: &models::UpdateMediaObjects,
    ) -> Result<serde_json::Value> {
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::PUT,
                "/medias/update-files",
                &query,
                Some(body),
                Some("2021-07-28"),
            )
            .await
    }

    /// Upload File into Media Storage
    ///
    /// If hosted is set to true then fileUrl is required. Else file is required. If adding
    /// a file, maximum allowed is 25 MB
    ///
    /// `POST /medias/upload-file`
    ///
    /// Requires scope: `medias.write`.
    pub async fn upload_file_into_media_storage(
        &self,
        body: &serde_json::Value,
    ) -> Result<models::UploadFileResponseDTO> {
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::POST,
                "/medias/upload-file",
                &query,
                Some(body),
                Some("2021-07-28"),
            )
            .await
    }

    /// Delete File or Folder
    ///
    /// Deletes specific file or folder from the media storage
    ///
    /// `DELETE /medias/{id}`
    ///
    /// Requires scope: `medias.write`.
    pub async fn delete_file_or_folder(
        &self,
        id: &str,
        params: &DeleteFileOrFolderParams,
    ) -> Result<serde_json::Value> {
        let path = format!("/medias/{}", crate::services::encode(id));
        let query = params.to_query();
        self.client
            .send_versioned(
                reqwest::Method::DELETE,
                &path,
                &query,
                None::<&()>,
                Some("2021-07-28"),
            )
            .await
    }

    /// Update File/ Folder
    ///
    /// Updates a single file or folder by ID
    ///
    /// `POST /medias/{id}`
    pub async fn update_file_folder(
        &self,
        id: &str,
        body: &models::UpdateObject,
    ) -> Result<serde_json::Value> {
        let path = format!("/medias/{}", crate::services::encode(id));
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::POST,
                &path,
                &query,
                Some(body),
                Some("2021-07-28"),
            )
            .await
    }
}