#![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 GetListingReviewsParams {
#[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 SearchMarketplaceParams {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub q: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub category: Option<models::SearchMarketplaceCategory>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub sort: Option<models::SearchMarketplaceSort>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub limit: Option<i64>,
}
#[derive(Debug, Clone)]
pub struct MarketplaceApi {
pub(crate) client: Client,
}
impl Client {
pub fn marketplace(&self) -> MarketplaceApi {
MarketplaceApi { client: self.clone() }
}
}
impl MarketplaceApi {
pub async fn get_listing(&self, listing_id: &str) -> Result<models::MarketplaceListing> {
self.client
.request_json(Request {
method: Method::GET,
path: format!("/api/v1/marketplace/listings/{}", encode_path(listing_id)),
query: NO_QUERY,
body: NO_BODY,
headers: Vec::new(),
idempotent: false,
})
.await
}
pub async fn get_listing_reviews(&self, listing_id: &str, params: &GetListingReviewsParams) -> Result<models::GetListingReviewsResponse> {
self.client
.request_json(Request {
method: Method::GET,
path: format!("/api/v1/marketplace/listings/{}/reviews", encode_path(listing_id)),
query: Some(params),
body: NO_BODY,
headers: Vec::new(),
idempotent: false,
})
.await
}
pub fn get_listing_reviews_all<'a>(&'a self, listing_id: &'a str, params: &'a GetListingReviewsParams) -> impl Stream<Item = Result<serde_json::Map<String, serde_json::Value>>> + '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.get_listing_reviews(listing_id, &page_params).await?;
let items = page.reviews.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 get_marketplace_categories(&self) -> Result<serde_json::Value> {
self.client
.request_json(Request {
method: Method::GET,
path: "/api/v1/marketplace/categories".to_string(),
query: NO_QUERY,
body: NO_BODY,
headers: Vec::new(),
idempotent: false,
})
.await
}
pub async fn get_marketplace_invocation(&self, invocation_id: &str) -> Result<models::MarketplaceInvocation> {
self.client
.request_json(Request {
method: Method::GET,
path: format!("/api/v1/marketplace/invocations/{}", encode_path(invocation_id)),
query: NO_QUERY,
body: NO_BODY,
headers: Vec::new(),
idempotent: false,
})
.await
}
pub async fn invoke_listing_agent(&self, listing_id: &str, body: &models::InvokeListingAgentRequest) -> Result<serde_json::Map<String, serde_json::Value>> {
self.client
.request_json(Request {
method: Method::POST,
path: format!("/api/v1/marketplace/listings/{}/invoke", encode_path(listing_id)),
query: NO_QUERY,
body: Some(body),
headers: Vec::new(),
idempotent: true,
})
.await
}
pub async fn list_subscriptions(&self) -> Result<models::ListSubscriptionsResponse> {
self.client
.request_json(Request {
method: Method::GET,
path: "/api/v1/marketplace/subscriptions".to_string(),
query: NO_QUERY,
body: NO_BODY,
headers: Vec::new(),
idempotent: false,
})
.await
}
pub async fn publish_listing(&self, body: &models::PublishListingRequest) -> Result<models::MarketplaceListing> {
self.client
.request_json(Request {
method: Method::POST,
path: "/api/v1/marketplace/listings".to_string(),
query: NO_QUERY,
body: Some(body),
headers: Vec::new(),
idempotent: true,
})
.await
}
pub async fn rate_listing(&self, listing_id: &str, body: &models::RateListingRequest) -> Result<serde_json::Value> {
self.client
.request_json(Request {
method: Method::POST,
path: format!("/api/v1/marketplace/listings/{}/rate", encode_path(listing_id)),
query: NO_QUERY,
body: Some(body),
headers: Vec::new(),
idempotent: true,
})
.await
}
pub async fn search(&self, params: &SearchMarketplaceParams) -> Result<models::SearchMarketplaceResponse> {
self.client
.request_json(Request {
method: Method::GET,
path: "/api/v1/marketplace/search".to_string(),
query: Some(params),
body: NO_BODY,
headers: Vec::new(),
idempotent: false,
})
.await
}
pub async fn subscribe_to_listing(&self, listing_id: &str, body: &models::SubscribeToListingRequest) -> Result<serde_json::Map<String, serde_json::Value>> {
self.client
.request_json(Request {
method: Method::POST,
path: format!("/api/v1/marketplace/listings/{}/subscribe", encode_path(listing_id)),
query: NO_QUERY,
body: Some(body),
headers: Vec::new(),
idempotent: true,
})
.await
}
pub async fn unpublish_listing(&self, listing_id: &str) -> Result<serde_json::Value> {
self.client
.request_json(Request {
method: Method::DELETE,
path: format!("/api/v1/marketplace/listings/{}", encode_path(listing_id)),
query: NO_QUERY,
body: NO_BODY,
headers: Vec::new(),
idempotent: true,
})
.await
}
pub async fn unsubscribe_from_listing(&self, listing_id: &str) -> Result<models::UnsubscribeFromListingResponse> {
self.client
.request_json(Request {
method: Method::DELETE,
path: format!("/api/v1/marketplace/listings/{}/subscribe", encode_path(listing_id)),
query: NO_QUERY,
body: NO_BODY,
headers: Vec::new(),
idempotent: true,
})
.await
}
}