steamkit-rs 0.1.2

Rust port of SteamKit2, a .NET library that aims to interoperate with the Steam network.
Documentation
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//! Calling the Steam Web API.
//!
//! Interfaces have explicit methods ([`AsyncInterface::call`],
//! [`AsyncInterface::call_protobuf`], [`AsyncInterface::call_protobuf_with_request`])
//! rather than SteamKit2's dynamic `api.ISteamDirectory.GetCMListForConnect(...)`
//! calls. [`Interface`] is async as well.
//!
//! Arguments are an ordered [`WebApiArgs`] list, so the query string comes out
//! in a stable order.

use std::io::Cursor;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, OnceLock};
use std::time::Duration;

use base64::prelude::BASE64_STANDARD;
use base64::Engine;
use percent_encoding::{utf8_percent_encode, AsciiSet, NON_ALPHANUMERIC};

use crate::base::generated::steam_language::EResult;
use crate::error::SteamError;
use crate::types::key_value::KeyValue;

use super::web_api_request_exception::WebAPIRequestException;

/// The default base address used for the Steam Web API.
pub const DEFAULT_BASE_ADDRESS: &str = "https://api.steampowered.com/";

/// The default timeout used for Web API requests.
pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(100);

/// The characters `Uri.EscapeDataString` leaves alone: `A-Z`, `a-z`, `0-9`,
/// `-`, `_`, `.` and `~`. `percent_encoding` has no constant for exactly this
/// set.
fn uri_unreserved() -> &'static AsciiSet {
    static SET: OnceLock<AsciiSet> = OnceLock::new();

    SET.get_or_init(|| {
        NON_ALPHANUMERIC
            .remove(b'-')
            .remove(b'_')
            .remove(b'.')
            .remove(b'~')
    })
}

/// The value of a Web API argument.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum WebApiValue {
    /// Text; escaped like `Uri.EscapeDataString`.
    Text(String),
    /// Binary; every byte is escaped as `%XX`.
    Bytes(Vec<u8>),
    /// Left out of the request.
    Null,
}

impl From<String> for WebApiValue {
    /// A text value.
    fn from(value: String) -> Self {
        WebApiValue::Text(value)
    }
}

impl From<&str> for WebApiValue {
    /// A text value.
    fn from(value: &str) -> Self {
        WebApiValue::Text(value.to_string())
    }
}

impl From<Vec<u8>> for WebApiValue {
    /// A binary value.
    fn from(value: Vec<u8>) -> Self {
        WebApiValue::Bytes(value)
    }
}

impl From<i32> for WebApiValue {
    /// The number as text.
    fn from(value: i32) -> Self {
        WebApiValue::Text(value.to_string())
    }
}

impl From<u32> for WebApiValue {
    /// The number as text.
    fn from(value: u32) -> Self {
        WebApiValue::Text(value.to_string())
    }
}

impl From<i64> for WebApiValue {
    /// The number as text.
    fn from(value: i64) -> Self {
        WebApiValue::Text(value.to_string())
    }
}

impl From<u64> for WebApiValue {
    /// The number as text.
    fn from(value: u64) -> Self {
        WebApiValue::Text(value.to_string())
    }
}

impl From<bool> for WebApiValue {
    /// The boolean as text.
    fn from(value: bool) -> Self {
        WebApiValue::Text(value.to_string())
    }
}

/// A single argument of a Web API call.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WebApiArgument {
    /// The name of the argument.
    pub name: String,
    /// The value of the argument.
    pub value: WebApiValue,
}

impl WebApiArgument {
    /// Creates an argument with the given name and value.
    pub fn new(name: impl Into<String>, value: impl Into<WebApiValue>) -> Self {
        WebApiArgument {
            name: name.into(),
            value: value.into(),
        }
    }
}

/// The arguments of a Web API call.
pub type WebApiArgs = Vec<WebApiArgument>;

/// Builds the argument list of a Web API call. The arguments keep their order
/// in the query string.
pub fn web_api_args<const N: usize>(entries: [(&str, WebApiValue); N]) -> WebApiArgs {
    entries
        .into_iter()
        .map(|(name, value)| WebApiArgument::new(name, value))
        .collect()
}

/// A response to a Web API request.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WebAPIResponse<TResponse> {
    /// The result of the response.
    pub result: EResult,
    /// The body of the response.
    pub body: TResponse,
}

impl<TResponse> WebAPIResponse<TResponse> {
    /// Creates a response.
    pub fn new(result: EResult, body: TResponse) -> Self {
        WebAPIResponse { result, body }
    }
}

/// Calling the Steam Web API.
pub struct WebAPI;

impl WebAPI {
    /// The default base address of the Steam Web API. Use a `SteamConfiguration`
    /// or the `_with_base_address` functions for a different one.
    pub fn default_base_address() -> reqwest::Url {
        // A valid constant, so this can't fail.
        reqwest::Url::parse(DEFAULT_BASE_ADDRESS)
            .expect("DEFAULT_BASE_ADDRESS is a valid absolute URL")
    }

    /// The default timeout used for Web API requests.
    pub fn default_timeout() -> Duration {
        DEFAULT_TIMEOUT
    }

    /// An interface for calling the given Web API interface.
    pub fn get_interface(iface: &str, api_key: &str) -> Interface {
        Interface::new(
            create_default_http_client(),
            Self::default_base_address(),
            iface,
            api_key,
        )
    }

    /// An interface for calling the given Web API interface at `base_address`.
    pub fn get_interface_with_base_address(
        base_address: reqwest::Url,
        iface: &str,
        api_key: &str,
    ) -> Interface {
        Interface::new(
            create_default_http_client(),
            base_address,
            iface,
            api_key,
        )
    }

    /// An async interface for calling the given Web API interface.
    pub fn get_async_interface(iface: &str, api_key: &str) -> AsyncInterface {
        AsyncInterface::new(
            create_default_http_client(),
            Self::default_base_address(),
            iface,
            api_key,
        )
    }

    /// An async interface for calling the given Web API interface at `base_address`.
    pub fn get_async_interface_with_base_address(
        base_address: reqwest::Url,
        iface: &str,
        api_key: &str,
    ) -> AsyncInterface {
        AsyncInterface::new(
            create_default_http_client(),
            base_address,
            iface,
            api_key,
        )
    }
}

/// The default HTTP client for Web API interfaces.
fn create_default_http_client() -> reqwest::Client {
    reqwest::Client::builder()
        .timeout(DEFAULT_TIMEOUT)
        .build()
        .unwrap_or_else(|_| reqwest::Client::new())
}

/// One interface of the Web API. Wraps an [`AsyncInterface`].
pub struct Interface {
    inner: Arc<AsyncInterface>,
}

impl Interface {
    /// Creates an interface for the given Web API interface.
    pub fn new(
        http_client: reqwest::Client,
        base_address: reqwest::Url,
        iface: impl Into<String>,
        api_key: impl Into<String>,
    ) -> Self {
        Interface {
            inner: Arc::new(AsyncInterface::new(http_client, base_address, iface, api_key)),
        }
    }

    /// The async interface this wraps.
    pub fn async_interface(&self) -> &AsyncInterface {
        &self.inner
    }

    /// The name of the Web API interface, for example `ISteamDirectory`.
    pub fn iface(&self) -> &str {
        self.inner.iface()
    }

    /// The timeout for Web API requests.
    pub fn timeout(&self) -> Duration {
        self.inner.timeout()
    }

    /// Sets the timeout for Web API requests.
    pub fn set_timeout(&self, timeout: Duration) {
        self.inner.set_timeout(timeout);
    }

    /// Calls a Web API function.
    pub async fn call(
        &self,
        func: &str,
        version: i32,
        args: &[WebApiArgument],
    ) -> Result<KeyValue, SteamError> {
        self.inner.call(func, version, args).await
    }

    /// Calls a Web API function with the given HTTP method.
    pub async fn call_with_method(
        &self,
        method: reqwest::Method,
        func: &str,
        version: i32,
        args: &[WebApiArgument],
    ) -> Result<KeyValue, SteamError> {
        self.inner
            .call_with_method(method, func, version, args)
            .await
    }
}

/// One interface of the Web API.
pub struct AsyncInterface {
    http_client: reqwest::Client,
    base_address: reqwest::Url,
    iface: String,
    api_key: String,
    timeout_millis: AtomicU64,
}

impl AsyncInterface {
    /// Creates an interface for the given Web API interface.
    pub fn new(
        http_client: reqwest::Client,
        base_address: reqwest::Url,
        iface: impl Into<String>,
        api_key: impl Into<String>,
    ) -> Self {
        AsyncInterface {
            http_client,
            base_address,
            iface: iface.into(),
            api_key: api_key.into(),
            timeout_millis: AtomicU64::new(DEFAULT_TIMEOUT.as_millis() as u64),
        }
    }

    /// The name of the Web API interface, for example `ISteamDirectory`.
    pub fn iface(&self) -> &str {
        &self.iface
    }

    /// The API key used for authorized requests.
    pub fn api_key(&self) -> &str {
        &self.api_key
    }

    /// The base address of the Web API.
    pub fn base_address(&self) -> &reqwest::Url {
        &self.base_address
    }

    /// The timeout for Web API requests.
    pub fn timeout(&self) -> Duration {
        Duration::from_millis(self.timeout_millis.load(Ordering::Relaxed))
    }

    /// Sets the timeout for Web API requests.
    pub fn set_timeout(&self, timeout: Duration) {
        self.timeout_millis
            .store(timeout.as_millis() as u64, Ordering::Relaxed);
    }

    /// Calls a Web API function. The response is requested as VDF and parsed
    /// into a [`KeyValue`].
    pub async fn call(
        &self,
        func: &str,
        version: i32,
        args: &[WebApiArgument],
    ) -> Result<KeyValue, SteamError> {
        self.call_with_method(reqwest::Method::GET, func, version, args)
            .await
    }

    /// Calls a Web API function with the given HTTP method. The response is
    /// requested as VDF and parsed into a [`KeyValue`].
    pub async fn call_with_method(
        &self,
        method: reqwest::Method,
        func: &str,
        version: i32,
        args: &[WebApiArgument],
    ) -> Result<KeyValue, SteamError> {
        let response = self.call_internal(method, func, version, args, "vdf").await?;
        let body = response
            .bytes()
            .await
            .map_err(|error| map_reqwest_error(error, self.timeout()))?;

        let mut key_value = KeyValue::new();
        let mut cursor = Cursor::new(body.as_ref());

        key_value
            .read_as_text(&mut cursor)
            .map(|()| key_value)
            .map_err(|error| {
                SteamError::Malformed(format!(
                    "An internal error occurred when attempting to parse the response from the WebAPI server. \
                     This can indicate a change in the VDF format. {error}"
                ))
            })
    }

    /// Calls a Web API function and parses the protobuf response.
    pub async fn call_protobuf<T>(
        &self,
        method: reqwest::Method,
        func: &str,
        version: i32,
        args: &[WebApiArgument],
    ) -> Result<T, SteamError>
    where
        T: prost::Message + Default,
    {
        let response = self
            .call_internal(method, func, version, args, "protobuf_raw")
            .await?;
        let body = response
            .bytes()
            .await
            .map_err(|error| map_reqwest_error(error, self.timeout()))?;

        T::decode(body.as_ref()).map_err(SteamError::Protobuf)
    }

    /// Calls a Web API function with a protobuf request and parses the protobuf
    /// response. The request goes base64-encoded in `input_protobuf_encoded`;
    /// the result code comes from the `x-eresult` response header.
    pub async fn call_protobuf_with_request<TResponse, TRequest>(
        &self,
        method: reqwest::Method,
        func: &str,
        request: &TRequest,
        version: i32,
        extra_args: &[WebApiArgument],
    ) -> Result<WebAPIResponse<TResponse>, SteamError>
    where
        TResponse: prost::Message + Default,
        TRequest: prost::Message + Default,
    {
        let base64 = BASE64_STANDARD.encode(request.encode_to_vec());

        let mut args: WebApiArgs = Vec::with_capacity(extra_args.len() + 1);
        args.push(WebApiArgument::new(
            "input_protobuf_encoded",
            WebApiValue::Text(base64),
        ));

        for argument in extra_args {
            if !args.iter().any(|existing| existing.name == argument.name) {
                args.push(argument.clone());
            }
        }

        let response = self
            .call_internal(method, func, version, &args, "protobuf_raw")
            .await?;

        let result = response
            .headers()
            .get("x-eresult")
            .and_then(|value| value.to_str().ok())
            .and_then(|value| value.parse::<i32>().ok())
            .and_then(EResult::from_value)
            .unwrap_or(EResult::Invalid);

        let body = response
            .bytes()
            .await
            .map_err(|error| map_reqwest_error(error, self.timeout()))?;
        let body = TResponse::decode(body.as_ref()).map_err(SteamError::Protobuf)?;

        Ok(WebAPIResponse::new(result, body))
    }

    /// Sends the request and returns the raw response.
    async fn call_internal(
        &self,
        method: reqwest::Method,
        func: &str,
        version: i32,
        args: &[WebApiArgument],
        expected_format: &str,
    ) -> Result<reqwest::Response, SteamError> {
        let format_provided = args.iter().any(|argument| argument.name == "format");

        if format_provided {
            let format = args
                .iter()
                .find(|argument| argument.name == "format")
                .map(|argument| argument.value.clone())
                .unwrap_or(WebApiValue::Null);

            match format {
                WebApiValue::Text(text) if text == expected_format => {}
                other => {
                    let text = match other {
                        WebApiValue::Text(text) => text,
                        WebApiValue::Bytes(bytes) => String::from_utf8_lossy(&bytes).into_owned(),
                        WebApiValue::Null => String::new(),
                    };

                    return Err(SteamError::InvalidArgument(format!(
                        "Unsupported 'format' value '{text}'. Format must either be '{expected_format}' or omitted."
                    )));
                }
            }
        }

        let params = build_params(&self.api_key, expected_format, format_provided, args);
        let params_go_in_query_string = method == reqwest::Method::GET;

        let relative = format!("{}/{func}/v{version}/", self.iface);

        let mut url = self
            .base_address
            .join(&relative)
            .map_err(|error| SteamError::InvalidArgument(format!("Invalid Web API url: {error}")))?;

        let request = if params_go_in_query_string {
            url.set_query(Some(&params));
            self.http_client.request(method.clone(), url)
        } else {
            self.http_client
                .request(method.clone(), url)
                .header(
                    reqwest::header::CONTENT_TYPE,
                    "application/x-www-form-urlencoded",
                )
                .body(params)
        };

        let timeout = self.timeout();

        let response = match tokio::time::timeout(timeout, request.send()).await {
            Ok(result) => result.map_err(|error| map_reqwest_error(error, timeout))?,
            Err(_) => return Err(SteamError::Timeout(timeout)),
        };

        if !response.status().is_success() {
            return Err(WebAPIRequestException::status(
                response.status().as_u16(),
                response
                    .status()
                    .canonical_reason()
                    .unwrap_or("Unknown")
                    .to_string(),
            )
            .into());
        }

        Ok(response)
    }
}

/// Builds the query string: the API key first, then the format (unless the
/// caller passed one), then every argument as `&name=value`.
fn build_params(
    api_key: &str,
    expected_format: &str,
    format_provided: bool,
    args: &[WebApiArgument],
) -> String {
    let mut params = String::new();

    if !api_key.is_empty() && !args.iter().any(|argument| argument.name == "key") {
        params.push_str("key=");
        params.push_str(&escape_data_string(api_key));
        params.push('&');
    }

    if !format_provided {
        params.push_str("format=");
        params.push_str(expected_format);
    }

    for argument in args {
        params.push('&');
        params.push_str(&escape_data_string(&argument.name));
        params.push('=');

        match &argument.value {
            WebApiValue::Null => {}
            WebApiValue::Bytes(bytes) => params.push_str(&url_encode_bytes(bytes)),
            WebApiValue::Text(text) => params.push_str(&escape_data_string(text)),
        }
    }

    params
}

/// Escapes a string like `Uri.EscapeDataString`.
pub(crate) fn escape_data_string(value: &str) -> String {
    utf8_percent_encode(value, uri_unreserved()).to_string()
}

/// Percent-encodes every byte (`%XX`).
fn url_encode_bytes(bytes: &[u8]) -> String {
    let mut encoded = String::with_capacity(bytes.len() * 3);

    for byte in bytes {
        encoded.push('%');
        encoded.push_str(&format!("{byte:02X}"));
    }

    encoded
}

/// Maps a `reqwest` error to a [`SteamError`].
fn map_reqwest_error(error: reqwest::Error, timeout: Duration) -> SteamError {
    if error.is_timeout() {
        SteamError::Timeout(timeout)
    } else if error.is_connect() {
        SteamError::Connection(error.to_string())
    } else {
        SteamError::Other(error.to_string())
    }
}