amazon-spapi 2.0.3

A Rust client library for Amazon Selling Partner API (SP-API)
Documentation
/*
 * Selling Partner API for FBA Inventory
 *
 * The Selling Partner API for FBA Inventory lets you programmatically retrieve information about inventory in Amazon's fulfillment network.
 *
 * The version of the OpenAPI document: v1
 * 
 * Generated by: https://openapi-generator.tech
 */


use reqwest;
use serde::{Deserialize, Serialize, de::Error as _};
use crate::{apis::ResponseContent, models};
use super::{Error, configuration, ContentType};


/// struct for typed errors of method [`add_inventory`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum AddInventoryError {
    Status400(models::fba_inventory::AddInventoryResponse),
    Status403(models::fba_inventory::AddInventoryResponse),
    Status404(models::fba_inventory::AddInventoryResponse),
    Status429(models::fba_inventory::AddInventoryResponse),
    Status500(models::fba_inventory::AddInventoryResponse),
    Status503(models::fba_inventory::AddInventoryResponse),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`create_inventory_item`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CreateInventoryItemError {
    Status400(models::fba_inventory::CreateInventoryItemResponse),
    Status403(models::fba_inventory::CreateInventoryItemResponse),
    Status404(models::fba_inventory::CreateInventoryItemResponse),
    Status429(models::fba_inventory::CreateInventoryItemResponse),
    Status500(models::fba_inventory::CreateInventoryItemResponse),
    Status503(models::fba_inventory::CreateInventoryItemResponse),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`delete_inventory_item`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DeleteInventoryItemError {
    Status400(models::fba_inventory::DeleteInventoryItemResponse),
    Status403(models::fba_inventory::DeleteInventoryItemResponse),
    Status404(models::fba_inventory::DeleteInventoryItemResponse),
    Status429(models::fba_inventory::DeleteInventoryItemResponse),
    Status500(models::fba_inventory::DeleteInventoryItemResponse),
    Status503(models::fba_inventory::DeleteInventoryItemResponse),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`get_inventory_summaries`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetInventorySummariesError {
    Status400(models::fba_inventory::GetInventorySummariesResponse),
    Status403(models::fba_inventory::GetInventorySummariesResponse),
    Status404(models::fba_inventory::GetInventorySummariesResponse),
    Status429(models::fba_inventory::GetInventorySummariesResponse),
    Status500(models::fba_inventory::GetInventorySummariesResponse),
    Status503(models::fba_inventory::GetInventorySummariesResponse),
    UnknownValue(serde_json::Value),
}


/// Requests that Amazon add items to the Sandbox Inventory with desired amount of quantity in the sandbox environment. This is a sandbox-only operation and must be directed to a sandbox endpoint. Refer to [Selling Partner API sandbox](https://developer-docs.amazon.com/sp-api/docs/the-selling-partner-api-sandbox) for more information.
pub async fn add_inventory(configuration: &configuration::Configuration, x_amzn_idempotency_token: &str, add_inventory_request_body: models::fba_inventory::AddInventoryRequest) -> Result<models::fba_inventory::AddInventoryResponse, Error<AddInventoryError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_x_amzn_idempotency_token = x_amzn_idempotency_token;
    let p_add_inventory_request_body = add_inventory_request_body;

    let uri_str = format!("{}/fba/inventory/v1/items/inventory", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    req_builder = req_builder.header("x-amzn-idempotency-token", p_x_amzn_idempotency_token.to_string());
    req_builder = req_builder.json(&p_add_inventory_request_body);

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::fba_inventory::AddInventoryResponse`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::fba_inventory::AddInventoryResponse`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<AddInventoryError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}

/// Requests that Amazon create product-details in the Sandbox Inventory in the sandbox environment. This is a sandbox-only operation and must be directed to a sandbox endpoint. Refer to [Selling Partner API sandbox](https://developer-docs.amazon.com/sp-api/docs/the-selling-partner-api-sandbox) for more information.
pub async fn create_inventory_item(configuration: &configuration::Configuration, create_inventory_item_request_body: models::fba_inventory::CreateInventoryItemRequest) -> Result<models::fba_inventory::CreateInventoryItemResponse, Error<CreateInventoryItemError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_create_inventory_item_request_body = create_inventory_item_request_body;

    let uri_str = format!("{}/fba/inventory/v1/items", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    req_builder = req_builder.json(&p_create_inventory_item_request_body);

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::fba_inventory::CreateInventoryItemResponse`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::fba_inventory::CreateInventoryItemResponse`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<CreateInventoryItemError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}

/// Requests that Amazon Deletes an item from the Sandbox Inventory in the sandbox environment. This is a sandbox-only operation and must be directed to a sandbox endpoint. Refer to [Selling Partner API sandbox](https://developer-docs.amazon.com/sp-api/docs/the-selling-partner-api-sandbox) for more information.
pub async fn delete_inventory_item(configuration: &configuration::Configuration, seller_sku: &str, marketplace_id: &str) -> Result<models::fba_inventory::DeleteInventoryItemResponse, Error<DeleteInventoryItemError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_seller_sku = seller_sku;
    let p_marketplace_id = marketplace_id;

    let uri_str = format!("{}/fba/inventory/v1/items/{sellerSku}", configuration.base_path, sellerSku=crate::apis::urlencode(p_seller_sku));
    let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);

    req_builder = req_builder.query(&[("marketplaceId", &p_marketplace_id.to_string())]);
    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::fba_inventory::DeleteInventoryItemResponse`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::fba_inventory::DeleteInventoryItemResponse`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<DeleteInventoryItemError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}

/// Returns a list of inventory summaries. The summaries returned depend on the presence or absence of the startDateTime, sellerSkus and sellerSku parameters:  - All inventory summaries with available details are returned when the startDateTime, sellerSkus and sellerSku parameters are omitted. - When startDateTime is provided, the operation returns inventory summaries that have had changes after the date and time specified. The sellerSkus and sellerSku parameters are ignored. Important: To avoid errors, use both startDateTime and nextToken to get the next page of inventory summaries that have changed after the date and time specified. - When the sellerSkus parameter is provided, the operation returns inventory summaries for only the specified sellerSkus. The sellerSku parameter is ignored. - When the sellerSku parameter is provided, the operation returns inventory summaries for only the specified sellerSku.  Note: The parameters associated with this operation may contain special characters that must be encoded to successfully call the API. To avoid errors with SKUs when encoding URLs, refer to [URL Encoding](https://developer-docs.amazon.com/sp-api/docs/url-encoding).  Usage Plan:  | Rate (requests per second) | Burst | | ---- | ---- | | 2 | 2 |  The x-amzn-RateLimit-Limit response header returns the usage plan rate limits that were applied to the requested operation, when available. The table above indicates the default rate and burst values for this operation. Selling partners whose business demands require higher throughput may see higher rate and burst values than those shown here. For more information, see [Usage Plans and Rate Limits in the Selling Partner API](https://developer-docs.amazon.com/sp-api/docs/usage-plans-and-rate-limits).
pub async fn get_inventory_summaries(configuration: &configuration::Configuration, granularity_type: &str, granularity_id: &str, marketplace_ids: Vec<String>, details: Option<bool>, start_date_time: Option<String>, seller_skus: Option<Vec<String>>, seller_sku: Option<&str>, next_token: Option<&str>) -> Result<models::fba_inventory::GetInventorySummariesResponse, Error<GetInventorySummariesError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_granularity_type = granularity_type;
    let p_granularity_id = granularity_id;
    let p_marketplace_ids = marketplace_ids;
    let p_details = details;
    let p_start_date_time = start_date_time;
    let p_seller_skus = seller_skus;
    let p_seller_sku = seller_sku;
    let p_next_token = next_token;

    let uri_str = format!("{}/fba/inventory/v1/summaries", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

    if let Some(ref param_value) = p_details {
        req_builder = req_builder.query(&[("details", &param_value.to_string())]);
    }
    req_builder = req_builder.query(&[("granularityType", &p_granularity_type.to_string())]);
    req_builder = req_builder.query(&[("granularityId", &p_granularity_id.to_string())]);
    if let Some(ref param_value) = p_start_date_time {
        req_builder = req_builder.query(&[("startDateTime", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_seller_skus {
        req_builder = match "csv" {
            "multi" => req_builder.query(&param_value.into_iter().map(|p| ("sellerSkus".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
            _ => req_builder.query(&[("sellerSkus", &param_value.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
        };
    }
    if let Some(ref param_value) = p_seller_sku {
        req_builder = req_builder.query(&[("sellerSku", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_next_token {
        req_builder = req_builder.query(&[("nextToken", &param_value.to_string())]);
    }
    req_builder = match "csv" {
        "multi" => req_builder.query(&p_marketplace_ids.into_iter().map(|p| ("marketplaceIds".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
        _ => req_builder.query(&[("marketplaceIds", &p_marketplace_ids.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
    };
    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::fba_inventory::GetInventorySummariesResponse`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::fba_inventory::GetInventorySummariesResponse`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<GetInventorySummariesError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}