Skip to main content

steam_webapi_rust_sdk/util/
mod.rs

1use std::collections::HashMap;
2use std::env;
3use std::time::{SystemTime, UNIX_EPOCH};
4use serde_json::Value;
5use url_build_parse::{build_url, UrlAuthority, UrlComponents};
6
7pub fn get_steam_web_api_key() -> String {
8    let boxed_steam_web_api_key = env::var("STEAM_WEBAPI_KEY");
9    if boxed_steam_web_api_key.is_err() {
10        println!("To use this SDK please specify STEAM_WEBAPI_KEY environment variable");
11        return "".to_string();
12    }
13    let steam_web_api_key = boxed_steam_web_api_key.unwrap();
14    let _key = ["STEAM_WEB_API_KEY is ", &steam_web_api_key].join("");
15
16    return steam_web_api_key;
17}
18
19pub fn build_api_url(interface: &str, method: &str, version: &str, _parameters: HashMap<String, String>) -> String {
20    let slash_separator = "/";
21    let parameters_start = "?";
22    let parameter_equals = "=";
23    let key_parameter = "key";
24
25    let steam_api_url = "https://api.steampowered.com";
26
27    let steam_web_api_key = get_steam_web_api_key();
28
29    let url = [steam_api_url, slash_separator, interface, slash_separator, method, slash_separator, version, parameters_start, key_parameter, parameter_equals, &steam_web_api_key].join("");
30
31    return url
32}
33
34pub fn get_cache_dir_path() -> String {
35    ["steam-webapi-cache".to_string()].join("/")
36}
37
38pub fn as_unix_timestamp(system_time: SystemTime) -> u64 {
39    let since_the_epoch = system_time.duration_since(UNIX_EPOCH).expect("Time went backwards");
40    let unix_timestamp = since_the_epoch.as_secs() * 1000 + since_the_epoch.subsec_nanos() as u64 / 1_000_000;
41    unix_timestamp
42}
43
44pub fn get_json_filetype() -> String {
45    "json".to_string()
46}
47
48/// Builds a `https://api.steampowered.com/<interface>/<method>/<version>?<params>` URL.
49pub fn build_steam_api_url(interface: &str, method: &str, version: &str, params_map: HashMap<String, String>) -> String {
50    let path = ["/".to_string(), interface.to_string(), "/".to_string(), method.to_string(), "/".to_string(), version.to_string()].join("");
51
52    let url_builder = UrlComponents {
53        scheme: "https".to_string(),
54        authority: Some(UrlAuthority {
55            user_info: None,
56            host: "api.steampowered.com".to_string(),
57            port: None
58        }),
59        path,
60        query: Some(params_map),
61        fragment: None
62    };
63
64    build_url(url_builder).unwrap()
65}
66
67/// Reads an unsigned integer field from a JSON object, defaulting to 0 if absent or the wrong type.
68pub fn json_u64(value: &Value, key: &str) -> u64 {
69    value.get(key).and_then(Value::as_u64).unwrap_or(0)
70}
71
72/// Reads a signed integer field from a JSON object, defaulting to 0 if absent or the wrong type.
73pub fn json_i64(value: &Value, key: &str) -> i64 {
74    value.get(key).and_then(Value::as_i64).unwrap_or(0)
75}
76
77/// Reads a floating point field from a JSON object, defaulting to 0.0 if absent or the wrong type.
78pub fn json_f64(value: &Value, key: &str) -> f64 {
79    value.get(key).and_then(Value::as_f64).unwrap_or(0.0)
80}
81
82/// Reads a string field from a JSON object, defaulting to an empty string if absent or the wrong type.
83pub fn json_str(value: &Value, key: &str) -> String {
84    value.get(key).and_then(Value::as_str).unwrap_or("").to_string()
85}
86
87/// Reads a boolean field from a JSON object, defaulting to false if absent or the wrong type.
88pub fn json_bool(value: &Value, key: &str) -> bool {
89    value.get(key).and_then(Value::as_bool).unwrap_or(false)
90}