Skip to main content

polyoxide_gamma/
client.rs

1use polyoxide_core::{HttpClient, HttpClientBuilder, DEFAULT_POOL_SIZE, DEFAULT_TIMEOUT_MS};
2use reqwest::Client;
3use url::Url;
4
5use crate::{
6    api::{
7        comments::Comments, events::Events, health::Health, markets::Markets, series::Series,
8        sports::Sports, tags::Tags, user::User,
9    },
10    error::GammaError,
11};
12
13const DEFAULT_BASE_URL: &str = "https://gamma-api.polymarket.com";
14
15/// Main Gamma API client
16#[derive(Clone)]
17pub struct Gamma {
18    pub(crate) client: Client,
19    pub(crate) base_url: Url,
20}
21
22impl Gamma {
23    /// Create a new Gamma client with default configuration
24    pub fn new() -> Result<Self, GammaError> {
25        Self::builder().build()
26    }
27
28    /// Create a builder for configuring the client
29    pub fn builder() -> GammaBuilder {
30        GammaBuilder::new()
31    }
32
33    /// Get markets namespace
34    pub fn markets(&self) -> Markets {
35        Markets {
36            client: self.client.clone(),
37            base_url: self.base_url.clone(),
38        }
39    }
40
41    /// Get events namespace
42    pub fn events(&self) -> Events {
43        Events {
44            client: self.client.clone(),
45            base_url: self.base_url.clone(),
46        }
47    }
48
49    /// Get series namespace
50    pub fn series(&self) -> Series {
51        Series {
52            client: self.client.clone(),
53            base_url: self.base_url.clone(),
54        }
55    }
56
57    /// Get tags namespace
58    pub fn tags(&self) -> Tags {
59        Tags {
60            client: self.client.clone(),
61            base_url: self.base_url.clone(),
62        }
63    }
64
65    /// Get sports namespace
66    pub fn sports(&self) -> Sports {
67        Sports {
68            client: self.client.clone(),
69            base_url: self.base_url.clone(),
70        }
71    }
72
73    /// Get comments namespace
74    pub fn comments(&self) -> Comments {
75        Comments {
76            client: self.client.clone(),
77            base_url: self.base_url.clone(),
78        }
79    }
80
81    /// Get user namespace
82    pub fn user(&self) -> User {
83        User {
84            client: self.client.clone(),
85            base_url: self.base_url.clone(),
86        }
87    }
88
89    /// Get health namespace
90    pub fn health(&self) -> Health {
91        Health {
92            client: self.client.clone(),
93            base_url: self.base_url.clone(),
94        }
95    }
96}
97
98/// Builder for configuring Gamma client
99pub struct GammaBuilder {
100    base_url: String,
101    timeout_ms: u64,
102    pool_size: usize,
103}
104
105impl GammaBuilder {
106    fn new() -> Self {
107        Self {
108            base_url: DEFAULT_BASE_URL.to_string(),
109            timeout_ms: DEFAULT_TIMEOUT_MS,
110            pool_size: DEFAULT_POOL_SIZE,
111        }
112    }
113
114    /// Set base URL for the API
115    pub fn base_url(mut self, url: impl Into<String>) -> Self {
116        self.base_url = url.into();
117        self
118    }
119
120    /// Set request timeout in milliseconds
121    pub fn timeout_ms(mut self, timeout: u64) -> Self {
122        self.timeout_ms = timeout;
123        self
124    }
125
126    /// Set connection pool size
127    pub fn pool_size(mut self, size: usize) -> Self {
128        self.pool_size = size;
129        self
130    }
131
132    /// Build the Gamma client
133    pub fn build(self) -> Result<Gamma, GammaError> {
134        let HttpClient { client, base_url } = HttpClientBuilder::new(&self.base_url)
135            .timeout_ms(self.timeout_ms)
136            .pool_size(self.pool_size)
137            .build()?;
138
139        Ok(Gamma { client, base_url })
140    }
141}
142
143impl Default for GammaBuilder {
144    fn default() -> Self {
145        Self::new()
146    }
147}