polyte_gamma/
client.rs

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