#![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;
#[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>,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct ListCommerceEnrollmentsParams {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub status: Option<String>,
}
#[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>,
}
#[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>,
}
#[derive(Debug, Clone)]
pub struct CommerceApi {
pub(crate) client: Client,
}
impl Client {
pub fn commerce(&self) -> CommerceApi {
CommerceApi { client: self.clone() }
}
}
impl CommerceApi {
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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,
}
}
}
}
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
}
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
}
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
}
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,
}
}
}
}
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
}
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
}
}