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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
use crate::error::Error;
use crate::types::*;
use std::time::Duration;
const DEFAULT_BASE_URL: &str = "https://api-public.cs-prod.leetify.com";
const API_KEY_HEADER: &str = "_leetify_key";
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
/// Builder for creating a customized `Client`
pub struct ClientBuilder {
base_url: Option<String>,
api_key: Option<String>,
timeout: Option<Duration>,
client_builder: reqwest::ClientBuilder,
}
impl ClientBuilder {
/// Create a new builder with default settings
pub fn new() -> Self {
Self {
base_url: None,
api_key: None,
timeout: Some(DEFAULT_TIMEOUT),
client_builder: reqwest::Client::builder(),
}
}
/// Set a custom base URL for the API
///
/// # Examples
///
/// ```no_run
/// use leetify::Client;
///
/// let client = Client::builder()
/// .base_url("https://custom-api.example.com")
/// .build()
/// .unwrap();
/// ```
pub fn base_url(mut self, url: impl Into<String>) -> Self {
self.base_url = Some(url.into());
self
}
/// Set the API key
///
/// # Examples
///
/// ```no_run
/// use leetify::Client;
///
/// let client = Client::builder()
/// .api_key("your-api-key")
/// .build()
/// .unwrap();
/// ```
pub fn api_key(mut self, key: impl Into<String>) -> Self {
self.api_key = Some(key.into());
self
}
/// Set the request timeout
///
/// # Examples
///
/// ```no_run
/// use leetify::Client;
/// use std::time::Duration;
///
/// let client = Client::builder()
/// .timeout(Duration::from_secs(60))
/// .build()
/// .unwrap();
/// ```
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self.client_builder = self.client_builder.timeout(timeout);
self
}
/// Configure the underlying reqwest client builder
///
/// This allows advanced configuration of the HTTP client.
pub fn client_builder(mut self, builder: reqwest::ClientBuilder) -> Self {
self.client_builder = builder;
self
}
/// Build the client
///
/// # Examples
///
/// ```no_run
/// use leetify::Client;
///
/// let client = Client::builder()
/// .api_key("your-api-key")
/// .build()?;
/// # Ok::<(), leetify::Error>(())
/// ```
pub fn build(self) -> Result<Client, Error> {
let client = self
.client_builder
.timeout(self.timeout.unwrap_or(DEFAULT_TIMEOUT))
.build()
.map_err(Error::Http)?;
Ok(Client {
client,
base_url: self
.base_url
.unwrap_or_else(|| DEFAULT_BASE_URL.to_string()),
api_key: self.api_key,
})
}
}
impl Default for ClientBuilder {
fn default() -> Self {
Self::new()
}
}
/// Client for interacting with the Leetify Public CS API
pub struct Client {
client: reqwest::Client,
base_url: String,
api_key: Option<String>,
}
impl Client {
/// Create a new client without an API key
///
/// Requests without an API key are subject to increased rate limits.
///
/// # Examples
///
/// ```no_run
/// use leetify::Client;
///
/// let client = Client::new();
/// ```
pub fn new() -> Self {
ClientBuilder::new()
.build()
.expect("Failed to create default client")
}
/// Create a new client with an API key
///
/// API keys can be obtained at https://leetify.com/app/developer
///
/// # Examples
///
/// ```no_run
/// use leetify::Client;
///
/// let client = Client::with_api_key("your-api-key".to_string());
/// ```
pub fn with_api_key(api_key: String) -> Self {
ClientBuilder::new()
.api_key(api_key)
.build()
.expect("Failed to create client with API key")
}
/// Create a builder for customizing the client configuration
///
/// # Examples
///
/// ```no_run
/// use leetify::Client;
/// use std::time::Duration;
///
/// let client = Client::builder()
/// .api_key("your-api-key")
/// .timeout(Duration::from_secs(60))
/// .base_url("https://custom-api.example.com")
/// .build()
/// .unwrap();
/// ```
pub fn builder() -> ClientBuilder {
ClientBuilder::new()
}
/// Get player profile
///
/// # Arguments
/// * `id` - Player id (either Steam64 ID or Leetify ID)
///
/// # Examples
///
/// ```no_run
/// # use leetify::{Client, Id, Steam64Id, LeetifyId};
/// # async fn example() -> Result<(), leetify::Error> {
/// let client = Client::new();
///
/// // Using Steam64 ID
/// let profile = client.get_profile(Id::Steam64("76561198000000000".into())).await?;
///
/// // Using Leetify ID (UUID format)
/// let profile = client.get_profile(Id::Leetify("5ea07280-2399-4c7e-88ab-f2f7db0c449f".into())).await?;
///
/// // Using automatic conversion with type annotation
/// let id: Id = "76561198000000000".into();
/// let profile = client.get_profile(id).await?;
/// # Ok(())
/// # }
/// ```
pub async fn get_profile(&self, id: impl Into<Id>) -> Result<ProfileResponse, Error> {
let id = id.into();
let url = format!("{}/v3/profile", self.base_url);
let query_params = self.build_profile_query_params(&id);
let mut request = self.client.get(&url);
if !query_params.is_empty() {
request = request.query(&query_params);
}
request = self.add_api_key_header(request);
let response = request.send().await?;
self.handle_response(response).await
}
/// Get player match history
///
/// # Arguments
/// * `id` - Player id (either Steam64 ID or Leetify ID)
///
/// # Examples
///
/// ```no_run
/// # use leetify::{Client, Id};
/// # async fn example() -> Result<(), leetify::Error> {
/// let client = Client::new();
///
/// // Get matches by Steam64 ID
/// let matches = client.get_profile_matches(Id::Steam64("76561198000000000".into())).await?;
///
/// // Get matches by Leetify ID (UUID format)
/// let matches = client.get_profile_matches(Id::Leetify("5ea07280-2399-4c7e-88ab-f2f7db0c449f".into())).await?;
///
/// // Using automatic conversion with type annotation
/// let id: Id = "76561198000000000".into();
/// let matches = client.get_profile_matches(id).await?;
/// # Ok(())
/// # }
/// ```
pub async fn get_profile_matches(
&self,
id: impl Into<Id>,
) -> Result<Vec<MatchDetailsResponse>, Error> {
let id = id.into();
let url = format!("{}/v3/profile/matches", self.base_url);
let query_params = self.build_profile_query_params(&id);
let mut request = self.client.get(&url);
if !query_params.is_empty() {
request = request.query(&query_params);
}
request = self.add_api_key_header(request);
let response = request.send().await?;
self.handle_response(response).await
}
/// Get match details by game ID
///
/// # Arguments
/// * `game_id` - The game ID (Leetify match ID)
///
/// # Examples
///
/// ```no_run
/// # use leetify::Client;
/// # async fn example() -> Result<(), leetify::Error> {
/// let client = Client::new();
///
/// let match_details = client.get_match_by_game_id("match-id-123".to_string()).await?;
/// # Ok(())
/// # }
/// ```
pub async fn get_match_by_game_id(
&self,
game_id: String,
) -> Result<MatchDetailsResponse, Error> {
let url = format!("{}/v2/matches/{}", self.base_url, game_id);
let request = self.client.get(&url);
let request = self.add_api_key_header(request);
let response = request.send().await?;
self.handle_response(response).await
}
/// Get match details by data source and data source ID
///
/// # Arguments
/// * `data_source` - The data source (e.g., "faceit", "matchmaking")
/// * `data_source_id` - The data source specific match ID
///
/// # Examples
///
/// ```no_run
/// # use leetify::{Client, DataSource};
/// # async fn example() -> Result<(), leetify::Error> {
/// let client = Client::new();
///
/// // Using DataSource enum
/// let match_details = client
/// .get_match_by_data_source(DataSource::FACEIT, "faceit-match-id")
/// .await?;
///
/// // Using string (will be converted to DataSource)
/// let match_details = client
/// .get_match_by_data_source("matchmaking", "matchmaking-match-id")
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn get_match_by_data_source(
&self,
data_source: impl Into<DataSource>,
data_source_id: impl AsRef<str>,
) -> Result<MatchDetailsResponse, Error> {
let data_source = data_source.into();
let url = format!(
"{}/v2/matches/{}/{}",
self.base_url,
data_source.as_str(),
data_source_id.as_ref()
);
let request = self.client.get(&url);
let request = self.add_api_key_header(request);
let response = request.send().await?;
self.handle_response(response).await
}
/// Validate the API key
///
/// Returns:
/// - `Ok(())` if the key is valid
/// - `Err(Error::InvalidApiKey)` if the key is invalid or missing
/// - `Err(Error::ServerError)` if there was a server error
///
/// # Examples
///
/// ```no_run
/// # use leetify::Client;
/// # async fn example() -> Result<(), leetify::Error> {
/// let client = Client::with_api_key("your-api-key".to_string());
///
/// match client.validate_api_key().await {
/// Ok(()) => println!("API key is valid"),
/// Err(e) => eprintln!("API key validation failed: {}", e),
/// }
/// # Ok(())
/// # }
/// ```
pub async fn validate_api_key(&self) -> Result<(), Error> {
let url = format!("{}/api-key/validate", self.base_url);
let request = self.client.get(&url);
let request = self.add_api_key_header(request);
let response = request.send().await?;
let status = response.status();
match status.as_u16() {
200 => Ok(()),
401 => Err(Error::InvalidApiKey),
500 => Err(Error::ServerError),
_ => Err(Error::Api(
status.as_u16(),
format!("Unexpected status code: {}", status),
)),
}
}
fn build_profile_query_params(&self, id: &Id) -> Vec<(&'static str, String)> {
match id {
Id::Steam64(id) => {
vec![("steam64_id", id.as_ref().to_string())]
}
Id::Leetify(id) => {
vec![("id", id.as_ref().to_string())]
}
}
}
fn add_api_key_header(&self, request: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
if let Some(ref api_key) = self.api_key {
request.header(API_KEY_HEADER, api_key.as_str())
} else {
request
}
}
async fn handle_response<T>(&self, response: reqwest::Response) -> Result<T, Error>
where
T: serde::de::DeserializeOwned,
{
let status = response.status();
let response_text = response.text().await?;
if !status.is_success() {
let status_code = status.as_u16();
return match status_code {
401 => Err(Error::InvalidApiKey),
500 => Err(Error::ServerError),
_ => Err(Error::Api(status_code, response_text)),
};
}
// Try to parse JSON, but provide better error message if it fails
match serde_json::from_str::<T>(&response_text) {
Ok(json) => Ok(json),
Err(e) => {
// If JSON parsing fails, create a more descriptive error
// We'll wrap it in an Api error with the response text
Err(Error::Api(
status.as_u16(),
format!(
"Failed to parse JSON response: {}. Response body: {}",
e, response_text
),
))
}
}
}
}
impl Default for Client {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_player_id_conversion() {
// Test Steam64 ID conversion (numeric, 17 digits)
let id: Id = "76561198283431555".into();
assert!(matches!(id, Id::Steam64(_)));
// Test Leetify ID conversion (UUID format)
let id: Id = "5ea07280-2399-4c7e-88ab-f2f7db0c449f".into();
assert!(matches!(id, Id::Leetify(_)));
// Test explicit Steam64 variant
let id = Id::Steam64("76561198283431555".into());
assert!(matches!(id, Id::Steam64(_)));
// Test explicit Leetify variant
let id = Id::Leetify("5ea07280-2399-4c7e-88ab-f2f7db0c449f".into());
assert!(matches!(id, Id::Leetify(_)));
// Test that numeric strings >= 15 digits are treated as Steam64
let id: Id = "76561198000000000".into();
assert!(matches!(id, Id::Steam64(_)));
// Test that UUID format strings are treated as Leetify
let id: Id = "00000000-0000-0000-0000-000000000000".into();
assert!(matches!(id, Id::Leetify(_)));
}
#[test]
fn test_client_builder() {
let builder = ClientBuilder::new();
assert!(builder.base_url.is_none());
assert!(builder.api_key.is_none());
assert!(builder.timeout.is_some());
}
#[test]
fn test_client_builder_with_options() {
let client = ClientBuilder::new()
.base_url("https://test.example.com")
.api_key("test-key")
.timeout(Duration::from_secs(60))
.build()
.unwrap();
assert_eq!(client.base_url, "https://test.example.com");
assert_eq!(client.api_key, Some("test-key".to_string()));
}
#[test]
fn test_steam64_id_conversion() {
let id: Steam64Id = "76561198000000000".into();
assert_eq!(id.as_ref(), "76561198000000000");
}
#[test]
fn test_leetify_id_conversion() {
let id: LeetifyId = "user-123".into();
assert_eq!(id.as_ref(), "user-123");
}
#[test]
fn test_data_source_conversion() {
let ds: DataSource = "faceit".into();
assert!(matches!(ds, DataSource::FACEIT));
assert_eq!(ds.as_str(), "faceit");
let ds: DataSource = "matchmaking".into();
assert!(matches!(ds, DataSource::Matchmaking));
assert_eq!(ds.as_str(), "matchmaking");
let ds: DataSource = "other".into();
match ds {
DataSource::Other(s) => assert_eq!(s, "other"),
_ => panic!("Expected Other variant"),
}
}
}