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

#![allow(clippy::too_many_arguments)]

use crate::client::Ghl;
use crate::error::Result;
use ghl_models::v3::users as models;

/// Typed access to the `users` API v3 surface (6 operations). Obtained via
/// [`Ghl::v3`](crate::Ghl::v3)`().users()`.
#[derive(Debug, Clone)]
pub struct UsersService {
    pub(crate) client: Ghl,
}

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

/// Query parameters for [`UsersService::search_users`].
#[derive(Debug, Clone, Default)]
pub struct SearchUsersParams {
    /// Company ID in which the search needs to be performed
    /// Required by the API.
    pub company_id: String,
    /// The search term for the user is matched based on the user full name, email or phone
    pub query: Option<String>,
    /// No of results to be skipped before returning the result
    pub skip: Option<String>,
    /// No of results to be limited before returning the result
    pub limit: Option<String>,
    /// Location ID in which the search needs to be performed
    pub location_id: Option<String>,
    /// Type of the users to be filtered in the search
    pub type_: Option<String>,
    /// Role of the users to be filtered in the search
    pub role: Option<String>,
    /// List of User IDs to be filtered in the search
    pub ids: Option<String>,
    /// The field on which sort is applied in which the results need to be sorted. Default
    /// is based on the first and last name
    pub sort: Option<String>,
    /// The direction in which the results need to be sorted
    pub sort_direction: Option<String>,
    /// Filter users by whether 2-way sync is enabled
    pub enabled2way_sync: Option<bool>,
}

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

    /// The search term for the user is matched based on the user full name, email or phone
    pub fn query(mut self, v: impl Into<String>) -> Self {
        self.query = Some(v.into());
        self
    }

    /// No of results to be skipped before returning the result
    pub fn skip(mut self, v: impl Into<String>) -> Self {
        self.skip = Some(v.into());
        self
    }

    /// No of results to be limited before returning the result
    pub fn limit(mut self, v: impl Into<String>) -> Self {
        self.limit = Some(v.into());
        self
    }

    /// Location ID in which the search needs to be performed
    pub fn location_id(mut self, v: impl Into<String>) -> Self {
        self.location_id = Some(v.into());
        self
    }

    /// Type of the users to be filtered in the search
    pub fn type_(mut self, v: impl Into<String>) -> Self {
        self.type_ = Some(v.into());
        self
    }

    /// Role of the users to be filtered in the search
    pub fn role(mut self, v: impl Into<String>) -> Self {
        self.role = Some(v.into());
        self
    }

    /// List of User IDs to be filtered in the search
    pub fn ids(mut self, v: impl Into<String>) -> Self {
        self.ids = Some(v.into());
        self
    }

    /// The field on which sort is applied in which the results need to be sorted. Default
    /// is based on the first and last name
    pub fn sort(mut self, v: impl Into<String>) -> Self {
        self.sort = Some(v.into());
        self
    }

    /// The direction in which the results need to be sorted
    pub fn sort_direction(mut self, v: impl Into<String>) -> Self {
        self.sort_direction = Some(v.into());
        self
    }

    /// Filter users by whether 2-way sync is enabled
    pub fn enabled2way_sync(mut self, v: bool) -> Self {
        self.enabled2way_sync = Some(v);
        self
    }

    fn to_query(&self) -> Vec<(String, String)> {
        let mut q: Vec<(String, String)> = vec![("companyId".into(), self.company_id.clone())];
        if let Some(v) = &self.query {
            q.push(("query".into(), v.to_string()));
        }
        if let Some(v) = &self.skip {
            q.push(("skip".into(), v.to_string()));
        }
        if let Some(v) = &self.limit {
            q.push(("limit".into(), v.to_string()));
        }
        if let Some(v) = &self.location_id {
            q.push(("locationId".into(), v.to_string()));
        }
        if let Some(v) = &self.type_ {
            q.push(("type".into(), v.to_string()));
        }
        if let Some(v) = &self.role {
            q.push(("role".into(), v.to_string()));
        }
        if let Some(v) = &self.ids {
            q.push(("ids".into(), v.to_string()));
        }
        if let Some(v) = &self.sort {
            q.push(("sort".into(), v.to_string()));
        }
        if let Some(v) = &self.sort_direction {
            q.push(("sortDirection".into(), v.to_string()));
        }
        if let Some(v) = &self.enabled2way_sync {
            q.push(("enabled2waySync".into(), v.to_string()));
        }
        q
    }
}

impl UsersService {
    /// Create User
    ///
    /// `POST /users/`
    ///
    /// Requires scope: `users.write`.
    pub async fn create_user(
        &self,
        body: &models::CreateUserDto,
    ) -> Result<models::UserSuccessfulResponseDto> {
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::POST,
                "/users/",
                &query,
                Some(body),
                Some("v3"),
            )
            .await
    }

    /// Search Users
    ///
    /// `GET /users/search`
    ///
    /// Requires scope: `users.readonly`.
    pub async fn search_users(
        &self,
        params: &SearchUsersParams,
    ) -> Result<models::SearchUserSuccessfulResponseDto> {
        let query = params.to_query();
        self.client
            .send_versioned(
                reqwest::Method::GET,
                "/users/search",
                &query,
                None::<&()>,
                Some("v3"),
            )
            .await
    }

    /// Filter Users by Email
    ///
    /// Filter users by company ID, deleted status, and email array
    ///
    /// `POST /users/search/filter-by-email`
    ///
    /// Requires scope: `users.readonly`.
    pub async fn filter_users_by_email(
        &self,
        body: &models::FilterByEmailDto,
    ) -> Result<models::SearchUserSuccessfulResponseDto> {
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::POST,
                "/users/search/filter-by-email",
                &query,
                Some(body),
                Some("v3"),
            )
            .await
    }

    /// Delete User
    ///
    /// `DELETE /users/{userId}`
    ///
    /// Requires scope: `users.write`.
    pub async fn delete_user(
        &self,
        user_id: &str,
    ) -> Result<models::DeleteUserSuccessfulResponseV3Dto> {
        let path = format!("/users/{}", crate::services::encode(user_id));
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::DELETE,
                &path,
                &query,
                None::<&()>,
                Some("v3"),
            )
            .await
    }

    /// Get User
    ///
    /// `GET /users/{userId}`
    ///
    /// Requires scope: `users.readonly`.
    pub async fn get_user(&self, user_id: &str) -> Result<models::UserSuccessfulResponseDto> {
        let path = format!("/users/{}", crate::services::encode(user_id));
        let query = Vec::new();
        self.client
            .send_versioned(reqwest::Method::GET, &path, &query, None::<&()>, Some("v3"))
            .await
    }

    /// Update User
    ///
    /// `PUT /users/{userId}`
    ///
    /// Requires scope: `users.write`.
    pub async fn update_user(
        &self,
        user_id: &str,
        body: &models::UpdateUserDto,
    ) -> Result<models::UserSuccessfulResponseDto> {
        let path = format!("/users/{}", crate::services::encode(user_id));
        let query = Vec::new();
        self.client
            .send_versioned(reqwest::Method::PUT, &path, &query, Some(body), Some("v3"))
            .await
    }
}