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 }
293
294 /// Fetch a single half-open row window `[offset, offset+limit)` — the
295 /// primitive a paged, lazily-loaded grid drives on scroll (offset is
296 /// an absolute row index, not a page number).
297 pub(crate) async fn fetch_window_records<'a>(
298 &self,
299 table_name: &str,
300 id_field: Option<&str>,
301 offset: i64,
302 limit: i64,
303 conditions: impl IntoIterator<Item = &'a Expression<CborValue>>,
304 ) -> Result<IndexMap<String, Record<CborValue>>> {
305 self.fetch_windowed(table_name, id_field, Some((offset, limit)), conditions)
306 .await
307 }
308
309 /// Read the grand total of matching rows from the response envelope's
310 /// configured `total_key` (e.g. `count`). Returns `None` when no
311 /// `total_key` is set — the caller then falls back to counting fetched
312 /// rows. Issues a cheap `limit=1` request so the body carries the count
313 /// without paying for the rows.
314 pub(crate) async fn fetch_total<'a>(
315 &self,
316 table_name: &str,
317 conditions: impl IntoIterator<Item = &'a Expression<CborValue>>,
318 ) -> Result<Option<i64>> {
319 let Some(total_key) = self.total_key.clone() else {
320 return Ok(None);
321 };
322 let (body, _client_filters) = self
323 .fetch_raw_body(table_name, Some((0, 1)), conditions)
324 .await?;
325 let total = body
326 .get(total_key.as_str())
327 .and_then(|v| v.as_i64())
328 .ok_or_else(|| {
329 error!(
330 "total_key missing or not an integer in API response",
331 total_key = total_key.as_str()
332 )
333 })?;
334 if self.debug {
335 tracing::info!(target: "vantage_api_client::rest", total, "REST count");
336 }
337 Ok(Some(total))
338 }
339
340 /// Resolve conditions, build the windowed request URL, GET it (with the
341 /// auth header if configured), and return the parsed JSON body together
342 /// with any client-side filters that still need applying (under
343 /// [`FilterStrategy::Client`]).
344 async fn fetch_raw_body<'a>(
345 &self,
346 table_name: &str,
347 window: Option<(i64, i64)>,
348 conditions: impl IntoIterator<Item = &'a Expression<CborValue>>,
349 ) -> Result<(serde_json::Value, Vec<(String, String)>)> {
350 // Conditions may carry `DeferredFn` values — typically from
351 // `related_in_condition` for `with_one`-style traversals where the FK
352 // lives in a parent record we haven't fetched yet. Resolve them once,
353 // up front, so the rest of the pipeline sees only sync scalars.
354 let raw: Vec<&Expression<CborValue>> = conditions.into_iter().collect();
355 let mut resolved: Vec<Expression<CborValue>> = Vec::with_capacity(raw.len());
356 for cond in raw {
357 resolved.push(resolve_deferreds(cond.clone()).await?);
358 }
359 let conds: Vec<&Expression<CborValue>> = resolved.iter().collect();
360 let (endpoint, consumed) = self.endpoint_url(table_name, &conds)?;
361
362 // Under `FilterStrategy::Client`, non-path eq-conditions are applied
363 // to the fetched rows in memory rather than sent as query params (the
364 // API rejects/ignores unknown params). Collect them, and keep them out
365 // of the query string by marking every condition as consumed.
366 let (query_consumed, client_filters): (Vec<usize>, Vec<(String, String)>) =
367 if self.filter_strategy == FilterStrategy::Client {
368 let filters = conds
369 .iter()
370 .enumerate()
371 .filter(|(i, _)| !consumed.contains(i))
372 .filter_map(|(_, c)| crate::condition_to_query_param(c))
373 .collect();
374 ((0..conds.len()).collect(), filters)
375 } else {
376 (consumed, Vec::new())
377 };
378
379 let query = self.build_query_string(window, &conds, &query_consumed);
380 let url = join_query(&endpoint, &query);
381
382 if self.debug {
383 tracing::info!(target: "vantage_api_client::rest", table = table_name, url = %url, "REST GET");
384 }
385
386 let mut request = self.client.get(&url);
387 if let Some(ref auth) = self.auth_header {
388 request = request.header("Authorization", auth);
389 }
390
391 let response = request
392 .send()
393 .await
394 .map_err(|e| error!("API request failed", url = url, detail = e))?;
395
396 if !response.status().is_success() {
397 return Err(error!(
398 "API returned error status",
399 url = url,
400 status = response.status().as_u16()
401 ));
402 }
403
404 let body: serde_json::Value = response
405 .json()
406 .await
407 .map_err(|e| error!("Failed to parse API response as JSON", detail = e))?;
408
409 Ok((body, client_filters))
410 }
411
412 async fn fetch_windowed<'a>(
413 &self,
414 table_name: &str,
415 id_field: Option<&str>,
416 window: Option<(i64, i64)>,
417 conditions: impl IntoIterator<Item = &'a Expression<CborValue>>,
418 ) -> Result<IndexMap<String, Record<CborValue>>> {
419 // Non-paginating endpoints return the whole list on the first
420 // window; a later window would just re-deliver the same rows and the
421 // perpetual grid would never mark itself exhausted. Short-circuit any
422 // window past the start to empty so the grid sees the chunk shrink
423 // and stops asking for more.
424 if self.no_pagination && window.is_some_and(|(offset, _)| offset > 0) {
425 return Ok(IndexMap::new());
426 }
427
428 let (body, client_filters) = self.fetch_raw_body(table_name, window, conditions).await?;
429 let data = self.extract_array(&body, table_name)?;
430
431 let mut records = IndexMap::new();
432 for (row_idx, item) in data.iter().enumerate() {
433 let obj = item
434 .as_object()
435 .ok_or_else(|| error!("API data item is not an object", index = row_idx))?;
436
437 // Extract ID from the configured id_field, or use row index
438 let id = id_field
439 .and_then(|field| obj.get(field))
440 .and_then(|v| match v {
441 serde_json::Value::String(s) => Some(s.clone()),
442 serde_json::Value::Number(n) => Some(n.to_string()),
443 _ => None,
444 })
445 .unwrap_or_else(|| row_idx.to_string());
446
447 // The HTTP body parses as JSON for free; convert to CBOR
448 // at this single boundary so the rest of the pipeline
449 // (Table, Vista) sees the universal carrier.
450 let mut record: Record<CborValue> = Record::new();
451 for (k, v) in obj {
452 let cbor = CborValue::serialized(v).map_err(|e| {
453 error!(
454 "JSON → CBOR conversion failed",
455 field = k.clone(),
456 detail = e.to_string()
457 )
458 })?;
459 record.insert(k.clone(), cbor);
460 }
461
462 records.insert(id, record);
463 }
464
465 // Client-side filtering (FilterStrategy::Client): drop rows that
466 // don't match the non-path eq-conditions. A condition whose field
467 // is absent from a row is treated as a pass (it was a path/request
468 // param, not a record field) — mirroring the AWS connector and the
469 // Mercury CLI's own post-fetch `_filter_deployments`.
470 if !client_filters.is_empty() {
471 records.retain(|_id, record| {
472 client_filters
473 .iter()
474 .all(|(field, want)| match record.get(field) {
475 Some(v) => crate::cbor_to_query_string(v).as_deref() == Some(want.as_str()),
476 None => true,
477 })
478 });
479 }
480
481 Ok(records)
482 }
483}
484
485fn urlencode(s: &str) -> String {
486 urlencoding::encode(s).into_owned()
487}
488
489/// Append a `build_query_string` result (always opening with `?`, or empty)
490/// to an endpoint URL. The table path may itself carry a query string (e.g.
491/// `launches/?mode=detailed`), in which case the appended params must join
492/// with `&` — otherwise the URL gets two `?` and the API rejects it.
493fn join_query(endpoint: &str, query: &str) -> String {
494 match query.strip_prefix('?') {
495 Some(rest) if endpoint.contains('?') => format!("{endpoint}&{rest}"),
496 _ => format!("{endpoint}{query}"),
497 }
498}
499
500/// Walk an `Expression`'s parameter tree and force any `Deferred`
501/// branches to their resolved form. Used at the `fetch_records`
502/// boundary so the URL builder only sees sync scalars.
503///
504/// Recursion lives on the heap (boxed) because the future's body
505/// contains another `async` call of the same shape — Rust can't size
506/// a directly-recursive `async fn` without indirection.
507fn resolve_deferreds(
508 mut expr: Expression<CborValue>,
509) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Expression<CborValue>>> + Send>> {
510 Box::pin(async move {
511 for param in expr.parameters.iter_mut() {
512 match param {
513 ExpressiveEnum::Deferred(deferred) => {
514 *param = deferred.call().await?;
515 }
516 ExpressiveEnum::Nested(inner) => {
517 let resolved = resolve_deferreds(inner.clone()).await?;
518 *inner = resolved;
519 }
520 ExpressiveEnum::Scalar(_) => {}
521 }
522 }
523 Ok(expr)
524 })
525}
526
527impl RestApi {
528 /// Pull the row array out of the response body, according to the
529 /// configured `ResponseShape`.
530 fn extract_array<'a>(
531 &self,
532 body: &'a serde_json::Value,
533 table_name: &str,
534 ) -> Result<&'a Vec<serde_json::Value>> {
535 match &self.response_shape {
536 ResponseShape::BareArray => body.as_array().ok_or_else(|| {
537 error!("Expected response body to be a JSON array (BareArray shape)")
538 }),
539 ResponseShape::Wrapped { array_key } => body[array_key].as_array().ok_or_else(|| {
540 error!(
541 "Response missing array under wrapper key",
542 array_key = array_key
543 )
544 }),
545 ResponseShape::WrappedByTableName => body[table_name].as_array().ok_or_else(|| {
546 error!(
547 "Response missing array under table-name key",
548 table_name = table_name
549 )
550 }),
551 }
552 }
553}
554
555/// Builder for [`RestApi`]. Lets callers pick a [`ResponseShape`] and
556/// override the pagination parameter names.
557///
558/// ```no_run
559/// use vantage_api_client::{RestApi, ResponseShape, PaginationParams};
560///
561/// // JSONPlaceholder: bare arrays, JSON-Server pagination conventions.
562/// let api = RestApi::builder("https://jsonplaceholder.typicode.com")
563/// .response_shape(ResponseShape::BareArray)
564/// .build();
565///
566/// // DummyJSON: wrapped-by-table-name, skip-based pagination.
567/// let api = RestApi::builder("https://dummyjson.com")
568/// .response_shape(ResponseShape::WrappedByTableName)
569/// .pagination_params(PaginationParams::skip_limit("skip", "limit"))
570/// .build();
571/// ```
572#[derive(Clone, Debug)]
573pub struct RestApiBuilder {
574 base_url: String,
575 auth_header: Option<String>,
576 response_shape: ResponseShape,
577 pagination: PaginationParams,
578 no_pagination: bool,
579 filter_strategy: FilterStrategy,
580 total_key: Option<String>,
581 debug: bool,
582}
583
584impl RestApiBuilder {
585 fn new(base_url: String) -> Self {
586 Self {
587 base_url,
588 auth_header: None,
589 response_shape: ResponseShape::default(),
590 pagination: PaginationParams::default(),
591 no_pagination: false,
592 filter_strategy: FilterStrategy::default(),
593 total_key: None,
594 debug: false,
595 }
596 }
597
598 /// Set the Authorization header value (e.g. "Bearer `<token>`").
599 pub fn auth(mut self, auth: impl Into<String>) -> Self {
600 self.auth_header = Some(auth.into());
601 self
602 }
603
604 /// Choose how the API wraps its row array. Defaults to
605 /// `Wrapped { array_key: "data" }` for backwards compat.
606 pub fn response_shape(mut self, shape: ResponseShape) -> Self {
607 self.response_shape = shape;
608 self
609 }
610
611 /// Override the page/limit query parameter names. Default is
612 /// `("_page", "_limit")` (JSON Server convention).
613 pub fn pagination_params(mut self, pagination: PaginationParams) -> Self {
614 self.pagination = pagination;
615 self
616 }
617
618 /// Disable pagination entirely — no `_page`/`_limit` query
619 /// params are appended, and a request for page > 1 is short-
620 /// circuited to an empty result. Use this for APIs that don't
621 /// paginate (return the full list every call) or that treat
622 /// unknown query params as strict filters.
623 pub fn no_pagination(mut self) -> Self {
624 self.no_pagination = true;
625 self
626 }
627
628 /// Choose how non-path eq-conditions are applied. Default is
629 /// [`FilterStrategy::Query`]; use [`FilterStrategy::Client`] for
630 /// APIs that only filter via path segments and reject/ignore unknown
631 /// query params (the conditions are then applied in memory).
632 pub fn filter_strategy(mut self, strategy: FilterStrategy) -> Self {
633 self.filter_strategy = strategy;
634 self
635 }
636
637 /// Name the response-envelope key carrying the grand total of matching
638 /// rows (e.g. `count`). Setting it lets the shell report an exact count
639 /// and advertise `can_fetch_window` for lazy/scroll loading.
640 pub fn total_key(mut self, key: impl Into<String>) -> Self {
641 self.total_key = Some(key.into());
642 self
643 }
644
645 /// Emit `tracing` events for window/count requests.
646 pub fn debug(mut self, debug: bool) -> Self {
647 self.debug = debug;
648 self
649 }
650
651 pub fn build(self) -> RestApi {
652 RestApi {
653 base_url: self.base_url,
654 client: reqwest::Client::new(),
655 auth_header: self.auth_header,
656 response_shape: self.response_shape,
657 pagination: self.pagination,
658 no_pagination: self.no_pagination,
659 filter_strategy: self.filter_strategy,
660 total_key: self.total_key,
661 debug: self.debug,
662 }
663 }
664}
665
666#[cfg(test)]
667mod tests {
668 use super::*;
669
670 /// `build_query_string` with no conditions, exercising only the
671 /// window → pagination-param mapping.
672 fn qs(api: &RestApi, window: Option<(i64, i64)>) -> String {
673 api.build_query_string(window, &[], &[])
674 }
675
676 #[test]
677 fn skip_based_window_uses_offset_verbatim() {
678 let api = RestApi::builder("http://x")
679 .pagination_params(PaginationParams::skip_limit("skip", "limit"))
680 .build();
681 assert_eq!(qs(&api, Some((20, 10))), "?skip=20&limit=10");
682 }
683
684 #[test]
685 fn page_based_window_derives_one_based_page() {
686 let api = RestApi::builder("http://x").build(); // default _page/_limit
687 // offset 20 / limit 10 → page 3 (1-based).
688 assert_eq!(qs(&api, Some((20, 10))), "?_page=3&_limit=10");
689 }
690
691 #[test]
692 fn no_window_emits_no_pagination_params() {
693 let api = RestApi::builder("http://x").build();
694 assert_eq!(qs(&api, None), "");
695 }
696
697 #[test]
698 fn no_pagination_suppresses_window_params() {
699 let api = RestApi::builder("http://x").no_pagination().build();
700 assert_eq!(qs(&api, Some((20, 10))), "");
701 }
702
703 #[test]
704 fn query_string_joins_plain_endpoint_with_question_mark() {
705 assert_eq!(
706 join_query("http://x/launches/", "?_page=1&_limit=10"),
707 "http://x/launches/?_page=1&_limit=10"
708 );
709 }
710
711 #[test]
712 fn query_string_joins_templated_endpoint_with_ampersand() {
713 // Endpoint already carries `?mode=detailed`; pagination must append
714 // with `&`, not a second `?`.
715 assert_eq!(
716 join_query("http://x/launches/?mode=detailed", "?offset=0&limit=1"),
717 "http://x/launches/?mode=detailed&offset=0&limit=1"
718 );
719 }
720
721 #[test]
722 fn empty_query_string_leaves_endpoint_untouched() {
723 assert_eq!(
724 join_query("http://x/launches/?mode=detailed", ""),
725 "http://x/launches/?mode=detailed"
726 );
727 }
728
729 /// Live regression for the double-`?` bug: a real fetch against the
730 /// Launch Library 2 dev API using a table path that already carries a
731 /// query string (`launches/?mode=detailed`). Before the `join_query`
732 /// fix the request URL was `…/launches/?mode=detailed?offset=0&limit=1`
733 /// and the server answered 500. Network-gated, so `#[ignore]`d:
734 /// `cargo test -p vantage-api-client -- --ignored query_string`.
735 #[tokio::test]
736 #[ignore = "hits the live Launch Library 2 dev API"]
737 async fn live_templated_table_path_fetches_rows() {
738 let api = RestApi::builder("https://lldev.thespacedevs.com/2.3.0")
739 .pagination_params(PaginationParams::skip_limit("offset", "limit"))
740 .response_shape(ResponseShape::Wrapped {
741 array_key: "results".into(),
742 })
743 .total_key("count")
744 .build();
745
746 let total = api
747 .fetch_total("launches/?mode=detailed", [])
748 .await
749 .expect("fetch_total");
750 assert!(total.is_some_and(|n| n > 0), "expected a positive count");
751
752 let rows = api
753 .fetch_window_records("launches/?mode=detailed", Some("id"), 0, 3, [])
754 .await
755 .expect("fetch_window_records");
756 assert_eq!(rows.len(), 3, "expected the requested 3-row window");
757 }
758}