makegov-tango 0.1.0

Official Rust SDK for the Tango federal-contracting data API
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
423
//! Webhook management API — CRUD for endpoints and filter-based alerts.
//!
//! This module covers the *management* surface (`/api/webhooks/...`). Signing
//! and verification of inbound deliveries live in the separate `tango-webhooks`
//! crate so a receiver service doesn't have to pull the full SDK.

use crate::client::Client;
use crate::error::{Error, Result};
use crate::models::{
    WebhookAlert, WebhookAlertCreateInput, WebhookAlertUpdateInput, WebhookEndpoint,
    WebhookEndpointCreateInput, WebhookEndpointUpdateInput, WebhookEventTypesResponse,
    WebhookSamplePayloadResponse, WebhookTestDeliveryResult,
};
use crate::pagination::Page;
use crate::resources::agencies::urlencoding;
use crate::ListOptions;
use serde::Serialize;

/// Body for `POST /api/webhooks/endpoints/test-delivery/`. The canonical key
/// is `endpoint` (per tango#2252).
#[derive(Serialize)]
struct TestDeliveryBody<'a> {
    endpoint: &'a str,
}

// ---------------------------------------------------------------------------
// Webhook endpoints
// ---------------------------------------------------------------------------

impl Client {
    /// `GET /api/webhooks/event-types/` — list the event types the server can emit.
    pub async fn list_webhook_event_types(&self) -> Result<WebhookEventTypesResponse> {
        self.get_json::<WebhookEventTypesResponse>("/api/webhooks/event-types/", &[])
            .await
    }

    /// `GET /api/webhooks/endpoints/` — list the caller's configured webhook
    /// endpoints. Pagination only — no resource-specific filters.
    pub async fn list_webhook_endpoints(&self, opts: ListOptions) -> Result<Page<WebhookEndpoint>> {
        let mut q = Vec::new();
        opts.apply(&mut q);
        let bytes = self.get_bytes("/api/webhooks/endpoints/", &q).await?;
        Page::<WebhookEndpoint>::decode(&bytes)
    }

    /// `GET /api/webhooks/endpoints/{id}/` — fetch a single endpoint.
    pub async fn get_webhook_endpoint(&self, id: &str) -> Result<WebhookEndpoint> {
        if id.is_empty() {
            return Err(Error::Validation {
                message: "GetWebhookEndpoint: id is required".into(),
                response: None,
            });
        }
        let path = format!("/api/webhooks/endpoints/{}/", urlencoding(id));
        self.get_json::<WebhookEndpoint>(&path, &[]).await
    }

    /// `POST /api/webhooks/endpoints/` — create a new endpoint.
    ///
    /// `name` and `callback_url` are required. The Tango API enforces
    /// `unique(user, name)` on endpoints, so the SDK validates client-side
    /// for a cleaner error than the server's 400 on duplicates.
    pub async fn create_webhook_endpoint(
        &self,
        input: WebhookEndpointCreateInput,
    ) -> Result<WebhookEndpoint> {
        if input.name.is_empty() {
            return Err(Error::Validation {
                message: "CreateWebhookEndpoint: name is required (the Tango API enforces unique(user, name) on endpoints)".into(),
                response: None,
            });
        }
        if input.callback_url.is_empty() {
            return Err(Error::Validation {
                message: "CreateWebhookEndpoint: callback_url is required".into(),
                response: None,
            });
        }
        self.post_json::<_, WebhookEndpoint>("/api/webhooks/endpoints/", &input)
            .await
    }

    /// `PATCH /api/webhooks/endpoints/{id}/` — update an existing endpoint.
    /// Only `Some` fields on the input are sent.
    pub async fn update_webhook_endpoint(
        &self,
        id: &str,
        input: WebhookEndpointUpdateInput,
    ) -> Result<WebhookEndpoint> {
        if id.is_empty() {
            return Err(Error::Validation {
                message: "UpdateWebhookEndpoint: id is required".into(),
                response: None,
            });
        }
        let path = format!("/api/webhooks/endpoints/{}/", urlencoding(id));
        self.patch_json::<_, WebhookEndpoint>(&path, &input).await
    }

    /// `DELETE /api/webhooks/endpoints/{id}/` — remove an endpoint.
    pub async fn delete_webhook_endpoint(&self, id: &str) -> Result<()> {
        if id.is_empty() {
            return Err(Error::Validation {
                message: "DeleteWebhookEndpoint: id is required".into(),
                response: None,
            });
        }
        let path = format!("/api/webhooks/endpoints/{}/", urlencoding(id));
        self.delete_no_content(&path).await
    }

    /// `POST /api/webhooks/endpoints/test-delivery/` — trigger a test delivery
    /// for `endpoint_id`. The request body uses the canonical key `endpoint`
    /// (per tango#2252); the server still accepts the legacy `endpoint_id`
    /// alias but the SDK sends the canonical key.
    pub async fn test_webhook_endpoint(
        &self,
        endpoint_id: &str,
    ) -> Result<WebhookTestDeliveryResult> {
        if endpoint_id.is_empty() {
            return Err(Error::Validation {
                message: "TestWebhookEndpoint: endpoint_id is required".into(),
                response: None,
            });
        }
        let body = TestDeliveryBody {
            endpoint: endpoint_id,
        };
        self.post_json::<_, WebhookTestDeliveryResult>(
            "/api/webhooks/endpoints/test-delivery/",
            &body,
        )
        .await
    }

    /// `GET /api/webhooks/endpoints/sample-payload/` — fetch a sample delivery
    /// body. When `event_type` is `Some`, returns the single-event-type variant;
    /// when `None`, the server returns samples for every event type.
    pub async fn get_webhook_sample_payload(
        &self,
        event_type: Option<&str>,
    ) -> Result<WebhookSamplePayloadResponse> {
        let mut q = Vec::new();
        if let Some(ev) = event_type.filter(|s| !s.is_empty()) {
            q.push(("event_type".into(), ev.into()));
        }
        self.get_json::<WebhookSamplePayloadResponse>("/api/webhooks/endpoints/sample-payload/", &q)
            .await
    }

    // -----------------------------------------------------------------------
    // Webhook alerts (filter-subscription convenience API)
    // -----------------------------------------------------------------------

    /// `GET /api/webhooks/alerts/` — list the caller's filter-based subscriptions.
    pub async fn list_webhook_alerts(&self, opts: ListOptions) -> Result<Page<WebhookAlert>> {
        let mut q = Vec::new();
        opts.apply(&mut q);
        let bytes = self.get_bytes("/api/webhooks/alerts/", &q).await?;
        Page::<WebhookAlert>::decode(&bytes)
    }

    /// `GET /api/webhooks/alerts/{id}/` — fetch a single alert.
    pub async fn get_webhook_alert(&self, id: &str) -> Result<WebhookAlert> {
        if id.is_empty() {
            return Err(Error::Validation {
                message: "GetWebhookAlert: id is required".into(),
                response: None,
            });
        }
        let path = format!("/api/webhooks/alerts/{}/", urlencoding(id));
        self.get_json::<WebhookAlert>(&path, &[]).await
    }

    /// `POST /api/webhooks/alerts/` — create a filter-based subscription.
    ///
    /// `name`, `query_type` (singular), and a non-empty `filters` object are
    /// required. For accounts with multiple webhook endpoints, set
    /// `input.endpoint` to the destination endpoint UUID; single-endpoint
    /// accounts may omit it.
    pub async fn create_webhook_alert(
        &self,
        input: WebhookAlertCreateInput,
    ) -> Result<WebhookAlert> {
        if input.name.is_empty() {
            return Err(Error::Validation {
                message: "CreateWebhookAlert: name is required".into(),
                response: None,
            });
        }
        if input.query_type.is_empty() {
            return Err(Error::Validation {
                message:
                    r#"CreateWebhookAlert: query_type is required (singular, e.g. "contract")"#
                        .into(),
                response: None,
            });
        }
        let empty = match &input.filters {
            serde_json::Value::Null => true,
            serde_json::Value::Object(m) => m.is_empty(),
            serde_json::Value::Array(a) => a.is_empty(),
            _ => false,
        };
        if empty {
            return Err(Error::Validation {
                message: "CreateWebhookAlert: filters must be a non-empty object".into(),
                response: None,
            });
        }
        self.post_json::<_, WebhookAlert>("/api/webhooks/alerts/", &input)
            .await
    }

    /// `PATCH /api/webhooks/alerts/{id}/` — update an existing alert.
    /// Only `name`, `frequency`, `cron_expression`, and `is_active` are writable.
    pub async fn update_webhook_alert(
        &self,
        id: &str,
        input: WebhookAlertUpdateInput,
    ) -> Result<WebhookAlert> {
        if id.is_empty() {
            return Err(Error::Validation {
                message: "UpdateWebhookAlert: id is required".into(),
                response: None,
            });
        }
        let path = format!("/api/webhooks/alerts/{}/", urlencoding(id));
        self.patch_json::<_, WebhookAlert>(&path, &input).await
    }

    /// `DELETE /api/webhooks/alerts/{id}/` — remove an alert.
    pub async fn delete_webhook_alert(&self, id: &str) -> Result<()> {
        if id.is_empty() {
            return Err(Error::Validation {
                message: "DeleteWebhookAlert: id is required".into(),
                response: None,
            });
        }
        let path = format!("/api/webhooks/alerts/{}/", urlencoding(id));
        self.delete_no_content(&path).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn client() -> Client {
        // Validation must trip BEFORE any HTTP call, so the unreachable base
        // URL is fine — the request must never be issued.
        Client::builder()
            .api_key("k")
            .base_url("http://localhost:1".to_string())
            .build()
            .expect("build client")
    }

    // ---- endpoints: empty-id rejection ----

    #[tokio::test]
    async fn get_endpoint_rejects_empty_id() {
        let err = client().get_webhook_endpoint("").await.unwrap_err();
        assert!(matches!(err, Error::Validation { .. }));
    }

    #[tokio::test]
    async fn update_endpoint_rejects_empty_id() {
        let err = client()
            .update_webhook_endpoint("", WebhookEndpointUpdateInput::default())
            .await
            .unwrap_err();
        assert!(matches!(err, Error::Validation { .. }));
    }

    #[tokio::test]
    async fn delete_endpoint_rejects_empty_id() {
        let err = client().delete_webhook_endpoint("").await.unwrap_err();
        assert!(matches!(err, Error::Validation { .. }));
    }

    #[tokio::test]
    async fn test_endpoint_rejects_empty_id() {
        let err = client().test_webhook_endpoint("").await.unwrap_err();
        assert!(matches!(err, Error::Validation { .. }));
    }

    // ---- create endpoint: required fields ----

    #[tokio::test]
    async fn create_endpoint_rejects_empty_name() {
        let err = client()
            .create_webhook_endpoint(WebhookEndpointCreateInput {
                name: String::new(),
                callback_url: "https://example.com/hook".into(),
                is_active: None,
                event_types: vec![],
            })
            .await
            .unwrap_err();
        assert!(matches!(err, Error::Validation { .. }));
    }

    #[tokio::test]
    async fn create_endpoint_rejects_empty_callback_url() {
        let err = client()
            .create_webhook_endpoint(WebhookEndpointCreateInput {
                name: "my-hook".into(),
                callback_url: String::new(),
                is_active: None,
                event_types: vec![],
            })
            .await
            .unwrap_err();
        assert!(matches!(err, Error::Validation { .. }));
    }

    // ---- alerts: empty-id rejection ----

    #[tokio::test]
    async fn get_alert_rejects_empty_id() {
        let err = client().get_webhook_alert("").await.unwrap_err();
        assert!(matches!(err, Error::Validation { .. }));
    }

    #[tokio::test]
    async fn update_alert_rejects_empty_id() {
        let err = client()
            .update_webhook_alert("", WebhookAlertUpdateInput::default())
            .await
            .unwrap_err();
        assert!(matches!(err, Error::Validation { .. }));
    }

    #[tokio::test]
    async fn delete_alert_rejects_empty_id() {
        let err = client().delete_webhook_alert("").await.unwrap_err();
        assert!(matches!(err, Error::Validation { .. }));
    }

    // ---- create alert: required fields + filters shape ----

    #[tokio::test]
    async fn create_alert_rejects_empty_name() {
        let err = client()
            .create_webhook_alert(WebhookAlertCreateInput {
                name: String::new(),
                query_type: "contract".into(),
                filters: json!({"piid": "X"}),
                frequency: None,
                cron_expression: None,
                endpoint: None,
            })
            .await
            .unwrap_err();
        assert!(matches!(err, Error::Validation { .. }));
    }

    #[tokio::test]
    async fn create_alert_rejects_empty_query_type() {
        let err = client()
            .create_webhook_alert(WebhookAlertCreateInput {
                name: "n".into(),
                query_type: String::new(),
                filters: json!({"piid": "X"}),
                frequency: None,
                cron_expression: None,
                endpoint: None,
            })
            .await
            .unwrap_err();
        assert!(matches!(err, Error::Validation { .. }));
    }

    #[tokio::test]
    async fn create_alert_rejects_null_filters() {
        let err = client()
            .create_webhook_alert(WebhookAlertCreateInput {
                name: "n".into(),
                query_type: "contract".into(),
                filters: serde_json::Value::Null,
                frequency: None,
                cron_expression: None,
                endpoint: None,
            })
            .await
            .unwrap_err();
        assert!(matches!(err, Error::Validation { .. }));
    }

    #[tokio::test]
    async fn create_alert_rejects_empty_object_filters() {
        let err = client()
            .create_webhook_alert(WebhookAlertCreateInput {
                name: "n".into(),
                query_type: "contract".into(),
                filters: json!({}),
                frequency: None,
                cron_expression: None,
                endpoint: None,
            })
            .await
            .unwrap_err();
        assert!(matches!(err, Error::Validation { .. }));
    }

    #[tokio::test]
    async fn create_alert_rejects_empty_array_filters() {
        let err = client()
            .create_webhook_alert(WebhookAlertCreateInput {
                name: "n".into(),
                query_type: "contract".into(),
                filters: json!([]),
                frequency: None,
                cron_expression: None,
                endpoint: None,
            })
            .await
            .unwrap_err();
        assert!(matches!(err, Error::Validation { .. }));
    }
}