fba_inventory_v1/
fba_inventory_v1.rs

1use amazon_spapi::{
2    client::{SpapiClient, SpapiConfig},
3    models::fba_inventory::InventorySummary,
4};
5use anyhow::Result;
6
7async fn get_inventory_summaries_all(
8    spapi: &SpapiClient,
9    details: Option<bool>,
10) -> Result<Vec<InventorySummary>> {
11    let mut res: Vec<InventorySummary> = Vec::new();
12    let mut next_token: Option<String> = None;
13
14    loop {
15        let granularity_type = "Marketplace";
16        let granularity_id = spapi.get_marketplace_id();
17        let marketplace_ids = vec![spapi.get_marketplace_id().to_string()];
18
19        let inventory = spapi
20            .get_inventory_summaries(
21                granularity_type,
22                granularity_id,
23                marketplace_ids,
24                details,
25                None,
26                None,
27                None,
28                next_token.as_deref(),
29            )
30            .await?;
31        log::debug!("Fetched inventory summaries successfully: {:?}", inventory);
32
33        if let Some(payload) = inventory.payload {
34            res.extend(payload.inventory_summaries);
35        }
36
37        if let Some(pagination) = inventory.pagination {
38            next_token = pagination.next_token;
39        } else {
40            break;
41        }
42    }
43
44    Ok(res)
45}
46
47#[tokio::main]
48async fn main() -> Result<()> {
49    let client = SpapiClient::new(SpapiConfig::from_env()?)?;
50
51    let inventory_summaries = get_inventory_summaries_all(&client, Some(false)).await?;
52    log::info!("Fetched {} inventory summaries", inventory_summaries.len());
53    for summary in inventory_summaries {
54        log::info!("Inventory Summary: {:?}", summary);
55    }
56
57    Ok(())
58}