polyte_data/api/
open_interest.rs

1use polyte_core::RequestError;
2use reqwest::Client;
3use url::Url;
4
5use crate::{
6    error::{DataApiError, Result},
7    types::OpenInterest,
8};
9
10/// OpenInterest namespace for open interest operations
11#[derive(Clone)]
12pub struct OpenInterestApi {
13    pub(crate) client: Client,
14    pub(crate) base_url: Url,
15}
16
17impl OpenInterestApi {
18    /// Get open interest for markets
19    pub fn get(&self) -> GetOpenInterest {
20        GetOpenInterest {
21            client: self.client.clone(),
22            base_url: self.base_url.clone(),
23            markets: None,
24        }
25    }
26}
27
28/// Request builder for getting open interest
29pub struct GetOpenInterest {
30    client: Client,
31    base_url: Url,
32    markets: Option<Vec<String>>,
33}
34
35impl GetOpenInterest {
36    /// Filter by specific market condition IDs
37    pub fn market(mut self, condition_ids: impl IntoIterator<Item = impl ToString>) -> Self {
38        let ids: Vec<String> = condition_ids.into_iter().map(|s| s.to_string()).collect();
39        if !ids.is_empty() {
40            self.markets = Some(ids);
41        }
42        self
43    }
44
45    /// Execute the request
46    pub async fn send(self) -> Result<Vec<OpenInterest>> {
47        let url = self.base_url.join("/oi")?;
48        let mut request = self.client.get(url);
49
50        if let Some(markets) = self.markets {
51            request = request.query(&[("market", markets.join(","))]);
52        }
53
54        let response = request.send().await?;
55        let status = response.status();
56
57        if !status.is_success() {
58            return Err(DataApiError::from_response(response).await);
59        }
60
61        let oi: Vec<OpenInterest> = response.json().await?;
62        Ok(oi)
63    }
64}