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.
//! `objects` — typed methods for all 9 API v2 operations
//! in this module.
//!
//! Access via [`Ghl::objects`](crate::Ghl::objects).
//!
//! Request and response types come from [`ghl_models::v2::objects`](https://docs.rs/ghl-models/latest/ghl_models/v2/objects/); every endpoint is also documented in the
//! [`objects` API reference](https://github.com/Shahroz/ghl-rs/blob/main/docs/api/objects.md).
//!
//! Enable with `features = ["objects"]`.

#![allow(clippy::too_many_arguments)]

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

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

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

/// Query parameters for [`ObjectsService::get_all_objects_for_a_location`].
#[derive(Debug, Clone, Default)]
pub struct GetAllObjectsForALocationParams {
    /// location id
    /// Required by the API.
    pub location_id: String,
}

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

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

/// Query parameters for [`ObjectsService::get_object_schema_by_key_id`].
#[derive(Debug, Clone, Default)]
pub struct GetObjectSchemaByKeyIdParams {
    /// location id of the sub account
    /// Required by the API.
    pub location_id: String,
    /// Fetch Properties , Fetches all the standard / custom fields of the object when set
    /// to true
    pub fetch_properties: Option<String>,
}

impl GetObjectSchemaByKeyIdParams {
    /// Start from the parameters the API requires.
    pub fn new(location_id: impl Into<String>) -> Self {
        Self {
            location_id: location_id.into(),
            ..Default::default()
        }
    }

    /// Fetch Properties , Fetches all the standard / custom fields of the object when set
    /// to true
    pub fn fetch_properties(mut self, v: impl Into<String>) -> Self {
        self.fetch_properties = Some(v.into());
        self
    }

    fn to_query(&self) -> Vec<(String, String)> {
        let mut q: Vec<(String, String)> = vec![("locationId".into(), self.location_id.clone())];
        if let Some(v) = &self.fetch_properties {
            q.push(("fetchProperties".into(), v.to_string()));
        }
        q
    }
}

/// Query parameters for [`ObjectsService::update_record`].
#[derive(Debug, Clone, Default)]
pub struct UpdateRecordParams {
    /// `locationId` query parameter.
    /// Required by the API.
    pub location_id: String,
}

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

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

impl ObjectsService {
    /// Get all objects for a location
    ///
    /// Get all objects for a location. Supported Objects are contact, opportunity, business
    /// and custom objects.To understand objects and records, please have a look at the
    /// documentation here :
    /// <https://doc.clickup.com/8631005/d/h/87cpx-277156/93bf0c2e23177b0>
    ///
    /// `GET /objects/`
    ///
    /// Requires scope: `objects/schema.readonly`.
    pub async fn get_all_objects_for_a_location(
        &self,
        params: &GetAllObjectsForALocationParams,
    ) -> Result<models::CustomObjectListResponseDTO> {
        let query = params.to_query();
        self.client
            .send_versioned(
                reqwest::Method::GET,
                "/objects/",
                &query,
                None::<&()>,
                Some("2021-07-28"),
            )
            .await
    }

    /// Create Custom Object
    ///
    /// Allows you to create a custom object schema. To understand objects and records,
    /// please have a look at the documentation here :
    /// <https://doc.clickup.com/8631005/d/h/87cpx-277156/93bf0c2e23177b0>
    ///
    /// `POST /objects/`
    ///
    /// Requires scope: `objects/schema.write`.
    pub async fn create_custom_object(
        &self,
        body: &models::CreateCustomObjectSchemaDTO,
    ) -> Result<models::CustomObjectResponseDTO> {
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::POST,
                "/objects/",
                &query,
                Some(body),
                Some("2021-07-28"),
            )
            .await
    }

    /// Get Object Schema by key / id
    ///
    /// Retrieve Object Schema by key or ID. This will return the schema of the custom
    /// object, including all its fields and properties. Supported objects include contact,
    /// opportunity, business and custom objects.To understand objects and records, please
    /// have a look the documentation here :
    /// <https://doc.clickup.com/8631005/d/h/87cpx-277156/93bf0c2e23177b0>
    ///
    /// `GET /objects/{key}`
    ///
    /// Requires scope: `objects/schema.readonly`.
    pub async fn get_object_schema_by_key_id(
        &self,
        key: &str,
        params: &GetObjectSchemaByKeyIdParams,
    ) -> Result<models::CustomObjectByIdResponseDTO> {
        let path = format!("/objects/{}", crate::services::encode(key));
        let query = params.to_query();
        self.client
            .send_versioned(
                reqwest::Method::GET,
                &path,
                &query,
                None::<&()>,
                Some("2021-07-28"),
            )
            .await
    }

    /// Update Object Schema By Key / Id
    ///
    /// Update Custom Object Schema or standard object's like contact, opportunity, business
    /// searchable fields. To understand objects and records, please have a look at the
    /// documentation here :
    /// <https://doc.clickup.com/8631005/d/h/87cpx-277156/93bf0c2e23177b0>
    ///
    /// `PUT /objects/{key}`
    ///
    /// Requires scope: `objects/schema.write`.
    pub async fn update_object_schema_by_key_id(
        &self,
        key: &str,
        body: &models::UpdateCustomObjectSchemaDTO,
    ) -> Result<models::CustomObjectResponseDTO> {
        let path = format!("/objects/{}", crate::services::encode(key));
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::PUT,
                &path,
                &query,
                Some(body),
                Some("2021-07-28"),
            )
            .await
    }

    /// Create Record
    ///
    /// Create a Custom Object Record. Supported Objects business and custom objects.
    /// Documentation Link -
    /// <https://doc.clickup.com/8631005/d/h/87cpx-277156/93bf0c2e23177b0/87cpx-376296>
    ///
    /// `POST /objects/{schemaKey}/records`
    ///
    /// Requires scope: `objects/record.write`.
    pub async fn create_record(
        &self,
        schema_key: &str,
        body: &models::CreateCustomObjectRecordDto,
    ) -> Result<models::RecordByIdResponseDTO> {
        let path = format!("/objects/{}/records", crate::services::encode(schema_key));
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::POST,
                &path,
                &query,
                Some(body),
                Some("2021-07-28"),
            )
            .await
    }

    /// Search Object Records
    ///
    /// Supported Objects are custom objects and standard objects like "business".
    /// Documentation Link -
    /// <https://doc.clickup.com/8631005/d/h/87cpx-277156/93bf0c2e23177b0/87cpx-379336>
    ///
    /// `POST /objects/{schemaKey}/records/search`
    ///
    /// Requires scope: `objects/record.readonly`.
    pub async fn search_object_records(
        &self,
        schema_key: &str,
        body: &models::SearchRecordsBody,
    ) -> Result<models::SearchRecordResponseDTO> {
        let path = format!(
            "/objects/{}/records/search",
            crate::services::encode(schema_key)
        );
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::POST,
                &path,
                &query,
                Some(body),
                Some("2021-07-28"),
            )
            .await
    }

    /// Delete Record
    ///
    /// Delete Record By Id . Supported Objects are business and custom objects.
    ///
    /// `DELETE /objects/{schemaKey}/records/{id}`
    pub async fn delete_record(
        &self,
        schema_key: &str,
        id: &str,
    ) -> Result<models::ObjectRecordDeleteResponseDTO> {
        let path = format!(
            "/objects/{}/records/{}",
            crate::services::encode(schema_key),
            crate::services::encode(id)
        );
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::DELETE,
                &path,
                &query,
                None::<&()>,
                Some("2021-07-28"),
            )
            .await
    }

    /// Get Record By Id
    ///
    /// Allows you to get a Standard Object like business and custom object record by Id
    ///
    /// `GET /objects/{schemaKey}/records/{id}`
    pub async fn get_record_by_id(
        &self,
        schema_key: &str,
        id: &str,
    ) -> Result<models::RecordByIdResponseDTO> {
        let path = format!(
            "/objects/{}/records/{}",
            crate::services::encode(schema_key),
            crate::services::encode(id)
        );
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::GET,
                &path,
                &query,
                None::<&()>,
                Some("2021-07-28"),
            )
            .await
    }

    /// Update Record
    ///
    /// Update a Custom Object Record by Id. Supported Objects are business and custom
    /// objects. Documentation Link -
    /// <https://doc.clickup.com/8631005/d/h/87cpx-277156/93bf0c2e23177b0/87cpx-376296>
    ///
    /// `PUT /objects/{schemaKey}/records/{id}`
    pub async fn update_record(
        &self,
        schema_key: &str,
        id: &str,
        params: &UpdateRecordParams,
        body: &models::UpdateCustomObjectRecordDto,
    ) -> Result<models::RecordByIdResponseDTO> {
        let path = format!(
            "/objects/{}/records/{}",
            crate::services::encode(schema_key),
            crate::services::encode(id)
        );
        let query = params.to_query();
        self.client
            .send_versioned(
                reqwest::Method::PUT,
                &path,
                &query,
                Some(body),
                Some("2021-07-28"),
            )
            .await
    }
}