1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//! CLOB API client implementation.
use reqwest::{Client as HttpClient, Response};
use reqwest_middleware::ClientWithMiddleware;
use tracing::trace;
use url::Url;
use crate::client::http::{DEFAULT_MAX_RETRIES, HttpClientConfig, wrap_with_retry};
use crate::error::{PolymarketError, Result};
/// Default base URL for the Polymarket CLOB API.
pub const DEFAULT_BASE_URL: &str = "https://clob.polymarket.com";
/// Maximum error message length to prevent sensitive data leakage.
const MAX_ERROR_MESSAGE_LEN: usize = 500;
/// Client for interacting with the Polymarket CLOB API.
#[derive(Debug, Clone)]
pub struct Client {
/// HTTP client with retry middleware.
pub(super) http_client: ClientWithMiddleware,
/// Base URL for the API (validated URL).
pub(super) base_url: Url,
}
impl Client {
/// Creates a new CLOB API client with the default base URL.
///
/// # Example
///
/// ```no_run
/// use polymarket_hft::client::polymarket::clob::Client;
///
/// let client = Client::new();
/// ```
pub fn new() -> Self {
Self::with_base_url(DEFAULT_BASE_URL).expect("default CLOB base URL is valid")
}
/// Creates a new CLOB API client with a custom base URL.
///
/// # Arguments
///
/// * `base_url` - The base URL for the API.
///
/// # Returns
///
/// Returns `Ok(Client)` if the URL is valid, or an error if parsing fails.
///
/// # Example
///
/// ```no_run
/// use polymarket_hft::client::polymarket::clob::Client;
///
/// let client = Client::with_base_url("https://custom-clob.example.com").unwrap();
/// ```
pub fn with_base_url(base_url: &str) -> Result<Self> {
let url = Url::parse(base_url)?;
let http_client = HttpClientConfig::default()
.build()
.map_err(|e| PolymarketError::other(format!("failed to create HTTP client: {}", e)))?;
Ok(Self {
http_client,
base_url: url,
})
}
/// Creates a new CLOB API client with a custom base URL and retry configuration.
///
/// # Arguments
///
/// * `base_url` - The base URL for the API.
/// * `max_retries` - Maximum number of retry attempts for transient failures.
///
/// # Returns
///
/// Returns `Ok(Client)` if the URL is valid, or an error if parsing fails.
pub fn with_retries(base_url: &str, max_retries: u32) -> Result<Self> {
let url = Url::parse(base_url)?;
let http_client = HttpClientConfig::default()
.with_max_retries(max_retries)
.build()
.map_err(|e| PolymarketError::other(format!("failed to create HTTP client: {}", e)))?;
Ok(Self {
http_client,
base_url: url,
})
}
/// Creates a new CLOB API client with an existing HTTP client.
///
/// The provided client will be wrapped with retry middleware using default settings.
///
/// # Arguments
///
/// * `http_client` - An existing reqwest HTTP client.
///
/// # Example
///
/// ```no_run
/// use polymarket_hft::client::polymarket::clob::Client;
/// use reqwest::Client as HttpClient;
///
/// let http_client = HttpClient::builder()
/// .timeout(std::time::Duration::from_secs(30))
/// .build()
/// .unwrap();
///
/// let client = Client::with_http_client(http_client);
/// ```
pub fn with_http_client(http_client: HttpClient) -> Self {
Self {
http_client: wrap_with_retry(http_client, DEFAULT_MAX_RETRIES),
base_url: Url::parse(DEFAULT_BASE_URL).expect("default CLOB base URL is valid"),
}
}
/// Creates a new CLOB API client with an existing middleware-enabled HTTP client.
///
/// # Arguments
///
/// * `http_client` - An existing reqwest-middleware HTTP client.
pub fn with_middleware_client(http_client: ClientWithMiddleware) -> Self {
Self {
http_client,
base_url: Url::parse(DEFAULT_BASE_URL).expect("default CLOB base URL is valid"),
}
}
/// Checks if the response is successful and returns an appropriate error if not.
pub(super) async fn check_response(&self, response: Response) -> Result<Response> {
let status = response.status();
trace!(status = %status, "received HTTP response");
if status.is_success() {
return Ok(response);
}
let status_code = status.as_u16();
let message = response
.text()
.await
.unwrap_or_default()
.chars()
.take(MAX_ERROR_MESSAGE_LEN)
.collect::<String>();
let error_msg = match status_code {
400..=499 => format!("client error ({}): {}", status_code, message),
500..=599 => {
if message.is_empty() {
format!("server error ({})", status_code)
} else {
format!("server error ({}): {}", status_code, message)
}
}
_ => format!("unexpected status ({})", status_code),
};
trace!(error = %error_msg, "HTTP request failed");
Err(PolymarketError::api(error_msg))
}
/// Builds a URL for the given path, preserving any base path prefix.
pub(super) fn build_url(&self, path: &str) -> Url {
let mut url = self.base_url.clone();
let base_path = url.path().trim_end_matches('/');
let suffix = path.trim_start_matches('/');
let merged = if base_path.is_empty() {
format!("/{}", suffix)
} else {
format!("{}/{}", base_path, suffix)
};
url.set_path(&merged);
url
}
// =========================================================================
// Server Endpoints
// =========================================================================
/// Health check - verifies the server is operational.
///
/// # Returns
///
/// Returns `Ok(())` if the server is healthy.
///
/// # Example
///
/// ```no_run
/// use polymarket_hft::client::polymarket::clob::Client;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new();
/// client.get_ok().await?;
/// println!("Server is healthy!");
/// Ok(())
/// }
/// ```
pub async fn get_ok(&self) -> Result<()> {
let url = self.build_url("");
trace!(url = %url, method = "GET", "sending health check request");
let response = self.http_client.get(url).send().await?;
self.check_response(response).await?;
trace!("server health check passed");
Ok(())
}
/// Gets the current server time in Unix milliseconds.
///
/// This is useful for synchronizing timestamps for order signatures.
///
/// # Returns
///
/// Returns the server time as Unix milliseconds.
///
/// # Example
///
/// ```no_run
/// use polymarket_hft::client::polymarket::clob::Client;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new();
/// let time = client.get_server_time().await?;
/// println!("Server time: {} ms", time);
/// Ok(())
/// }
/// ```
pub async fn get_server_time(&self) -> Result<u64> {
let url = self.build_url("time");
trace!(url = %url, method = "GET", "sending server time request");
let response = self.http_client.get(url).send().await?;
let response = self.check_response(response).await?;
#[derive(serde::Deserialize)]
struct TimeResponse {
time: u64,
}
let result: TimeResponse = response.json().await?;
trace!(time = result.time, "received server time");
Ok(result.time)
}
}
impl Default for Client {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg_attr(
target_os = "macos",
ignore = "reqwest native TLS unavailable in sandboxed macOS tests"
)]
#[test]
fn test_client_creation() {
let client = Client::new();
assert!(client.base_url.as_str().starts_with(DEFAULT_BASE_URL));
}
#[cfg_attr(
target_os = "macos",
ignore = "reqwest native TLS unavailable in sandboxed macOS tests"
)]
#[test]
fn test_client_with_custom_url() {
let client = Client::with_base_url("https://example.com/").unwrap();
assert_eq!(client.base_url.as_str(), "https://example.com/");
}
#[test]
fn test_build_url_preserves_base_path_prefix() {
let client = Client::with_base_url("https://example.com/api/v1").unwrap();
let url = client.build_url("book");
assert_eq!(url.as_str(), "https://example.com/api/v1/book");
}
#[cfg_attr(
target_os = "macos",
ignore = "reqwest native TLS unavailable in sandboxed macOS tests"
)]
#[test]
fn test_client_with_invalid_url() {
let result = Client::with_base_url("not-a-valid-url");
assert!(result.is_err());
}
#[cfg_attr(
target_os = "macos",
ignore = "reqwest native TLS unavailable in sandboxed macOS tests"
)]
#[test]
fn test_client_with_http_client() {
let http_client = HttpClient::new();
let client = Client::with_http_client(http_client);
assert!(client.base_url.as_str().starts_with(DEFAULT_BASE_URL));
}
#[cfg_attr(
target_os = "macos",
ignore = "reqwest native TLS unavailable in sandboxed macOS tests"
)]
#[test]
fn test_default_trait() {
let client = Client::default();
assert!(client.base_url.as_str().starts_with(DEFAULT_BASE_URL));
}
}