uarp-sdk 0.5.7

Async Rust client for the UARP (Snaga) Universal Agent Runtime Platform API
Documentation
// Code generated by @uarp/codegen from spec/openapi.json. DO NOT EDIT.
//!
//! E-commerce: products, customers, orders, enrollments

#![allow(unused_imports, clippy::too_many_arguments)]

use reqwest::Method;
use serde::{Deserialize, Serialize};
use futures_core::Stream;

use crate::client::{Client, Request, NO_BODY, NO_QUERY};
use crate::error::Result;
use crate::generated::models;
use crate::multipart::{field_text, FilePart};
use crate::pagination::CursorGuard;
use crate::util::encode_path;

/// Query and header parameters for `listCommerceCustomers`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct ListCommerceCustomersParams {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limit: Option<i64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cursor: Option<String>,
}

/// Query and header parameters for `listCommerceEnrollments`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct ListCommerceEnrollmentsParams {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
}

/// Query and header parameters for `listCommerceOrders`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct ListCommerceOrdersParams {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limit: Option<i64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status: Option<models::OrderStatus>,
}

/// Query and header parameters for `listCommerceProducts`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct ListCommerceProductsParams {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limit: Option<i64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cursor: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub r#type: Option<models::ProductType>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status: Option<models::ProductStatus>,
}

/// E-commerce: products, customers, orders, enrollments
#[derive(Debug, Clone)]
pub struct CommerceApi {
    pub(crate) client: Client,
}

impl Client {
    /// E-commerce: products, customers, orders, enrollments
    pub fn commerce(&self) -> CommerceApi {
        CommerceApi { client: self.clone() }
    }
}

impl CommerceApi {
    /// Create product
    ///
    /// `POST /api/v1/commerce/products`
    pub async fn create_commerce_product(&self, body: &models::CreateCommerceProductRequest) -> Result<models::Product> {
        self.client
            .request_json(Request {
                method: Method::POST,
                path: "/api/v1/commerce/products".to_string(),
                query: NO_QUERY,
                body: Some(body),
                headers: Vec::new(),
                idempotent: true,
            })
            .await
    }

    /// Delete customer
    ///
    /// `DELETE /api/v1/commerce/customers/{id}`
    pub async fn delete_commerce_customer(&self, id: &str) -> Result<models::DeleteCommerceCustomerResponse> {
        self.client
            .request_json(Request {
                method: Method::DELETE,
                path: format!("/api/v1/commerce/customers/{}", encode_path(id)),
                query: NO_QUERY,
                body: NO_BODY,
                headers: Vec::new(),
                idempotent: true,
            })
            .await
    }

    /// Delete product
    ///
    /// `DELETE /api/v1/commerce/products/{id}`
    pub async fn delete_commerce_product(&self, id: &str) -> Result<models::DeleteCommerceProductResponse> {
        self.client
            .request_json(Request {
                method: Method::DELETE,
                path: format!("/api/v1/commerce/products/{}", encode_path(id)),
                query: NO_QUERY,
                body: NO_BODY,
                headers: Vec::new(),
                idempotent: true,
            })
            .await
    }

    /// Commerce analytics
    ///
    /// `GET /api/v1/commerce/analytics`
    pub async fn get_commerce_analytics(&self) -> Result<models::GetCommerceAnalyticsResponse> {
        self.client
            .request_json(Request {
                method: Method::GET,
                path: "/api/v1/commerce/analytics".to_string(),
                query: NO_QUERY,
                body: NO_BODY,
                headers: Vec::new(),
                idempotent: false,
            })
            .await
    }

    /// Get customer
    ///
    /// `GET /api/v1/commerce/customers/{id}`
    pub async fn get_commerce_customer(&self, id: &str) -> Result<models::Customer> {
        self.client
            .request_json(Request {
                method: Method::GET,
                path: format!("/api/v1/commerce/customers/{}", encode_path(id)),
                query: NO_QUERY,
                body: NO_BODY,
                headers: Vec::new(),
                idempotent: false,
            })
            .await
    }

    /// Get enrollment
    ///
    /// `GET /api/v1/commerce/enrollments/{id}`
    pub async fn get_commerce_enrollment(&self, id: &str) -> Result<models::Enrollment> {
        self.client
            .request_json(Request {
                method: Method::GET,
                path: format!("/api/v1/commerce/enrollments/{}", encode_path(id)),
                query: NO_QUERY,
                body: NO_BODY,
                headers: Vec::new(),
                idempotent: false,
            })
            .await
    }

    /// Get order
    ///
    /// `GET /api/v1/commerce/orders/{id}`
    pub async fn get_commerce_order(&self, id: &str) -> Result<models::Order> {
        self.client
            .request_json(Request {
                method: Method::GET,
                path: format!("/api/v1/commerce/orders/{}", encode_path(id)),
                query: NO_QUERY,
                body: NO_BODY,
                headers: Vec::new(),
                idempotent: false,
            })
            .await
    }

    /// Get product
    ///
    /// `GET /api/v1/commerce/products/{id}`
    pub async fn get_commerce_product(&self, id: &str) -> Result<models::Product> {
        self.client
            .request_json(Request {
                method: Method::GET,
                path: format!("/api/v1/commerce/products/{}", encode_path(id)),
                query: NO_QUERY,
                body: NO_BODY,
                headers: Vec::new(),
                idempotent: false,
            })
            .await
    }

    /// List customers
    ///
    /// `GET /api/v1/commerce/customers`
    pub async fn list_commerce_customers(&self, params: &ListCommerceCustomersParams) -> Result<models::ListCommerceCustomersResponse> {
        self.client
            .request_json(Request {
                method: Method::GET,
                path: "/api/v1/commerce/customers".to_string(),
                query: Some(params),
                body: NO_BODY,
                headers: Vec::new(),
                idempotent: false,
            })
            .await
    }

    /// Stream every item returned by `listCommerceCustomers`, following the `cursor` cursor until
    /// the server reports no further pages.
    pub fn list_commerce_customers_all<'a>(&'a self, params: &'a ListCommerceCustomersParams) -> impl Stream<Item = Result<models::Customer>> + 'a {
        async_stream::try_stream! {
            let mut guard = CursorGuard::new();
            let mut cursor = params.cursor.clone();
            loop {
                let mut page_params = params.clone();
                page_params.cursor = cursor.clone();
                let page = self.list_commerce_customers(&page_params).await?;
                let items = page.items;
                let was_empty = items.is_empty();
                for item in items {
                    yield item;
                }
                match guard.advance(page.cursor, None, was_empty) {
                    Some(next) => cursor = Some(next),
                    None => break,
                }
            }
        }
    }

    /// List enrollments
    ///
    /// `GET /api/v1/commerce/enrollments`
    pub async fn list_commerce_enrollments(&self, params: &ListCommerceEnrollmentsParams) -> Result<models::ListCommerceEnrollmentsResponse> {
        self.client
            .request_json(Request {
                method: Method::GET,
                path: "/api/v1/commerce/enrollments".to_string(),
                query: Some(params),
                body: NO_BODY,
                headers: Vec::new(),
                idempotent: false,
            })
            .await
    }

    /// List orders
    ///
    /// `GET /api/v1/commerce/orders`
    pub async fn list_commerce_orders(&self, params: &ListCommerceOrdersParams) -> Result<models::ListCommerceOrdersResponse> {
        self.client
            .request_json(Request {
                method: Method::GET,
                path: "/api/v1/commerce/orders".to_string(),
                query: Some(params),
                body: NO_BODY,
                headers: Vec::new(),
                idempotent: false,
            })
            .await
    }

    /// List products
    ///
    /// `GET /api/v1/commerce/products`
    pub async fn list_commerce_products(&self, params: &ListCommerceProductsParams) -> Result<models::ListCommerceProductsResponse> {
        self.client
            .request_json(Request {
                method: Method::GET,
                path: "/api/v1/commerce/products".to_string(),
                query: Some(params),
                body: NO_BODY,
                headers: Vec::new(),
                idempotent: false,
            })
            .await
    }

    /// Stream every item returned by `listCommerceProducts`, following the `cursor` cursor until
    /// the server reports no further pages.
    pub fn list_commerce_products_all<'a>(&'a self, params: &'a ListCommerceProductsParams) -> impl Stream<Item = Result<models::Product>> + 'a {
        async_stream::try_stream! {
            let mut guard = CursorGuard::new();
            let mut cursor = params.cursor.clone();
            loop {
                let mut page_params = params.clone();
                page_params.cursor = cursor.clone();
                let page = self.list_commerce_products(&page_params).await?;
                let items = page.products.unwrap_or_default();
                let was_empty = items.is_empty();
                for item in items {
                    yield item;
                }
                match guard.advance(page.cursor, None, was_empty) {
                    Some(next) => cursor = Some(next),
                    None => break,
                }
            }
        }
    }

    /// Update customer
    ///
    /// Whitelisted fields: `name, first_name, last_name, phone, status, tags, note, addresses,
    /// accepts_marketing, verified_email, tax_exempt, currency, locale, metadata`. Unknown fields
    /// silently dropped.
    ///
    /// `PATCH /api/v1/commerce/customers/{id}`
    pub async fn update_commerce_customer(&self, id: &str, body: &models::CustomerUpdate) -> Result<models::Customer> {
        self.client
            .request_json(Request {
                method: Method::PATCH,
                path: format!("/api/v1/commerce/customers/{}", encode_path(id)),
                query: NO_QUERY,
                body: Some(body),
                headers: Vec::new(),
                idempotent: true,
            })
            .await
    }

    /// Update product
    ///
    /// Whitelisted fields: same as create plus `stripe_price_id`. Unknown fields silently dropped.
    ///
    /// `PATCH /api/v1/commerce/products/{id}`
    pub async fn update_commerce_product(&self, id: &str, body: &models::ProductUpdate) -> Result<models::Product> {
        self.client
            .request_json(Request {
                method: Method::PATCH,
                path: format!("/api/v1/commerce/products/{}", encode_path(id)),
                query: NO_QUERY,
                body: Some(body),
                headers: Vec::new(),
                idempotent: true,
            })
            .await
    }
}