polyte_data/api/
open_interest.rs

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