use crate::error::Result;
use crate::http::HttpClient;
use crate::types::{CursorResponse, SpotPair, SpotTwapStatus, Timestamp};
#[derive(Debug, Clone)]
pub struct SpotPairsResource {
http: HttpClient,
prefix: String,
}
impl SpotPairsResource {
pub(crate) fn new(http: HttpClient, prefix: &str) -> Self {
Self {
http,
prefix: prefix.to_string(),
}
}
pub async fn list(&self) -> Result<Vec<SpotPair>> {
self.http
.get(&format!("{}/pairs", self.prefix), &[])
.await
}
pub async fn get(&self, symbol: &str) -> Result<SpotPair> {
self.http
.get(&format!("{}/pairs/{}", self.prefix, symbol), &[])
.await
}
}
#[derive(Debug, Default, Clone)]
pub struct SpotTwapParams {
pub start: Option<Timestamp>,
pub end: Option<Timestamp>,
pub cursor: Option<String>,
pub limit: Option<i64>,
}
#[derive(Debug, Clone)]
pub struct SpotTwapResource {
http: HttpClient,
prefix: String,
}
impl SpotTwapResource {
pub(crate) fn new(http: HttpClient, prefix: &str) -> Self {
Self {
http,
prefix: prefix.to_string(),
}
}
pub async fn by_symbol(
&self,
symbol: &str,
params: SpotTwapParams,
) -> Result<CursorResponse<Vec<SpotTwapStatus>>> {
let mut qp = vec![];
if let Some(s) = params.start {
qp.push(("start", s.to_millis().to_string()));
}
if let Some(e) = params.end {
qp.push(("end", e.to_millis().to_string()));
}
if let Some(c) = params.cursor {
qp.push(("cursor", c));
}
if let Some(l) = params.limit {
qp.push(("limit", l.to_string()));
}
let (data, next_cursor) = self
.http
.get_with_cursor(&format!("{}/twap/{}", self.prefix, symbol), &qp)
.await?;
Ok(CursorResponse { data, next_cursor })
}
pub async fn by_user(
&self,
user: &str,
params: SpotTwapParams,
) -> Result<CursorResponse<Vec<SpotTwapStatus>>> {
let mut qp = vec![];
if let Some(s) = params.start {
qp.push(("start", s.to_millis().to_string()));
}
if let Some(e) = params.end {
qp.push(("end", e.to_millis().to_string()));
}
if let Some(c) = params.cursor {
qp.push(("cursor", c));
}
if let Some(l) = params.limit {
qp.push(("limit", l.to_string()));
}
let (data, next_cursor) = self
.http
.get_with_cursor(&format!("{}/twap/user/{}", self.prefix, user), &qp)
.await?;
Ok(CursorResponse { data, next_cursor })
}
}