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
//! The Steam API client

use crate::errors::SteamError;
use reqwest::blocking::Client;
use reqwest::StatusCode;
use serde::Serialize;
use serde_json::Value;

/// Common functions for a Steam API client.
pub trait ApiClient {
    /// Send a GET request and return JSON
    fn get_request<T: Serialize>(
        &self,
        endpoint: &str,
        query: Vec<(&str, T)>,
    ) -> Result<Value, SteamError>;
}

/// This struct holds the blocking reqwest client and is used to interact with the API.
pub struct SteamClient {
    /// A [`reqwest::blocking`] HTTP client
    client: Client,
    /// The dev's Steam API key
    api_key: String,
}

impl ApiClient for SteamClient {
    fn get_request<T: Serialize>(
        &self,
        endpoint: &str,
        query: Vec<(&str, T)>,
    ) -> Result<Value, SteamError> {
        let response = self
            .client
            .get(endpoint)
            .query(&[("key", self.api_key.clone())])
            .query(&query)
            .send();

        match response {
            Ok(r) => match r.status() {
                StatusCode::OK => Ok(r.json().unwrap()),  // we trust steam that we'll actually get json w/ a 200 response, so unwrap() is good enough
                StatusCode::UNAUTHORIZED => {
                    Err(SteamError::FailedRequest("Unauthorized. Either you have used an invalid API key, or the data you wanted to access is private".to_string()))
                }
                _ => Err(SteamError::FailedRequest(
                    "Steam could not process your request. Double-check your provided parameters (Steam ID, app ID, ...).".to_string(),
                )),
            },
            Err(_) => Err(SteamError::FailedRequest(
                "Something went wrong with your request".to_string(),
            )),
        }
    }
}

impl SteamClient {
    /// Returns a new SteamClient instance.
    pub fn new(api_key: String) -> Self {
        let client = reqwest::blocking::Client::new();
        SteamClient { client, api_key }
    }
}