ghl-sdk 0.5.1

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 v2 operations
//! in this module.
//!
//! Access via [`Ghl::oauth`](crate::Ghl::oauth).
//!
//! Request and response types come from [`ghl_models::v2::oauth`](https://docs.rs/ghl-models/latest/ghl_models/v2/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::v2::oauth as models;

/// Typed access to the `oauth` API v2 surface (3 operations). Obtained via
/// [`Ghl::oauth`](crate::Ghl::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 {
    /// Parameter to skip the number installed locations
    pub skip: Option<String>,
    /// Parameter to limit the number installed locations
    pub limit: 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>,
    /// 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()
        }
    }

    /// Parameter to skip the number installed locations
    pub fn skip(mut self, v: impl Into<String>) -> Self {
        self.skip = Some(v.into());
        self
    }

    /// Parameter to limit the number installed locations
    pub fn limit(mut self, v: impl Into<String>) -> Self {
        self.limit = 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
    }

    /// 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.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.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.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/installedLocations`
    ///
    /// Requires scope: `oauth.readonly`.
    pub async fn get_location_where_app_is_installed(
        &self,
        params: &GetLocationWhereAppIsInstalledParams,
    ) -> Result<models::GetInstalledLocationsSuccessfulResponseDto> {
        let query = params.to_query();
        self.client
            .send_versioned(
                reqwest::Method::GET,
                "/oauth/installedLocations",
                &query,
                None::<&()>,
                Some("2021-07-28"),
            )
            .await
    }

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

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