Skip to main content

vantage_api_client/rest/
api.rs

1use ciborium::Value as CborValue;
2use indexmap::IndexMap;
3use vantage_core::error;
4use vantage_dataset::traits::Result;
5use vantage_expressions::Expression;
6use vantage_expressions::traits::expressive::ExpressiveEnum;
7use vantage_table::pagination::Pagination;
8use vantage_types::Record;
9
10/// How the API wraps its row array in the response body.
11///
12/// Most public APIs use one of these three shapes; the legacy vantage
13/// "wrapped under `data`" shape is `Wrapped { array_key: "data" }`.
14#[derive(Clone, Debug)]
15pub enum ResponseShape {
16    /// Body is a bare JSON array of records.
17    /// Example: `GET /users` → `[ {…}, {…} ]`. JSONPlaceholder, GitHub, etc.
18    BareArray,
19
20    /// Body is a JSON object with the array under a fixed key.
21    /// Example: `GET /users` → `{ "data": [ … ] }`.
22    Wrapped { array_key: String },
23
24    /// Body is a JSON object with the array under a key matching the
25    /// table name. Example (DummyJSON):
26    /// `GET /products` → `{ "products": [ … ], "total": …, "skip": …, "limit": … }`.
27    WrappedByTableName,
28}
29
30impl Default for ResponseShape {
31    /// Default matches the legacy 0.1.x shape: `{ "data": [...] }`.
32    fn default() -> Self {
33        ResponseShape::Wrapped {
34            array_key: "data".to_string(),
35        }
36    }
37}
38
39/// Names of the page/limit query parameters the API expects.
40///
41/// Defaults to `("_page", "_limit")` — the JSON Server convention used
42/// by JSONPlaceholder. DummyJSON uses `("skip", "limit")` (in items not
43/// pages). Customise via `RestApiBuilder::pagination_params`.
44#[derive(Clone, Debug)]
45pub struct PaginationParams {
46    pub page: String,
47    pub limit: String,
48    /// If true, the page parameter is sent as a *0-based item offset*
49    /// (`skip`) instead of a 1-based page index. DummyJSON-style.
50    pub skip_based: bool,
51}
52
53impl PaginationParams {
54    pub fn page_limit(page: impl Into<String>, limit: impl Into<String>) -> Self {
55        Self {
56            page: page.into(),
57            limit: limit.into(),
58            skip_based: false,
59        }
60    }
61
62    pub fn skip_limit(skip: impl Into<String>, limit: impl Into<String>) -> Self {
63        Self {
64            page: skip.into(),
65            limit: limit.into(),
66            skip_based: true,
67        }
68    }
69}
70
71impl Default for PaginationParams {
72    fn default() -> Self {
73        Self::page_limit("_page", "_limit")
74    }
75}
76
77/// REST API backend for Vantage — reads data from HTTP JSON endpoints.
78///
79/// Each table maps to an API endpoint: `{base_url}/{table_name}`.
80/// Response shape is configurable via [`RestApi::builder`]; see
81/// [`ResponseShape`] for the supported variants.
82///
83/// Currently read-only — write operations return errors.
84/// How a table's conditions are applied to a request.
85///
86/// URL `{placeholder}` path segments are always filled from matching
87/// eq-conditions regardless of strategy; this governs what happens to
88/// the *remaining* (non-path) eq-conditions.
89#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
90pub enum FilterStrategy {
91    /// Append remaining eq-conditions as `?field=value` query params
92    /// (JSON-Server semantics). The default.
93    #[default]
94    Query,
95    /// Apply remaining eq-conditions as in-memory row filters after the
96    /// fetch, never as query params. For APIs whose only server-side
97    /// filters are path segments and that reject (or ignore) unknown
98    /// query params — e.g. the Mercury control-API, whose CLI likewise
99    /// filters version/env client-side after fetching by product path.
100    Client,
101}
102
103#[derive(Clone, Debug)]
104pub struct RestApi {
105    base_url: String,
106    client: reqwest::Client,
107    pub(crate) auth_header: Option<String>,
108    response_shape: ResponseShape,
109    pagination: PaginationParams,
110    /// When true, no `_page`/`_limit` query params are appended and
111    /// list endpoints are assumed to return the full result set in
112    /// one shot. Caller-side requests for page > 1 short-circuit to
113    /// an empty result so a perpetual-grid stops paging after the
114    /// first chunk. Useful for FastAPI/Pydantic services that treat
115    /// unknown query params as strict filters.
116    no_pagination: bool,
117    /// How non-path eq-conditions are applied — query params vs.
118    /// in-memory post-fetch filtering. See [`FilterStrategy`].
119    filter_strategy: FilterStrategy,
120    /// Response-envelope key carrying the grand total of matching rows
121    /// (e.g. `count`). When set, the shell reports an exact count and
122    /// advertises `can_fetch_window` for lazy/scroll loading; when `None`
123    /// it falls back to counting fetched rows.
124    total_key: Option<String>,
125    /// Emit `tracing` events for window/count requests.
126    debug: bool,
127}
128
129impl RestApi {
130    /// Create a new REST API pointing at `base_url`. Uses the legacy
131    /// default response shape (`{ "data": [...] }`). For other shapes
132    /// (bare array, wrapped-by-table-name) use [`RestApi::builder`].
133    pub fn new(base_url: impl Into<String>) -> Self {
134        RestApi::builder(base_url).build()
135    }
136
137    /// Start configuring a [`RestApi`] via the builder.
138    pub fn builder(base_url: impl Into<String>) -> RestApiBuilder {
139        RestApiBuilder::new(base_url.into())
140    }
141
142    /// Set the Authorization header value (e.g. "Bearer `<token>`").
143    /// Provided for backwards compatibility — prefer
144    /// `RestApi::builder(...).auth(...)`.
145    pub fn with_auth(mut self, auth: impl Into<String>) -> Self {
146        self.auth_header = Some(auth.into());
147        self
148    }
149
150    /// The configured response-envelope total key, if any. When set, the
151    /// REST shell can report an exact count and serve `fetch_window`.
152    pub fn total_key(&self) -> Option<&str> {
153        self.total_key.as_deref()
154    }
155
156    /// Build the endpoint path for `table_name`, substituting any
157    /// `{placeholder}` segments from matching eq-conditions.
158    ///
159    /// Returns the absolute URL up to (but excluding) the query string,
160    /// alongside the indices of conditions consumed by the substitution
161    /// — those are dropped from the query string by `build_query_string`.
162    ///
163    /// Tables that don't use templates (no `{}` in the name) pass
164    /// through unchanged and consume no conditions.
165    fn endpoint_url(
166        &self,
167        table_name: &str,
168        conditions: &[&Expression<CborValue>],
169    ) -> Result<(String, Vec<usize>)> {
170        let mut consumed = Vec::new();
171        let mut path = String::with_capacity(table_name.len());
172        let mut rest = table_name;
173        while let Some(open) = rest.find('{') {
174            path.push_str(&rest[..open]);
175            let after = &rest[open + 1..];
176            let close = after.find('}').ok_or_else(|| {
177                error!(
178                    "Unclosed `{` in table name URI template",
179                    table_name = table_name
180                )
181            })?;
182            let placeholder = &after[..close];
183            let (idx, value) = conditions
184                .iter()
185                .enumerate()
186                .find_map(|(i, cond)| {
187                    if consumed.contains(&i) {
188                        return None;
189                    }
190                    let (field, value) = crate::condition_to_query_param(cond)?;
191                    (field == placeholder).then_some((i, value))
192                })
193                .ok_or_else(|| {
194                    error!(
195                        "No eq-condition provided for URI placeholder",
196                        placeholder = placeholder,
197                        table_name = table_name
198                    )
199                })?;
200            consumed.push(idx);
201            path.push_str(&urlencode(&value));
202            rest = &after[close + 1..];
203        }
204        path.push_str(rest);
205        Ok((format!("{}/{}", self.base_url, path), consumed))
206    }
207
208    /// Build the combined query-string from pagination + conditions.
209    /// `consumed` lists condition indices already baked into the URI
210    /// path; those don't appear in the query string. Conditions that
211    /// don't peel cleanly into eq pairs are skipped — same "best effort"
212    /// stance as before.
213    fn build_query_string(
214        &self,
215        window: Option<(i64, i64)>,
216        conditions: &[&Expression<CborValue>],
217        consumed: &[usize],
218    ) -> String {
219        let mut params: Vec<(String, String)> = Vec::new();
220
221        // Pagination first — matches the order users see in the URL bar.
222        // When `no_pagination` is set the API doesn't accept page/limit
223        // query params (and may treat them as strict filters that
224        // return empty), so we leave them off.
225        //
226        // `window` is a half-open `[offset, offset+limit)` band. Skip-based
227        // APIs take the offset verbatim; page-based APIs are addressed by
228        // 1-based page, derived from the offset (the loader may hand
229        // non-page-aligned windows, so it rounds down to the containing page).
230        if !self.no_pagination
231            && let Some((offset, limit)) = window
232        {
233            let offset = offset.max(0);
234            let limit = limit.max(1);
235            let page_value = if self.pagination.skip_based {
236                offset.to_string()
237            } else {
238                (offset / limit + 1).to_string()
239            };
240            params.push((self.pagination.page.clone(), page_value));
241            params.push((self.pagination.limit.clone(), limit.to_string()));
242        }
243
244        // Conditions: each `eq` becomes `?field=value`. Multiple
245        // conditions AND together (JSON Server semantics).
246        for (i, cond) in conditions.iter().enumerate() {
247            if consumed.contains(&i) {
248                continue;
249            }
250            if let Some((field, value)) = crate::condition_to_query_param(cond) {
251                params.push((field, value));
252            }
253        }
254
255        if params.is_empty() {
256            return String::new();
257        }
258        let mut s = String::from("?");
259        for (i, (k, v)) in params.iter().enumerate() {
260            if i > 0 {
261                s.push('&');
262            }
263            // Minimal URL encoding — we encode `&` and `=` and spaces
264            // because those break the query format. Anything else
265            // passes through; the JSON Server convention is permissive.
266            s.push_str(&urlencode(k));
267            s.push('=');
268            s.push_str(&urlencode(v));
269        }
270        s
271    }
272
273    /// Fetch data from the API endpoint and return parsed records.
274    ///
275    /// `id_field` selects which JSON field is treated as the record ID;
276    /// if `None`, row indices are used. The page-based `pagination` is
277    /// lowered to a `[offset, offset+limit)` window; `conditions` are
278    /// pushed into the URL query string — eq-conditions become
279    /// `?field=value`. Conditions that can't be peeled into a simple
280    /// eq are silently skipped (caller-side filtering still applies if
281    /// needed).
282    pub(crate) async fn fetch_records<'a>(
283        &self,
284        table_name: &str,
285        id_field: Option<&str>,
286        pagination: Option<&Pagination>,
287        conditions: impl IntoIterator<Item = &'a Expression<CborValue>>,
288    ) -> Result<IndexMap<String, Record<CborValue>>> {
289        let window = pagination.map(|p| (p.skip(), p.limit()));
290        self.fetch_windowed(table_name, id_field, window, conditions)
291            .await
292            .map(|(records, _total)| records)
293    }
294
295    /// Fetch a single half-open row window `[offset, offset+limit)` — the
296    /// primitive a paged, lazily-loaded grid drives on scroll (offset is
297    /// an absolute row index, not a page number).
298    /// Fetch one half-open row window, plus the envelope's `total_key` when
299    /// the response carries one.
300    ///
301    /// The total comes out of the **same response as the rows**. Every paged
302    /// endpoint reports it on every reply, so a caller wanting both a window
303    /// and a grand total takes them together here rather than pairing a
304    /// window fetch with [`Self::fetch_total`] — that pairing costs a second
305    /// round trip for a number already in hand.
306    pub(crate) async fn fetch_window_records_counted<'a>(
307        &self,
308        table_name: &str,
309        id_field: Option<&str>,
310        offset: i64,
311        limit: i64,
312        conditions: impl IntoIterator<Item = &'a Expression<CborValue>>,
313    ) -> Result<(IndexMap<String, Record<CborValue>>, Option<i64>)> {
314        self.fetch_windowed(table_name, id_field, Some((offset, limit)), conditions)
315            .await
316    }
317
318    /// Read the grand total of matching rows from the response envelope's
319    /// configured `total_key` (e.g. `count`). Returns `None` when no
320    /// `total_key` is set — the caller then falls back to counting fetched
321    /// rows. Issues a cheap `limit=1` request so the body carries the count
322    /// without paying for the rows.
323    pub(crate) async fn fetch_total<'a>(
324        &self,
325        table_name: &str,
326        conditions: impl IntoIterator<Item = &'a Expression<CborValue>>,
327    ) -> Result<Option<i64>> {
328        let Some(total_key) = self.total_key.clone() else {
329            return Ok(None);
330        };
331        let (body, _client_filters) = self
332            .fetch_raw_body(table_name, Some((0, 1)), conditions)
333            .await?;
334        let total = body
335            .get(total_key.as_str())
336            .and_then(|v| v.as_i64())
337            .ok_or_else(|| {
338                error!(
339                    "total_key missing or not an integer in API response",
340                    total_key = total_key.as_str()
341                )
342            })?;
343        if self.debug {
344            tracing::debug!(target: "vantage_api_client::rest", total, "REST count");
345        }
346        Ok(Some(total))
347    }
348
349    /// Resolve conditions, build the windowed request URL, GET it (with the
350    /// auth header if configured), and return the parsed JSON body together
351    /// with any client-side filters that still need applying (under
352    /// [`FilterStrategy::Client`]).
353    async fn fetch_raw_body<'a>(
354        &self,
355        table_name: &str,
356        window: Option<(i64, i64)>,
357        conditions: impl IntoIterator<Item = &'a Expression<CborValue>>,
358    ) -> Result<(serde_json::Value, Vec<(String, String)>)> {
359        // Conditions may carry `DeferredFn` values — typically from
360        // `related_in_condition` for `with_one`-style traversals where the FK
361        // lives in a parent record we haven't fetched yet. Resolve them once,
362        // up front, so the rest of the pipeline sees only sync scalars.
363        let raw: Vec<&Expression<CborValue>> = conditions.into_iter().collect();
364        let mut resolved: Vec<Expression<CborValue>> = Vec::with_capacity(raw.len());
365        for cond in raw {
366            resolved.push(resolve_deferreds(cond.clone()).await?);
367        }
368        let conds: Vec<&Expression<CborValue>> = resolved.iter().collect();
369        let (endpoint, consumed) = self.endpoint_url(table_name, &conds)?;
370
371        // Under `FilterStrategy::Client`, non-path eq-conditions are applied
372        // to the fetched rows in memory rather than sent as query params (the
373        // API rejects/ignores unknown params). Collect them, and keep them out
374        // of the query string by marking every condition as consumed.
375        let (query_consumed, client_filters): (Vec<usize>, Vec<(String, String)>) =
376            if self.filter_strategy == FilterStrategy::Client {
377                let filters = conds
378                    .iter()
379                    .enumerate()
380                    .filter(|(i, _)| !consumed.contains(i))
381                    .filter_map(|(_, c)| crate::condition_to_query_param(c))
382                    .collect();
383                ((0..conds.len()).collect(), filters)
384            } else {
385                (consumed, Vec::new())
386            };
387
388        let query = self.build_query_string(window, &conds, &query_consumed);
389        let url = join_query(&endpoint, &query);
390
391        // The `(0, 1)` window is the count probe (reads only the envelope's
392        // total); log it at debug so it doesn't drown the real data fetches.
393        if self.debug {
394            if window == Some((0, 1)) {
395                tracing::debug!(target: "vantage_api_client::rest", table = table_name, url = %url, "REST GET (count probe)");
396            } else {
397                tracing::info!(target: "vantage_api_client::rest", table = table_name, url = %url, "REST GET");
398            }
399        }
400
401        let mut request = self.client.get(&url);
402        if let Some(ref auth) = self.auth_header {
403            request = request.header("Authorization", auth);
404        }
405
406        // Time every round trip, unconditionally — a remote API is the one part
407        // of a read the process cannot bound, and a slow page is far more often
408        // one slow GET than anything local. Reported regardless of `debug` so
409        // the cost is attributable from a default log; a request over a second
410        // is worth an operator's attention, hence `info` at that point.
411        let started = std::time::Instant::now();
412        let response = request.send().await.map_err(|e| {
413            tracing::warn!(
414                target: "vantage_api_client::rest",
415                table = table_name,
416                url = %url,
417                ms = started.elapsed().as_millis() as u64,
418                "REST GET failed",
419            );
420            error!("API request failed", url = url, detail = e)
421        })?;
422
423        if !response.status().is_success() {
424            return Err(error!(
425                "API returned error status",
426                url = url,
427                status = response.status().as_u16()
428            ));
429        }
430
431        let body: serde_json::Value = response
432            .json()
433            .await
434            .map_err(|e| error!("Failed to parse API response as JSON", detail = e))?;
435
436        let ms = started.elapsed().as_millis() as u64;
437        let probe = window == Some((0, 1));
438        if ms >= 1000 {
439            tracing::info!(
440                target: "vantage_api_client::rest",
441                table = table_name,
442                url = %url,
443                ms,
444                count_probe = probe,
445                "slow REST GET",
446            );
447        } else {
448            tracing::debug!(
449                target: "vantage_api_client::rest",
450                table = table_name,
451                url = %url,
452                ms,
453                count_probe = probe,
454                "REST GET done",
455            );
456        }
457
458        Ok((body, client_filters))
459    }
460
461    /// Also reports the envelope total when `total_key` is configured and the
462    /// body carries it. Unlike [`Self::fetch_total`] this never errors on a
463    /// missing total: the rows are the point here, and a caller that needs a
464    /// definitive count can still ask for one.
465    async fn fetch_windowed<'a>(
466        &self,
467        table_name: &str,
468        id_field: Option<&str>,
469        window: Option<(i64, i64)>,
470        conditions: impl IntoIterator<Item = &'a Expression<CborValue>>,
471    ) -> Result<(IndexMap<String, Record<CborValue>>, Option<i64>)> {
472        // Non-paginating endpoints return the whole list on the first
473        // window; a later window would just re-deliver the same rows and the
474        // perpetual grid would never mark itself exhausted. Short-circuit any
475        // window past the start to empty so the grid sees the chunk shrink
476        // and stops asking for more.
477        if self.no_pagination && window.is_some_and(|(offset, _)| offset > 0) {
478            return Ok((IndexMap::new(), None));
479        }
480
481        let (body, client_filters) = self.fetch_raw_body(table_name, window, conditions).await?;
482        let total = self
483            .total_key
484            .as_deref()
485            .and_then(|key| body.get(key))
486            .and_then(|v| v.as_i64());
487        let data = self.extract_array(&body, table_name)?;
488
489        let mut records = IndexMap::new();
490        for (row_idx, item) in data.iter().enumerate() {
491            let obj = item
492                .as_object()
493                .ok_or_else(|| error!("API data item is not an object", index = row_idx))?;
494
495            // Extract ID from the configured id_field, or use row index
496            let id = id_field
497                .and_then(|field| obj.get(field))
498                .and_then(|v| match v {
499                    serde_json::Value::String(s) => Some(s.clone()),
500                    serde_json::Value::Number(n) => Some(n.to_string()),
501                    _ => None,
502                })
503                .unwrap_or_else(|| row_idx.to_string());
504
505            // The HTTP body parses as JSON for free; convert to CBOR
506            // at this single boundary so the rest of the pipeline
507            // (Table, Vista) sees the universal carrier. json_to_cbor
508            // is total — JSON is a strict subset of CBOR.
509            let mut record: Record<CborValue> = Record::new();
510            for (k, v) in obj {
511                record.insert(k.clone(), vantage_types::json_to_cbor(v.clone()));
512            }
513
514            records.insert(id, record);
515        }
516
517        // Client-side filtering (FilterStrategy::Client): drop rows that
518        // don't match the non-path eq-conditions. A condition whose field
519        // is absent from a row is treated as a pass (it was a path/request
520        // param, not a record field) — mirroring the AWS connector and the
521        // Mercury CLI's own post-fetch `_filter_deployments`.
522        if !client_filters.is_empty() {
523            records.retain(|_id, record| {
524                client_filters
525                    .iter()
526                    .all(|(field, want)| match record.get(field) {
527                        Some(v) => crate::cbor_to_query_string(v).as_deref() == Some(want.as_str()),
528                        None => true,
529                    })
530            });
531            // The envelope counted what the SERVER matched, before these rows
532            // were dropped here — reporting it now would size a grid to rows
533            // it will never be given. No total is better than a wrong one.
534            return Ok((records, None));
535        }
536
537        Ok((records, total))
538    }
539}
540
541fn urlencode(s: &str) -> String {
542    urlencoding::encode(s).into_owned()
543}
544
545/// Append a `build_query_string` result (always opening with `?`, or empty)
546/// to an endpoint URL. The table path may itself carry a query string (e.g.
547/// `launches/?mode=detailed`), in which case the appended params must join
548/// with `&` — otherwise the URL gets two `?` and the API rejects it.
549fn join_query(endpoint: &str, query: &str) -> String {
550    match query.strip_prefix('?') {
551        Some(rest) if endpoint.contains('?') => format!("{endpoint}&{rest}"),
552        _ => format!("{endpoint}{query}"),
553    }
554}
555
556/// Walk an `Expression`'s parameter tree and force any `Deferred`
557/// branches to their resolved form. Used at the `fetch_records`
558/// boundary so the URL builder only sees sync scalars.
559///
560/// Recursion lives on the heap (boxed) because the future's body
561/// contains another `async` call of the same shape — Rust can't size
562/// a directly-recursive `async fn` without indirection.
563fn resolve_deferreds(
564    mut expr: Expression<CborValue>,
565) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Expression<CborValue>>> + Send>> {
566    Box::pin(async move {
567        for param in expr.parameters.iter_mut() {
568            match param {
569                ExpressiveEnum::Deferred(deferred) => {
570                    *param = deferred.call().await?;
571                }
572                ExpressiveEnum::Nested(inner) => {
573                    let resolved = resolve_deferreds(inner.clone()).await?;
574                    *inner = resolved;
575                }
576                ExpressiveEnum::Scalar(_) => {}
577            }
578        }
579        Ok(expr)
580    })
581}
582
583impl RestApi {
584    /// Pull the row array out of the response body, according to the
585    /// configured `ResponseShape`.
586    fn extract_array<'a>(
587        &self,
588        body: &'a serde_json::Value,
589        table_name: &str,
590    ) -> Result<&'a Vec<serde_json::Value>> {
591        match &self.response_shape {
592            ResponseShape::BareArray => body.as_array().ok_or_else(|| {
593                error!("Expected response body to be a JSON array (BareArray shape)")
594            }),
595            ResponseShape::Wrapped { array_key } => body[array_key].as_array().ok_or_else(|| {
596                error!(
597                    "Response missing array under wrapper key",
598                    array_key = array_key
599                )
600            }),
601            ResponseShape::WrappedByTableName => body[table_name].as_array().ok_or_else(|| {
602                error!(
603                    "Response missing array under table-name key",
604                    table_name = table_name
605                )
606            }),
607        }
608    }
609}
610
611/// Builder for [`RestApi`]. Lets callers pick a [`ResponseShape`] and
612/// override the pagination parameter names.
613///
614/// ```no_run
615/// use vantage_api_client::{RestApi, ResponseShape, PaginationParams};
616///
617/// // JSONPlaceholder: bare arrays, JSON-Server pagination conventions.
618/// let api = RestApi::builder("https://jsonplaceholder.typicode.com")
619///     .response_shape(ResponseShape::BareArray)
620///     .build();
621///
622/// // DummyJSON: wrapped-by-table-name, skip-based pagination.
623/// let api = RestApi::builder("https://dummyjson.com")
624///     .response_shape(ResponseShape::WrappedByTableName)
625///     .pagination_params(PaginationParams::skip_limit("skip", "limit"))
626///     .build();
627/// ```
628#[derive(Clone, Debug)]
629pub struct RestApiBuilder {
630    base_url: String,
631    auth_header: Option<String>,
632    response_shape: ResponseShape,
633    pagination: PaginationParams,
634    no_pagination: bool,
635    filter_strategy: FilterStrategy,
636    total_key: Option<String>,
637    debug: bool,
638}
639
640impl RestApiBuilder {
641    fn new(base_url: String) -> Self {
642        Self {
643            base_url,
644            auth_header: None,
645            response_shape: ResponseShape::default(),
646            pagination: PaginationParams::default(),
647            no_pagination: false,
648            filter_strategy: FilterStrategy::default(),
649            total_key: None,
650            debug: false,
651        }
652    }
653
654    /// Set the Authorization header value (e.g. "Bearer `<token>`").
655    pub fn auth(mut self, auth: impl Into<String>) -> Self {
656        self.auth_header = Some(auth.into());
657        self
658    }
659
660    /// Choose how the API wraps its row array. Defaults to
661    /// `Wrapped { array_key: "data" }` for backwards compat.
662    pub fn response_shape(mut self, shape: ResponseShape) -> Self {
663        self.response_shape = shape;
664        self
665    }
666
667    /// Override the page/limit query parameter names. Default is
668    /// `("_page", "_limit")` (JSON Server convention).
669    pub fn pagination_params(mut self, pagination: PaginationParams) -> Self {
670        self.pagination = pagination;
671        self
672    }
673
674    /// Disable pagination entirely — no `_page`/`_limit` query
675    /// params are appended, and a request for page > 1 is short-
676    /// circuited to an empty result. Use this for APIs that don't
677    /// paginate (return the full list every call) or that treat
678    /// unknown query params as strict filters.
679    pub fn no_pagination(mut self) -> Self {
680        self.no_pagination = true;
681        self
682    }
683
684    /// Choose how non-path eq-conditions are applied. Default is
685    /// [`FilterStrategy::Query`]; use [`FilterStrategy::Client`] for
686    /// APIs that only filter via path segments and reject/ignore unknown
687    /// query params (the conditions are then applied in memory).
688    pub fn filter_strategy(mut self, strategy: FilterStrategy) -> Self {
689        self.filter_strategy = strategy;
690        self
691    }
692
693    /// Name the response-envelope key carrying the grand total of matching
694    /// rows (e.g. `count`). Setting it lets the shell report an exact count
695    /// and advertise `can_fetch_window` for lazy/scroll loading.
696    pub fn total_key(mut self, key: impl Into<String>) -> Self {
697        self.total_key = Some(key.into());
698        self
699    }
700
701    /// Emit `tracing` events for window/count requests.
702    pub fn debug(mut self, debug: bool) -> Self {
703        self.debug = debug;
704        self
705    }
706
707    pub fn build(self) -> RestApi {
708        RestApi {
709            base_url: self.base_url,
710            client: reqwest::Client::new(),
711            auth_header: self.auth_header,
712            response_shape: self.response_shape,
713            pagination: self.pagination,
714            no_pagination: self.no_pagination,
715            filter_strategy: self.filter_strategy,
716            total_key: self.total_key,
717            debug: self.debug,
718        }
719    }
720}
721
722#[cfg(test)]
723mod tests {
724    use super::*;
725
726    /// `build_query_string` with no conditions, exercising only the
727    /// window → pagination-param mapping.
728    fn qs(api: &RestApi, window: Option<(i64, i64)>) -> String {
729        api.build_query_string(window, &[], &[])
730    }
731
732    #[test]
733    fn skip_based_window_uses_offset_verbatim() {
734        let api = RestApi::builder("http://x")
735            .pagination_params(PaginationParams::skip_limit("skip", "limit"))
736            .build();
737        assert_eq!(qs(&api, Some((20, 10))), "?skip=20&limit=10");
738    }
739
740    #[test]
741    fn page_based_window_derives_one_based_page() {
742        let api = RestApi::builder("http://x").build(); // default _page/_limit
743        // offset 20 / limit 10 → page 3 (1-based).
744        assert_eq!(qs(&api, Some((20, 10))), "?_page=3&_limit=10");
745    }
746
747    #[test]
748    fn no_window_emits_no_pagination_params() {
749        let api = RestApi::builder("http://x").build();
750        assert_eq!(qs(&api, None), "");
751    }
752
753    #[test]
754    fn no_pagination_suppresses_window_params() {
755        let api = RestApi::builder("http://x").no_pagination().build();
756        assert_eq!(qs(&api, Some((20, 10))), "");
757    }
758
759    #[test]
760    fn query_string_joins_plain_endpoint_with_question_mark() {
761        assert_eq!(
762            join_query("http://x/launches/", "?_page=1&_limit=10"),
763            "http://x/launches/?_page=1&_limit=10"
764        );
765    }
766
767    #[test]
768    fn query_string_joins_templated_endpoint_with_ampersand() {
769        // Endpoint already carries `?mode=detailed`; pagination must append
770        // with `&`, not a second `?`.
771        assert_eq!(
772            join_query("http://x/launches/?mode=detailed", "?offset=0&limit=1"),
773            "http://x/launches/?mode=detailed&offset=0&limit=1"
774        );
775    }
776
777    #[test]
778    fn empty_query_string_leaves_endpoint_untouched() {
779        assert_eq!(
780            join_query("http://x/launches/?mode=detailed", ""),
781            "http://x/launches/?mode=detailed"
782        );
783    }
784
785    /// Live regression for the double-`?` bug: a real fetch against the
786    /// Launch Library 2 dev API using a table path that already carries a
787    /// query string (`launches/?mode=detailed`). Before the `join_query`
788    /// fix the request URL was `…/launches/?mode=detailed?offset=0&limit=1`
789    /// and the server answered 500. Network-gated, so `#[ignore]`d:
790    /// `cargo test -p vantage-api-client -- --ignored query_string`.
791    #[tokio::test]
792    #[ignore = "hits the live Launch Library 2 dev API"]
793    async fn live_templated_table_path_fetches_rows() {
794        let api = RestApi::builder("https://lldev.thespacedevs.com/2.3.0")
795            .pagination_params(PaginationParams::skip_limit("offset", "limit"))
796            .response_shape(ResponseShape::Wrapped {
797                array_key: "results".into(),
798            })
799            .total_key("count")
800            .build();
801
802        let total = api
803            .fetch_total("launches/?mode=detailed", [])
804            .await
805            .expect("fetch_total");
806        assert!(total.is_some_and(|n| n > 0), "expected a positive count");
807
808        let (rows, window_total) = api
809            .fetch_window_records_counted("launches/?mode=detailed", Some("id"), 0, 3, [])
810            .await
811            .expect("fetch_window_records_counted");
812        assert_eq!(rows.len(), 3, "expected the requested 3-row window");
813        // The whole point of the counted window: the same response that
814        // carried the rows also carried the count, so `fetch_total`'s extra
815        // round trip buys nothing a caller couldn't already have.
816        assert_eq!(
817            window_total, total,
818            "the window's envelope total should match the dedicated count",
819        );
820    }
821}