use http::Method;
use serde::Deserialize;
use crate::Result;
use crate::models::common::Epic;
use super::WatchlistsApi;
use crate::markets::models::MarketSummary;
use super::models::{
AddMarketResponse, CreateWatchlistRequest, CreateWatchlistResponse, DeleteWatchlistResponse,
RemoveMarketResponse, WatchlistSummary,
};
impl WatchlistsApi<'_> {
#[tracing::instrument(skip_all)]
pub async fn list(&self) -> Result<Vec<WatchlistSummary>> {
#[derive(Deserialize)]
struct Envelope {
watchlists: Vec<WatchlistSummary>,
}
let body: Envelope = self
.client
.transport
.request(
Method::GET,
"watchlists",
Some(1),
None::<&()>,
&self.client.session,
)
.await?;
Ok(body.watchlists)
}
#[tracing::instrument(skip_all)]
pub async fn create(&self, req: CreateWatchlistRequest) -> Result<CreateWatchlistResponse> {
self.client
.transport
.request(
Method::POST,
"watchlists",
Some(1),
Some(&req),
&self.client.session,
)
.await
}
#[tracing::instrument(skip_all, fields(watchlist_id = %watchlist_id))]
pub async fn markets(&self, watchlist_id: &str) -> Result<Vec<MarketSummary>> {
#[derive(Deserialize)]
struct Envelope {
markets: Vec<MarketSummary>,
}
let path = format!("watchlists/{watchlist_id}");
let body: Envelope = self
.client
.transport
.request(
Method::GET,
&path,
Some(1),
None::<&()>,
&self.client.session,
)
.await?;
Ok(body.markets)
}
#[tracing::instrument(skip_all, fields(watchlist_id = %watchlist_id, epic = %epic))]
pub async fn add_market(&self, watchlist_id: &str, epic: &Epic) -> Result<AddMarketResponse> {
#[derive(serde::Serialize)]
struct Body<'b> {
epic: &'b Epic,
}
let path = format!("watchlists/{watchlist_id}");
self.client
.transport
.request(
Method::PUT,
&path,
Some(1),
Some(&Body { epic }),
&self.client.session,
)
.await
}
#[tracing::instrument(skip_all, fields(watchlist_id = %watchlist_id, epic = %epic))]
pub async fn remove_market(
&self,
watchlist_id: &str,
epic: &Epic,
) -> Result<RemoveMarketResponse> {
let path = format!("watchlists/{watchlist_id}/{epic}");
self.client
.transport
.request(
Method::DELETE,
&path,
Some(1),
None::<&()>,
&self.client.session,
)
.await
}
#[tracing::instrument(skip_all, fields(watchlist_id = %watchlist_id))]
pub async fn delete(&self, watchlist_id: &str) -> Result<DeleteWatchlistResponse> {
let path = format!("watchlists/{watchlist_id}");
self.client
.transport
.request(
Method::DELETE,
&path,
Some(1),
None::<&()>,
&self.client.session,
)
.await
}
}