coinbase_advanced/rest/
public.rs1use serde::Deserialize;
4
5use crate::client::RestClient;
6use crate::error::Result;
7use crate::models::{
8 Candle, GetCandlesParams, GetCandlesResponse, GetMarketTradesParams, GetMarketTradesResponse,
9 GetProductBookParams, GetProductBookResponse, ListProductsParams, ListProductsResponse,
10 Product, ProductBook,
11};
12
13#[derive(Debug, Clone, Deserialize)]
15pub struct ServerTime {
16 pub iso: String,
18 #[serde(rename = "epochSeconds")]
20 pub epoch_seconds: String,
21 #[serde(rename = "epochMillis")]
23 pub epoch_millis: String,
24}
25
26pub struct PublicApi<'a> {
31 client: &'a RestClient,
32}
33
34impl<'a> PublicApi<'a> {
35 pub(crate) fn new(client: &'a RestClient) -> Self {
37 Self { client }
38 }
39
40 pub async fn get_time(&self) -> Result<ServerTime> {
58 self.client.public_get("/time").await
59 }
60
61 pub async fn list_products(&self, params: ListProductsParams) -> Result<ListProductsResponse> {
66 self.client
67 .public_get_with_query("/market/products", ¶ms)
68 .await
69 }
70
71 pub async fn list_products_all(&self) -> Result<ListProductsResponse> {
73 self.list_products(ListProductsParams::default()).await
74 }
75
76 pub async fn get_product(&self, product_id: &str) -> Result<Product> {
78 let endpoint = format!("/market/products/{}", product_id);
79 self.client.public_get(&endpoint).await
80 }
81
82 pub async fn get_product_book(&self, params: GetProductBookParams) -> Result<ProductBook> {
84 let response: GetProductBookResponse = self
85 .client
86 .public_get_with_query("/market/product_book", ¶ms)
87 .await?;
88 Ok(response.pricebook)
89 }
90
91 pub async fn get_candles(&self, params: GetCandlesParams) -> Result<Vec<Candle>> {
93 let endpoint = format!("/market/products/{}/candles", params.product_id);
94 let response: GetCandlesResponse = self
95 .client
96 .public_get_with_query(&endpoint, ¶ms)
97 .await?;
98 Ok(response.candles)
99 }
100
101 pub async fn get_market_trades(
103 &self,
104 params: GetMarketTradesParams,
105 ) -> Result<GetMarketTradesResponse> {
106 let endpoint = format!("/market/products/{}/ticker", params.product_id);
107 self.client.public_get_with_query(&endpoint, ¶ms).await
108 }
109}