ghl-sdk 0.5.0

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

#![allow(clippy::too_many_arguments)]

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

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

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

/// Query parameters for [`OauthService::get_location_where_app_is_installed`].
#[derive(Debug, Clone, Default)]
pub struct GetLocationWhereAppIsInstalledParams {
    /// Max items per page (1-100). Replaces legacy `limit` parameter per AIP-158.
    pub page_size: Option<f64>,
    /// Opaque token returned in a previous response to fetch the next page. Replaces legacy
    /// `skip` parameter per AIP-158.
    pub page_token: Option<String>,
    /// Parameter to search for the installed location by name
    pub query: Option<String>,
    /// Filters out location which are installed for specified app under the specified
    /// company
    pub is_installed: Option<bool>,
    /// When true, restricts the list to locations the current user has access to (for
    /// restricted agency admins and account admins). When false or omitted, no user-based
    /// filter is applied for installed list; for backward compatibility, install list
    /// (isInstalled=false) is still filtered by user when this param is omitted.
    pub restrict_to_user_locations: Option<bool>,
    /// Parameter to search by the companyId
    /// Required by the API.
    pub company_id: String,
    /// Parameter to search by the appId
    /// Required by the API.
    pub app_id: String,
    /// VersionId of the app
    pub version_id: Option<String>,
    /// Filters out locations which are installed for specified app in trial mode
    pub on_trial: Option<bool>,
    /// Filters out location which are installed for specified app under the specified
    /// planId
    pub plan_id: Option<String>,
    /// locationId
    pub location_id: Option<String>,
}

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

    /// Max items per page (1-100). Replaces legacy `limit` parameter per AIP-158.
    pub fn page_size(mut self, v: f64) -> Self {
        self.page_size = Some(v);
        self
    }

    /// Opaque token returned in a previous response to fetch the next page. Replaces legacy
    /// `skip` parameter per AIP-158.
    pub fn page_token(mut self, v: impl Into<String>) -> Self {
        self.page_token = Some(v.into());
        self
    }

    /// Parameter to search for the installed location by name
    pub fn query(mut self, v: impl Into<String>) -> Self {
        self.query = Some(v.into());
        self
    }

    /// Filters out location which are installed for specified app under the specified
    /// company
    pub fn is_installed(mut self, v: bool) -> Self {
        self.is_installed = Some(v);
        self
    }

    /// When true, restricts the list to locations the current user has access to (for
    /// restricted agency admins and account admins). When false or omitted, no user-based
    /// filter is applied for installed list; for backward compatibility, install list
    /// (isInstalled=false) is still filtered by user when this param is omitted.
    pub fn restrict_to_user_locations(mut self, v: bool) -> Self {
        self.restrict_to_user_locations = Some(v);
        self
    }

    /// VersionId of the app
    pub fn version_id(mut self, v: impl Into<String>) -> Self {
        self.version_id = Some(v.into());
        self
    }

    /// Filters out locations which are installed for specified app in trial mode
    pub fn on_trial(mut self, v: bool) -> Self {
        self.on_trial = Some(v);
        self
    }

    /// Filters out location which are installed for specified app under the specified
    /// planId
    pub fn plan_id(mut self, v: impl Into<String>) -> Self {
        self.plan_id = Some(v.into());
        self
    }

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

    fn to_query(&self) -> Vec<(String, String)> {
        let mut q: Vec<(String, String)> = vec![
            ("companyId".into(), self.company_id.clone()),
            ("appId".into(), self.app_id.clone()),
        ];
        if let Some(v) = &self.page_size {
            q.push(("pageSize".into(), v.to_string()));
        }
        if let Some(v) = &self.page_token {
            q.push(("pageToken".into(), v.to_string()));
        }
        if let Some(v) = &self.query {
            q.push(("query".into(), v.to_string()));
        }
        if let Some(v) = &self.is_installed {
            q.push(("isInstalled".into(), v.to_string()));
        }
        if let Some(v) = &self.restrict_to_user_locations {
            q.push(("restrictToUserLocations".into(), v.to_string()));
        }
        if let Some(v) = &self.version_id {
            q.push(("versionId".into(), v.to_string()));
        }
        if let Some(v) = &self.on_trial {
            q.push(("onTrial".into(), v.to_string()));
        }
        if let Some(v) = &self.plan_id {
            q.push(("planId".into(), v.to_string()));
        }
        if let Some(v) = &self.location_id {
            q.push(("locationId".into(), v.to_string()));
        }
        q
    }
}

impl OauthService {
    /// Get Location where app is installed
    ///
    /// This API allows you fetch location where app is installed upon
    ///
    /// `GET /oauth/installed-locations`
    ///
    /// Requires scope: `oauth.readonly`.
    pub async fn get_location_where_app_is_installed(
        &self,
        params: &GetLocationWhereAppIsInstalledParams,
    ) -> Result<models::GetInstalledLocationsV3SuccessfulResponseDto> {
        let query = params.to_query();
        self.client
            .send_versioned(
                reqwest::Method::GET,
                "/oauth/installed-locations",
                &query,
                None::<&()>,
                Some("v3"),
            )
            .await
    }

    /// Get Location Access Token from Agency Token
    ///
    /// This API allows you to generate locationAccessToken from AgencyAccessToken
    ///
    /// `POST /oauth/location-token`
    ///
    /// Requires scope: `oauth.write`.
    pub async fn get_location_access_token_from_agency_token(
        &self,
        body: &serde_json::Value,
    ) -> Result<models::GetLocationAccessTokenV3SuccessfulResponseDto> {
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::POST,
                "/oauth/location-token",
                &query,
                Some(body),
                Some("v3"),
            )
            .await
    }

    /// Get Access Token
    ///
    /// Use Access Tokens to access CRM resources on behalf of an authenticated
    /// location/company.
    ///
    /// `POST /oauth/token`
    pub async fn get_access_token(
        &self,
        body: &models::GetAccessTokenBodyDto,
    ) -> Result<models::GetAccessTokenSuccessfulResponseDto> {
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::POST,
                "/oauth/token",
                &query,
                Some(body),
                Some("v3"),
            )
            .await
    }
}