ghl-sdk 0.1.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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! The core HTTP client: request building, auth, retries, and rate-limit tracking.

use std::sync::atomic::{AtomicI64, Ordering};
use std::sync::Arc;
use std::time::Duration;

use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION};
use reqwest::{Method, StatusCode};
use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::auth::Auth;
use crate::contacts::ContactsService;
use crate::error::{Error, Result};
use crate::locations::LocationsService;
use crate::opportunities::OpportunitiesService;

/// Production API base URL.
pub const DEFAULT_BASE_URL: &str = "https://services.leadconnectorhq.com";

/// The `Version` header value used by most API 2.0 modules.
pub const API_VERSION: &str = "2021-07-28";

const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
const DEFAULT_MAX_RETRIES: u32 = 3;
const BACKOFF_BASE: Duration = Duration::from_millis(500);
const BACKOFF_CAP: Duration = Duration::from_secs(8);

/// Async client for the GoHighLevel API 2.0.
///
/// Cloning is cheap (`Arc` internally); share one client across tasks.
///
/// ```no_run
/// # async fn demo() -> Result<(), ghl_sdk::Error> {
/// // From the environment (GHL_PIT_TOKEN or GHL_ACCESS_TOKEN, optional GHL_BASE_URL):
/// let ghl = ghl_sdk::Ghl::from_env()?;
/// // Or explicitly:
/// let ghl = ghl_sdk::Ghl::builder()
///     .private_integration_token("pit-…")
///     .build()?;
/// # Ok(()) }
/// ```
#[derive(Clone)]
pub struct Ghl {
    inner: Arc<Inner>,
}

struct Inner {
    http: reqwest::Client,
    base_url: String,
    auth: Auth,
    max_retries: u32,
    /// Last-seen `X-RateLimit-Remaining` (burst window); -1 = unknown.
    rate_remaining: AtomicI64,
    /// Last-seen `X-RateLimit-Daily-Remaining`; -1 = unknown.
    rate_daily_remaining: AtomicI64,
}

impl std::fmt::Debug for Ghl {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Ghl")
            .field("base_url", &self.inner.base_url)
            .field("auth", &self.inner.auth)
            .finish_non_exhaustive()
    }
}

/// Builder for [`Ghl`].
#[derive(Default)]
pub struct GhlBuilder {
    base_url: Option<String>,
    auth: Option<Auth>,
    timeout: Option<Duration>,
    max_retries: Option<u32>,
}

impl GhlBuilder {
    /// Override the API base URL (e.g. for tests or a proxy).
    pub fn base_url(mut self, url: impl Into<String>) -> Self {
        self.base_url = Some(url.into());
        self
    }

    /// Authenticate with a Private Integration Token (`pit-…`).
    pub fn private_integration_token(mut self, token: impl Into<String>) -> Self {
        self.auth = Some(Auth::private_integration(token));
        self
    }

    /// Authenticate with a pre-obtained OAuth access token (no refresh).
    pub fn access_token(mut self, token: impl Into<String>) -> Self {
        self.auth = Some(Auth::access_token(token));
        self
    }

    /// Authenticate with any [`Auth`] variant (e.g. full OAuth with refresh).
    pub fn auth(mut self, auth: Auth) -> Self {
        self.auth = Some(auth);
        self
    }

    /// Per-request timeout (default 30s).
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Maximum retry attempts after the initial request (default 3).
    pub fn max_retries(mut self, retries: u32) -> Self {
        self.max_retries = Some(retries);
        self
    }

    /// Finalize the client. Errors if no credentials were provided.
    pub fn build(self) -> Result<Ghl> {
        let auth = self.auth.ok_or_else(|| {
            Error::Config(
                "no credentials configured — call `.private_integration_token(…)`, \
                 `.access_token(…)`, or `.auth(…)`"
                    .into(),
            )
        })?;
        let base_url = self
            .base_url
            .unwrap_or_else(|| DEFAULT_BASE_URL.to_owned())
            .trim_end_matches('/')
            .to_owned();
        let http = reqwest::Client::builder()
            .timeout(self.timeout.unwrap_or(DEFAULT_TIMEOUT))
            .user_agent(concat!("ghl-sdk/", env!("CARGO_PKG_VERSION")))
            .build()?;
        Ok(Ghl {
            inner: Arc::new(Inner {
                http,
                base_url,
                auth,
                max_retries: self.max_retries.unwrap_or(DEFAULT_MAX_RETRIES),
                rate_remaining: AtomicI64::new(-1),
                rate_daily_remaining: AtomicI64::new(-1),
            }),
        })
    }
}

/// A snapshot of the most recently observed rate-limit headers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RateStatus {
    /// Remaining requests in the current burst window (100 req / 10s), if known.
    pub burst_remaining: Option<i64>,
    /// Remaining requests today (200k/day), if known.
    pub daily_remaining: Option<i64>,
}

impl Ghl {
    /// Start configuring a client.
    pub fn builder() -> GhlBuilder {
        GhlBuilder::default()
    }

    /// Build a client from environment variables.
    ///
    /// - `GHL_PIT_TOKEN` — Private Integration Token, **or**
    /// - `GHL_ACCESS_TOKEN` — OAuth access token
    /// - `GHL_BASE_URL` — optional base-URL override
    pub fn from_env() -> Result<Self> {
        let mut builder = Ghl::builder();
        if let Ok(url) = std::env::var("GHL_BASE_URL") {
            builder = builder.base_url(url);
        }
        if let Ok(token) = std::env::var("GHL_PIT_TOKEN") {
            builder = builder.private_integration_token(token);
        } else if let Ok(token) = std::env::var("GHL_ACCESS_TOKEN") {
            builder = builder.access_token(token);
        } else {
            return Err(Error::Config(
                "set GHL_PIT_TOKEN (or GHL_ACCESS_TOKEN) in the environment, \
                 or use `Ghl::builder()` to pass credentials as parameters"
                    .into(),
            ));
        }
        builder.build()
    }

    /// Contacts API.
    pub fn contacts(&self) -> ContactsService {
        ContactsService::new(self.clone())
    }

    /// Locations (sub-accounts) API.
    pub fn locations(&self) -> LocationsService {
        LocationsService::new(self.clone())
    }

    /// Opportunities (pipeline deals) API.
    pub fn opportunities(&self) -> OpportunitiesService {
        OpportunitiesService::new(self.clone())
    }

    /// The most recently observed rate-limit headroom.
    pub fn rate_status(&self) -> RateStatus {
        let read = |a: &AtomicI64| {
            let v = a.load(Ordering::Relaxed);
            (v >= 0).then_some(v)
        };
        RateStatus {
            burst_remaining: read(&self.inner.rate_remaining),
            daily_remaining: read(&self.inner.rate_daily_remaining),
        }
    }

    /// Exchange an **agency (Company) token** for a **location token**
    /// (`POST /oauth/locationToken`) and return a new client scoped to that location.
    ///
    /// This is the multi-tenant primitive: one agency credential, many sub-accounts.
    pub async fn as_location(&self, company_id: &str, location_id: &str) -> Result<Ghl> {
        #[derive(serde::Deserialize)]
        struct LocationTokenResponse {
            access_token: String,
        }

        let bearer = self
            .inner
            .auth
            .bearer(&self.inner.http, &self.inner.base_url)
            .await?;
        let response = self
            .inner
            .http
            .post(format!("{}/oauth/locationToken", self.inner.base_url))
            .header(AUTHORIZATION, format!("Bearer {bearer}"))
            .header("Version", API_VERSION)
            .form(&[("companyId", company_id), ("locationId", location_id)])
            .send()
            .await?;

        let status = response.status();
        if !status.is_success() {
            let body = response.text().await.unwrap_or_default();
            return Err(Error::Auth(format!(
                "location token exchange failed ({status}): {body}"
            )));
        }
        let parsed: LocationTokenResponse = response
            .json()
            .await
            .map_err(|e| Error::Auth(format!("unexpected locationToken response: {e}")))?;

        Ghl::builder()
            .base_url(&self.inner.base_url)
            .access_token(parsed.access_token)
            .max_retries(self.inner.max_retries)
            .build()
    }

    // ----- raw escape hatches (for endpoints not yet typed) -----

    /// `GET` any API path and return the raw JSON.
    pub async fn get_raw(&self, path: &str, query: &[(&str, &str)]) -> Result<serde_json::Value> {
        let query: Vec<(String, String)> = query
            .iter()
            .map(|(k, v)| ((*k).to_owned(), (*v).to_owned()))
            .collect();
        self.send(Method::GET, path, &query, None::<&()>).await
    }

    /// `POST` any API path with a JSON body and return the raw JSON.
    pub async fn post_raw(&self, path: &str, body: &impl Serialize) -> Result<serde_json::Value> {
        self.send(Method::POST, path, &[], Some(body)).await
    }

    // ----- request core -----

    pub(crate) async fn send<T: DeserializeOwned>(
        &self,
        method: Method,
        path: &str,
        query: &[(String, String)],
        body: Option<&impl Serialize>,
    ) -> Result<T> {
        // Serialize the body once so retries don't re-serialize (and can't observe drift).
        let body = match body {
            Some(b) => Some(serde_json::to_value(b).map_err(|source| Error::Decode {
                endpoint: path.to_owned(),
                source,
            })?),
            None => None,
        };

        let url = format!("{}{}", self.inner.base_url, path);
        let idempotent = matches!(
            method,
            Method::GET | Method::PUT | Method::DELETE | Method::HEAD
        );
        let mut attempt: u32 = 0;

        loop {
            let bearer = self
                .inner
                .auth
                .bearer(&self.inner.http, &self.inner.base_url)
                .await?;

            let mut request = self
                .inner
                .http
                .request(method.clone(), &url)
                .header(AUTHORIZATION, format!("Bearer {bearer}"))
                .header("Version", API_VERSION)
                .header(reqwest::header::ACCEPT, "application/json");
            if !query.is_empty() {
                request = request.query(query);
            }
            if let Some(ref b) = body {
                request = request.json(b);
            }

            let outcome = request.send().await;

            match outcome {
                Ok(response) => {
                    self.record_rate_headers(response.headers());
                    let status = response.status();

                    if status.is_success() {
                        let bytes = response.bytes().await?;
                        return serde_json::from_slice(&bytes).map_err(|source| Error::Decode {
                            endpoint: path.to_owned(),
                            source,
                        });
                    }

                    let retry_after = parse_retry_after(response.headers());
                    let request_id = response
                        .headers()
                        .get("x-request-id")
                        .and_then(|v| v.to_str().ok())
                        .map(str::to_owned);
                    let message = read_api_message(response).await;

                    let retryable = status == StatusCode::TOO_MANY_REQUESTS
                        || (idempotent && status.is_server_error());
                    if retryable && attempt < self.inner.max_retries {
                        let delay = retry_after.unwrap_or_else(|| backoff_delay(attempt));
                        tracing::warn!(
                            %status, attempt, delay_ms = delay.as_millis() as u64, path,
                            "GoHighLevel request failed; retrying"
                        );
                        tokio::time::sleep(delay).await;
                        attempt += 1;
                        continue;
                    }

                    return Err(if status == StatusCode::TOO_MANY_REQUESTS {
                        Error::RateLimited { retry_after }
                    } else {
                        Error::Api {
                            status,
                            message,
                            request_id,
                        }
                    });
                }
                Err(err) => {
                    // Connection-level failures: retry idempotent requests only.
                    if idempotent && attempt < self.inner.max_retries && err.status().is_none() {
                        let delay = backoff_delay(attempt);
                        tracing::warn!(
                            error = %err, attempt, delay_ms = delay.as_millis() as u64, path,
                            "transport error; retrying"
                        );
                        tokio::time::sleep(delay).await;
                        attempt += 1;
                        continue;
                    }
                    return Err(err.into());
                }
            }
        }
    }

    fn record_rate_headers(&self, headers: &HeaderMap) {
        let parse =
            |name: &str| -> Option<i64> { headers.get(name)?.to_str().ok()?.trim().parse().ok() };
        if let Some(v) = parse("x-ratelimit-remaining") {
            self.inner.rate_remaining.store(v, Ordering::Relaxed);
        }
        if let Some(v) = parse("x-ratelimit-daily-remaining") {
            self.inner.rate_daily_remaining.store(v, Ordering::Relaxed);
        }
    }
}

/// Exponential backoff with full jitter, capped.
fn backoff_delay(attempt: u32) -> Duration {
    let exp = BACKOFF_BASE.saturating_mul(2u32.saturating_pow(attempt));
    let cap = exp.min(BACKOFF_CAP);
    cap.mul_f64(0.5 + fastrand::f64() * 0.5)
}

fn parse_retry_after(headers: &HeaderMap) -> Option<Duration> {
    let value: &HeaderValue = headers.get(reqwest::header::RETRY_AFTER)?;
    let seconds: u64 = value.to_str().ok()?.trim().parse().ok()?;
    Some(Duration::from_secs(seconds))
}

/// Pull a human-readable message out of a GoHighLevel error body.
/// Bodies are typically `{"message": "…"}` or `{"message": ["…", "…"]}`.
async fn read_api_message(response: reqwest::Response) -> String {
    let text = response.text().await.unwrap_or_default();
    match serde_json::from_str::<serde_json::Value>(&text) {
        Ok(v) => match v.get("message") {
            Some(serde_json::Value::String(s)) => s.clone(),
            Some(serde_json::Value::Array(parts)) => parts
                .iter()
                .filter_map(|p| p.as_str())
                .collect::<Vec<_>>()
                .join("; "),
            _ => text,
        },
        Err(_) => text,
    }
}