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
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
//! Opportunities — pipeline deals, their stages, and status transitions.
//!
//! Access via [`Ghl::opportunities`](crate::Ghl::opportunities). See the
//! [full opportunities reference][ref] for all 12 v2 endpoints.
//!
//! | Method | Endpoint | Scope |
//! |---|---|---|
//! | [`OpportunitiesService::pipelines`] | `GET /opportunities/pipelines` | `opportunities.readonly` |
//! | [`OpportunitiesService::search`] | `GET /opportunities/search` | `opportunities.readonly` |
//! | [`OpportunitiesService::get`] | `GET /opportunities/{id}` | `opportunities.readonly` |
//! | [`OpportunitiesService::create`] | `POST /opportunities/` | `opportunities.write` |
//! | [`OpportunitiesService::update`] | `PUT /opportunities/{id}` | `opportunities.write` |
//! | [`OpportunitiesService::update_status`] | `PUT /opportunities/{id}/status` | `opportunities.write` |
//! | [`OpportunitiesService::delete`] | `DELETE /opportunities/{id}` | `opportunities.write` |
//!
//! Statuses are `open`, `won`, `lost`, and `abandoned`.
//!
//! # Examples
//!
//! Stage ids live on the pipeline, so fetch it first:
//!
//! ```no_run
//! # use ghl_sdk::{Ghl, opportunities::CreateOpportunity};
//! # async fn demo(ghl: Ghl, loc: String, contact_id: String) -> Result<(), ghl_sdk::Error> {
//! let pipelines = ghl.opportunities().pipelines(&loc).await?;
//! let pipeline = &pipelines[0];
//!
//! let deal = ghl.opportunities().create(CreateOpportunity {
//!     location_id: loc,
//!     pipeline_id: pipeline.id.clone(),
//!     name: "Acme — annual plan".into(),
//!     pipeline_stage_id: Some(pipeline.stages[0].id.clone()),
//!     monetary_value: Some(12_000.0),
//!     contact_id: Some(contact_id),
//!     ..Default::default()
//! }).await?;
//!
//! // Mark it won
//! ghl.opportunities().update_status(&deal.id, "won").await?;
//! # Ok(()) }
//! ```
//!
//! Search with filters, then page or stream:
//!
//! ```no_run
//! # use ghl_sdk::Ghl;
//! # async fn demo(ghl: Ghl, loc: &str) -> Result<(), ghl_sdk::Error> {
//! let page = ghl.opportunities().search(loc)
//!     .status("open")
//!     .limit(50)
//!     .page()
//!     .await?;
//! println!("{} open deals", page.opportunities.len());
//! # Ok(()) }
//! ```
//!
//! # A wire-format quirk
//!
//! Unlike the rest of the API, `GET /opportunities/search` takes **snake_case**
//! query parameters (`location_id`, `pipeline_id`). This module absorbs that for
//! you — [`SearchOpportunities`] sends the right names.
//!
//! [ref]: https://github.com/Shahroz/ghl-rs/blob/main/docs/api/opportunities.md

use futures_util::stream::{self, Stream, StreamExt, TryStreamExt};
use reqwest::Method;
use serde::{Deserialize, Serialize};

use crate::client::Ghl;
use crate::contacts::ListMeta;
use crate::error::Result;

/// A GoHighLevel opportunity (a deal in a pipeline).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(missing_docs)] // fields mirror the API wire format 1:1
pub struct Opportunity {
    pub id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub location_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pipeline_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pipeline_stage_id: Option<String>,
    /// One of `open`, `won`, `lost`, `abandoned`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub contact_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub monetary_value: Option<f64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub assigned_to: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub created_at: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<String>,
    /// Any fields this SDK doesn't model yet.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, serde_json::Value>,
}

/// Payload for [`OpportunitiesService::create`].
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "camelCase")]
#[allow(missing_docs)] // fields mirror the API wire format 1:1
pub struct CreateOpportunity {
    pub location_id: String,
    pub pipeline_id: String,
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pipeline_stage_id: Option<String>,
    /// One of `open`, `won`, `lost`, `abandoned` (defaults to `open` upstream).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contact_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub monetary_value: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub assigned_to: Option<String>,
}

/// Payload for [`OpportunitiesService::update`]. Only set fields are sent.
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "camelCase")]
#[allow(missing_docs)] // fields mirror the API wire format 1:1
pub struct UpdateOpportunity {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pipeline_stage_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub monetary_value: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub assigned_to: Option<String>,
}

/// A pipeline definition with its ordered stages.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(missing_docs)] // fields mirror the API wire format 1:1
pub struct Pipeline {
    pub id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub stages: Vec<PipelineStage>,
    /// Any fields this SDK doesn't model yet.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, serde_json::Value>,
}

/// One stage within a [`Pipeline`].
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(missing_docs)] // fields mirror the API wire format 1:1
pub struct PipelineStage {
    pub id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub position: Option<i64>,
    /// Any fields this SDK doesn't model yet.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, serde_json::Value>,
}

#[derive(Deserialize)]
struct OpportunityEnvelope {
    opportunity: Opportunity,
}

#[derive(Deserialize)]
struct PipelineList {
    #[serde(default)]
    pipelines: Vec<Pipeline>,
}

/// One page of opportunities plus cursor metadata.
#[derive(Debug, Clone, Deserialize)]
pub struct OpportunityPage {
    /// The opportunities on this page.
    #[serde(default)]
    pub opportunities: Vec<Opportunity>,
    /// Cursor metadata; `meta.start_after_id` feeds the next page request.
    #[serde(default)]
    pub meta: Option<ListMeta>,
}

/// Access to the Opportunities API. Obtained via [`Ghl::opportunities`].
pub struct OpportunitiesService {
    pub(crate) client: Ghl,
}

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

    /// `GET /opportunities/pipelines` — pipelines (and their stages) for a location.
    pub async fn pipelines(&self, location_id: &str) -> Result<Vec<Pipeline>> {
        let list: PipelineList = self
            .client
            .send(
                Method::GET,
                "/opportunities/pipelines",
                &[("locationId".into(), location_id.to_owned())],
                None::<&()>,
            )
            .await?;
        Ok(list.pipelines)
    }

    /// `POST /opportunities/`
    pub async fn create(&self, opportunity: CreateOpportunity) -> Result<Opportunity> {
        let envelope: OpportunityEnvelope = self
            .client
            .send(Method::POST, "/opportunities/", &[], Some(&opportunity))
            .await?;
        Ok(envelope.opportunity)
    }

    /// `GET /opportunities/{id}`
    pub async fn get(&self, opportunity_id: &str) -> Result<Opportunity> {
        let envelope: OpportunityEnvelope = self
            .client
            .send(
                Method::GET,
                &format!("/opportunities/{opportunity_id}"),
                &[],
                None::<&()>,
            )
            .await?;
        Ok(envelope.opportunity)
    }

    /// `PUT /opportunities/{id}`
    pub async fn update(
        &self,
        opportunity_id: &str,
        update: UpdateOpportunity,
    ) -> Result<Opportunity> {
        let envelope: OpportunityEnvelope = self
            .client
            .send(
                Method::PUT,
                &format!("/opportunities/{opportunity_id}"),
                &[],
                Some(&update),
            )
            .await?;
        Ok(envelope.opportunity)
    }

    /// `PUT /opportunities/{id}/status` — change only the status
    /// (`open` | `won` | `lost` | `abandoned`).
    pub async fn update_status(&self, opportunity_id: &str, status: &str) -> Result<()> {
        let _: serde_json::Value = self
            .client
            .send(
                Method::PUT,
                &format!("/opportunities/{opportunity_id}/status"),
                &[],
                Some(&serde_json::json!({ "status": status })),
            )
            .await?;
        Ok(())
    }

    /// `DELETE /opportunities/{id}`
    pub async fn delete(&self, opportunity_id: &str) -> Result<()> {
        let _: serde_json::Value = self
            .client
            .send(
                Method::DELETE,
                &format!("/opportunities/{opportunity_id}"),
                &[],
                None::<&()>,
            )
            .await?;
        Ok(())
    }

    /// `GET /opportunities/search` — returns a lazy request builder.
    pub fn search(&self, location_id: impl Into<String>) -> SearchOpportunities {
        SearchOpportunities {
            client: self.client.clone(),
            location_id: location_id.into(),
            limit: 20,
            query: None,
            pipeline_id: None,
            status: None,
            start_after_id: None,
            start_after: None,
        }
    }
}

/// Builder for searching opportunities. Finish with [`SearchOpportunities::page`]
/// (one page) or [`SearchOpportunities::stream`] (auto-pagination).
#[derive(Clone)]
pub struct SearchOpportunities {
    client: Ghl,
    location_id: String,
    limit: u32,
    query: Option<String>,
    pipeline_id: Option<String>,
    status: Option<String>,
    start_after_id: Option<String>,
    start_after: Option<i64>,
}

impl SearchOpportunities {
    /// Page size, 1–100 (API default 20).
    pub fn limit(mut self, limit: u32) -> Self {
        self.limit = limit.clamp(1, 100);
        self
    }

    /// Free-text search query.
    pub fn query(mut self, query: impl Into<String>) -> Self {
        self.query = Some(query.into());
        self
    }

    /// Restrict to one pipeline.
    pub fn pipeline_id(mut self, pipeline_id: impl Into<String>) -> Self {
        self.pipeline_id = Some(pipeline_id.into());
        self
    }

    /// Filter by status: `open` | `won` | `lost` | `abandoned` | `all`.
    pub fn status(mut self, status: impl Into<String>) -> Self {
        self.status = Some(status.into());
        self
    }

    /// Resume from a previous page's cursor.
    pub fn start_after_id(mut self, cursor: impl Into<String>) -> Self {
        self.start_after_id = Some(cursor.into());
        self
    }

    /// Fetch a single page.
    pub async fn page(&self) -> Result<OpportunityPage> {
        // This endpoint uses snake_case query params, unlike the rest of the API.
        let mut query: Vec<(String, String)> = vec![
            ("location_id".into(), self.location_id.clone()),
            ("limit".into(), self.limit.to_string()),
        ];
        if let Some(q) = &self.query {
            query.push(("q".into(), q.clone()));
        }
        if let Some(p) = &self.pipeline_id {
            query.push(("pipeline_id".into(), p.clone()));
        }
        if let Some(s) = &self.status {
            query.push(("status".into(), s.clone()));
        }
        if let Some(id) = &self.start_after_id {
            query.push(("startAfterId".into(), id.clone()));
        }
        if let Some(ts) = self.start_after {
            query.push(("startAfter".into(), ts.to_string()));
        }
        self.client
            .send(Method::GET, "/opportunities/search", &query, None::<&()>)
            .await
    }

    /// Auto-paginating stream of opportunities, following `meta.startAfterId` cursors.
    pub fn stream(self) -> impl Stream<Item = Result<Opportunity>> {
        stream::try_unfold(Some(self), |state| async move {
            let Some(mut request) = state else {
                return Ok::<_, crate::Error>(None);
            };
            let page = request.page().await?;
            let full_page = page.opportunities.len() as u32 >= request.limit;
            let cursor = page.meta.as_ref().and_then(|m| m.start_after_id.clone());
            let start_after = page.meta.as_ref().and_then(|m| m.start_after);

            let next = match (full_page, cursor) {
                (true, Some(cursor)) => {
                    request.start_after_id = Some(cursor);
                    request.start_after = start_after;
                    Some(request)
                }
                _ => None,
            };
            Ok(Some((
                stream::iter(page.opportunities.into_iter().map(Ok)),
                next,
            )))
        })
        .try_flatten()
        .boxed()
    }
}