makegov-tango 0.4.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
//! `GET /api/exclusions/` — SAM.gov exclusions (debarments, suspensions and other ineligibility records).
//!
//! Whether an exclusion is in force is derived at query time from its dates, so filter with `active` rather than trusting a stored flag.
//! Most exclusions are individuals and carry no UEI.

use crate::client::Client;
use crate::error::{Error, Result};
use crate::internal::{apply_pagination, push_opt, push_opt_bool};
use crate::pagination::{FetchFn, Page, PageStream};
use crate::resources::agencies::urlencoding;
use crate::Record;
use bon::Builder;
use std::collections::BTreeMap;
use std::sync::Arc;

/// Options for [`Client::list_exclusions`] and [`Client::iterate_exclusions`].
#[derive(Debug, Clone, Default, Builder, PartialEq, Eq)]
#[non_exhaustive]
pub struct ListExclusionsOptions {
    /// 1-based page number.
    #[builder(into)]
    pub page: Option<u32>,
    /// Page size (server caps at 100).
    #[builder(into)]
    pub limit: Option<u32>,
    /// Keyset cursor.
    #[builder(into)]
    pub cursor: Option<String>,
    /// Comma-separated field selector. Use [`SHAPE_EXCLUSIONS_MINIMAL`](crate::SHAPE_EXCLUSIONS_MINIMAL) or roll your own.
    #[builder(into)]
    pub shape: Option<String>,
    /// Collapse nested objects into dot-separated keys.
    #[builder(default)]
    pub flat: bool,
    /// When [`flat`](Self::flat) is also true, flatten list-valued fields.
    #[builder(default)]
    pub flat_lists: bool,

    /// Whether the exclusion is in force as of today: not delisted, already activated, and not yet terminated. Derived at query time. `Some(false)` reaches the server as a filter value.
    pub active: Option<bool>,
    /// Whether the exclusion has dropped out of the SAM catalog because SAM lifted or withdrew it. Distinct from a natural expiration, which leaves the record listed.
    pub delisted: Option<bool>,
    /// Classification type (e.g. `Firm|Individual`).
    #[builder(into)]
    pub classification_type: Option<String>,
    /// Exclusion type. Supports OR via `|`.
    #[builder(into)]
    pub exclusion_type: Option<String>,
    /// Exclusion program (e.g. `Reciprocal`).
    #[builder(into)]
    pub exclusion_program: Option<String>,
    /// The excluding agency as a Tango agency name, abbreviation, code or organization key (e.g. `DOD`). Matches by name, abbreviation, or code (CGAC, FPDS, Federal Hierarchy key or AAC) and includes every sub-agency and office beneath it; OR several with `|`. A token that does not resolve falls back to an exact match on `excluding_agency_code`.
    #[builder(into)]
    pub agency: Option<String>,
    /// Code of the agency that issued the exclusion.
    #[builder(into)]
    pub excluding_agency_code: Option<String>,
    /// Name of the agency that issued the exclusion.
    #[builder(into)]
    pub excluding_agency_name: Option<String>,
    /// SAM UEI on the exclusion record.
    #[builder(into)]
    pub uei: Option<String>,
    /// CAGE code. Supports OR via `|`.
    #[builder(into)]
    pub cage_code: Option<String>,
    /// National Provider Identifier. Supports OR via `|`.
    #[builder(into)]
    pub npi: Option<String>,
    /// UEI of the linked Tango entity. Set only when the exclusion's UEI matches a registered entity.
    #[builder(into)]
    pub entity_uei: Option<String>,
    /// Activation date on or after (`YYYY-MM-DD`).
    #[builder(into)]
    pub activate_date_after: Option<String>,
    /// Activation date on or before (`YYYY-MM-DD`).
    #[builder(into)]
    pub activate_date_before: Option<String>,
    /// Termination date on or after (`YYYY-MM-DD`).
    #[builder(into)]
    pub termination_date_after: Option<String>,
    /// Termination date on or before (`YYYY-MM-DD`).
    #[builder(into)]
    pub termination_date_before: Option<String>,
    /// SAM update date on or after (`YYYY-MM-DD`).
    #[builder(into)]
    pub update_date_after: Option<String>,
    /// SAM update date on or before (`YYYY-MM-DD`).
    #[builder(into)]
    pub update_date_before: Option<String>,
    /// Full-text search across names, excluding agency, and comments.
    #[builder(into)]
    pub search: Option<String>,
    /// One of `activate_date`, `termination_date`, `create_date`, `update_date`, `modified`, each optionally `-` prefixed.
    #[builder(into)]
    pub ordering: Option<String>,

    /// Escape hatch for filter keys not yet first-classed on this struct.
    #[builder(default)]
    pub extra: BTreeMap<String, String>,
}

impl ListExclusionsOptions {
    pub(crate) fn to_query(&self) -> Vec<(String, String)> {
        let mut q = Vec::new();
        apply_pagination(
            &mut q,
            self.page,
            self.limit,
            self.cursor.as_deref(),
            self.shape.as_deref(),
            self.flat,
            self.flat_lists,
        );
        push_opt_bool(&mut q, "active", self.active);
        push_opt_bool(&mut q, "delisted", self.delisted);
        push_opt(
            &mut q,
            "classification_type",
            self.classification_type.as_deref(),
        );
        push_opt(&mut q, "exclusion_type", self.exclusion_type.as_deref());
        push_opt(
            &mut q,
            "exclusion_program",
            self.exclusion_program.as_deref(),
        );
        push_opt(&mut q, "agency", self.agency.as_deref());
        push_opt(
            &mut q,
            "excluding_agency_code",
            self.excluding_agency_code.as_deref(),
        );
        push_opt(
            &mut q,
            "excluding_agency_name",
            self.excluding_agency_name.as_deref(),
        );
        push_opt(&mut q, "uei", self.uei.as_deref());
        push_opt(&mut q, "cage_code", self.cage_code.as_deref());
        push_opt(&mut q, "npi", self.npi.as_deref());
        push_opt(&mut q, "entity_uei", self.entity_uei.as_deref());
        push_opt(
            &mut q,
            "activate_date_after",
            self.activate_date_after.as_deref(),
        );
        push_opt(
            &mut q,
            "activate_date_before",
            self.activate_date_before.as_deref(),
        );
        push_opt(
            &mut q,
            "termination_date_after",
            self.termination_date_after.as_deref(),
        );
        push_opt(
            &mut q,
            "termination_date_before",
            self.termination_date_before.as_deref(),
        );
        push_opt(
            &mut q,
            "update_date_after",
            self.update_date_after.as_deref(),
        );
        push_opt(
            &mut q,
            "update_date_before",
            self.update_date_before.as_deref(),
        );
        push_opt(&mut q, "search", self.search.as_deref());
        push_opt(&mut q, "ordering", self.ordering.as_deref());
        for (k, v) in &self.extra {
            if !v.is_empty() {
                q.push((k.clone(), v.clone()));
            }
        }
        q
    }
}

/// Options for [`Client::get_exclusion`].
#[derive(Debug, Clone, Default, Builder, PartialEq, Eq)]
#[non_exhaustive]
pub struct GetExclusionOptions {
    /// Shape selector. When empty, the server returns its default detail shape.
    #[builder(into)]
    pub shape: Option<String>,
    /// Flatten nested objects into dot-separated keys.
    #[builder(default)]
    pub flat: bool,
    /// When `flat=true`, also flatten list-valued nested fields.
    #[builder(default)]
    pub flat_lists: bool,
}

impl GetExclusionOptions {
    pub(crate) fn to_query(&self) -> Vec<(String, String)> {
        let mut q = Vec::new();
        push_opt(&mut q, "shape", self.shape.as_deref());
        if self.flat {
            q.push(("flat".into(), "true".into()));
        }
        if self.flat_lists {
            q.push(("flat_lists".into(), "true".into()));
        }
        q
    }
}

impl Client {
    /// `GET /api/exclusions/` — one page of exclusion records.
    pub async fn list_exclusions(&self, opts: ListExclusionsOptions) -> Result<Page<Record>> {
        let q = opts.to_query();
        self.get_page("/api/exclusions/", &q).await
    }

    /// `GET /api/exclusions/{exclusion_key}/` — a single exclusion.
    pub async fn get_exclusion(
        &self,
        exclusion_key: &str,
        opts: Option<GetExclusionOptions>,
    ) -> Result<Record> {
        if exclusion_key.is_empty() {
            return Err(Error::Validation {
                message: "get_exclusion: exclusion_key is required".into(),
                response: None,
            });
        }
        let q = opts.unwrap_or_default().to_query();
        let path = format!("/api/exclusions/{}/", urlencoding(exclusion_key));
        self.get_json::<Record>(&path, &q).await
    }

    /// Stream every exclusion matching `opts`.
    pub fn iterate_exclusions(&self, opts: ListExclusionsOptions) -> PageStream<Record> {
        let opts = Arc::new(opts);
        let fetch: FetchFn<Record> = Box::new(move |client, page, cursor| {
            let mut next = (*opts).clone();
            next.page = page;
            next.cursor = cursor;
            Box::pin(async move { client.list_exclusions(next).await })
        });
        PageStream::new(self.clone(), fetch)
    }
}

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

    fn get_q(q: &[(String, String)], k: &str) -> Option<String> {
        q.iter().find(|(kk, _)| kk == k).map(|(_, v)| v.clone())
    }

    #[test]
    fn list_exclusions_all_filters_emit() {
        let opts = ListExclusionsOptions::builder()
            .active(true)
            .delisted(true)
            .classification_type("v-classification_type")
            .exclusion_type("v-exclusion_type")
            .exclusion_program("v-exclusion_program")
            .excluding_agency_code("v-excluding_agency_code")
            .excluding_agency_name("v-excluding_agency_name")
            .uei("v-uei")
            .cage_code("v-cage_code")
            .npi("v-npi")
            .entity_uei("v-entity_uei")
            .activate_date_after("v-activate_date_after")
            .activate_date_before("v-activate_date_before")
            .termination_date_after("v-termination_date_after")
            .termination_date_before("v-termination_date_before")
            .update_date_after("v-update_date_after")
            .update_date_before("v-update_date_before")
            .search("v-search")
            .ordering("v-ordering")
            .build();
        let q = opts.to_query();
        assert_eq!(get_q(&q, "active").as_deref(), Some("true"));
        assert_eq!(get_q(&q, "delisted").as_deref(), Some("true"));
        assert_eq!(
            get_q(&q, "classification_type").as_deref(),
            Some("v-classification_type")
        );
        assert_eq!(
            get_q(&q, "exclusion_type").as_deref(),
            Some("v-exclusion_type")
        );
        assert_eq!(
            get_q(&q, "exclusion_program").as_deref(),
            Some("v-exclusion_program")
        );
        assert_eq!(
            get_q(&q, "excluding_agency_code").as_deref(),
            Some("v-excluding_agency_code")
        );
        assert_eq!(
            get_q(&q, "excluding_agency_name").as_deref(),
            Some("v-excluding_agency_name")
        );
        assert_eq!(get_q(&q, "uei").as_deref(), Some("v-uei"));
        assert_eq!(get_q(&q, "cage_code").as_deref(), Some("v-cage_code"));
        assert_eq!(get_q(&q, "npi").as_deref(), Some("v-npi"));
        assert_eq!(get_q(&q, "entity_uei").as_deref(), Some("v-entity_uei"));
        assert_eq!(
            get_q(&q, "activate_date_after").as_deref(),
            Some("v-activate_date_after")
        );
        assert_eq!(
            get_q(&q, "activate_date_before").as_deref(),
            Some("v-activate_date_before")
        );
        assert_eq!(
            get_q(&q, "termination_date_after").as_deref(),
            Some("v-termination_date_after")
        );
        assert_eq!(
            get_q(&q, "termination_date_before").as_deref(),
            Some("v-termination_date_before")
        );
        assert_eq!(
            get_q(&q, "update_date_after").as_deref(),
            Some("v-update_date_after")
        );
        assert_eq!(
            get_q(&q, "update_date_before").as_deref(),
            Some("v-update_date_before")
        );
        assert_eq!(get_q(&q, "search").as_deref(), Some("v-search"));
        assert_eq!(get_q(&q, "ordering").as_deref(), Some("v-ordering"));
        assert_eq!(q.len(), 19);
    }

    #[test]
    fn list_exclusions_active_false_is_a_filter_not_an_absence() {
        let opts = ListExclusionsOptions::builder().active(false).build();
        assert_eq!(get_q(&opts.to_query(), "active").as_deref(), Some("false"));
    }

    #[test]
    fn list_exclusions_zero_value_omitted() {
        assert!(ListExclusionsOptions::builder()
            .build()
            .to_query()
            .is_empty());
    }

    #[test]
    fn list_exclusions_cursor_wins_over_page_and_shape_emits() {
        let opts = ListExclusionsOptions::builder()
            .page(3u32)
            .cursor("c0".to_string())
            .shape(crate::SHAPE_EXCLUSIONS_MINIMAL)
            .build();
        let q = opts.to_query();
        assert_eq!(get_q(&q, "cursor").as_deref(), Some("c0"));
        assert_eq!(get_q(&q, "page"), None);
        assert_eq!(
            get_q(&q, "shape").as_deref(),
            Some(crate::SHAPE_EXCLUSIONS_MINIMAL)
        );
    }

    #[test]
    fn list_exclusions_extra_emits() {
        let mut extra = BTreeMap::new();
        extra.insert("custom_x".to_string(), "xv".to_string());
        let q = ListExclusionsOptions::builder()
            .extra(extra)
            .build()
            .to_query();
        assert!(q.contains(&("custom_x".into(), "xv".into())));
    }

    #[tokio::test]
    async fn get_exclusion_validates_empty_exclusion_key() {
        let client = Client::builder().api_key("x").build().expect("client");
        let err = client.get_exclusion("", None).await.unwrap_err();
        match err {
            Error::Validation { message, .. } => {
                assert!(message.contains("exclusion_key is required"));
            }
            other => panic!("expected Validation, got {other:?}"),
        }
    }

    #[test]
    fn get_options_emits() {
        let q = GetExclusionOptions::builder()
            .shape("a,b")
            .flat(true)
            .flat_lists(true)
            .build()
            .to_query();
        assert_eq!(get_q(&q, "shape").as_deref(), Some("a,b"));
        assert_eq!(get_q(&q, "flat").as_deref(), Some("true"));
        assert_eq!(get_q(&q, "flat_lists").as_deref(), Some("true"));
    }

    #[test]
    fn list_exclusions_agency_filter_emits() {
        let q = ListExclusionsOptions::builder()
            .agency("DOD|HHS")
            .build()
            .to_query();
        assert_eq!(get_q(&q, "agency").as_deref(), Some("DOD|HHS"));
    }
}