use serde::Deserialize;
use crate::client::RestClient;
use crate::error::Result;
use crate::models::{
Candle, GetCandlesParams, GetCandlesResponse, GetMarketTradesParams, GetMarketTradesResponse,
GetProductBookParams, GetProductBookResponse, ListProductsParams, ListProductsResponse,
Product, ProductBook,
};
#[derive(Debug, Clone, Deserialize)]
pub struct ServerTime {
pub iso: String,
#[serde(rename = "epochSeconds")]
pub epoch_seconds: String,
#[serde(rename = "epochMillis")]
pub epoch_millis: String,
}
pub struct PublicApi<'a> {
client: &'a RestClient,
}
impl<'a> PublicApi<'a> {
pub(crate) fn new(client: &'a RestClient) -> Self {
Self { client }
}
pub async fn get_time(&self) -> Result<ServerTime> {
self.client.public_get("/time").await
}
pub async fn list_products(&self, params: ListProductsParams) -> Result<ListProductsResponse> {
self.client
.public_get_with_query("/market/products", ¶ms)
.await
}
pub async fn list_products_all(&self) -> Result<ListProductsResponse> {
self.list_products(ListProductsParams::default()).await
}
pub async fn get_product(&self, product_id: &str) -> Result<Product> {
let endpoint = format!("/market/products/{}", product_id);
self.client.public_get(&endpoint).await
}
pub async fn get_product_book(&self, params: GetProductBookParams) -> Result<ProductBook> {
let response: GetProductBookResponse = self
.client
.public_get_with_query("/market/product_book", ¶ms)
.await?;
Ok(response.pricebook)
}
pub async fn get_candles(&self, params: GetCandlesParams) -> Result<Vec<Candle>> {
let endpoint = format!("/market/products/{}/candles", params.product_id);
let response: GetCandlesResponse = self
.client
.public_get_with_query(&endpoint, ¶ms)
.await?;
Ok(response.candles)
}
pub async fn get_market_trades(
&self,
params: GetMarketTradesParams,
) -> Result<GetMarketTradesResponse> {
let endpoint = format!("/market/products/{}/ticker", params.product_id);
self.client.public_get_with_query(&endpoint, ¶ms).await
}
}