steamgriddb_api 0.3.1

Rust client for steamgriddb.com API
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
use serde::de::DeserializeOwned;

use crate::{
    games::{get_game_by_steam_app_id_url, get_gameinfo_by_game_id_url, GameInfo},
    images::{
        get_images_by_game_id_url, get_images_by_game_ids_url, get_images_by_platform_id_url,
        get_images_by_platform_ids_url, Image, InnerImagesMultipleIdsResponse,
        InnerImagesSingleIdResponse,
    },
    query_parameters::{Platform, QueryType},
    response::{response_to_result, response_to_result_flat, SteamGridDbResult},
    search::{get_search_url, InnerSearchResult, SearchResult},
    steam_static::SteamStaticUrls,
};

/// This Client provides a convenient way to interact with the SteamGrid API.
///
/// The client calls the API using the [reqwest](https://crates.io/crates/reqwest) crate and parses the result using the [serde](https://crates.io/crates/serde) crate.
///
/// ### Examples
///
/// Searching for a game and getting images for it:
/// ```no_run
/// use steamgriddb_api::client::Client;
/// use steamgriddb_api::query_parameters::QueryType::Grid;
///
/// async fn example() -> Result<(), Box<dyn std::error::Error>> {
///     let client = Client::new("my_auth_key");
///     let games = client.search("Celeste").await?;
///     let first_game = games.iter().next().ok_or("No games found")?;
///     assert_eq!("Celeste", first_game.name);
///     let images = client.get_images_for_id(first_game.id, &Grid(None)).await?;
///     Ok(())
///  }
/// ```
pub struct Client {
    auth_key: String,
    base_url: String,
    #[cfg(feature = "async")]
    client: reqwest::Client,
    #[cfg(feature = "blocking")]
    client: reqwest::blocking::Client,
}

impl Client {
    /// Creates a new client with the given auth key.
    ///
    /// ### Examples
    /// ```
    /// use steamgriddb_api::client::Client;
    /// # fn main() {
    /// let client = Client::new("my_auth_key");
    /// assert_eq!("my_auth_key", client.get_auth_key());
    /// # }
    /// ```
    pub fn new<S>(auth_key: S) -> Self
    where
        S: Into<String>,
    {
        let default_base_url = "https://www.steamgriddb.com/api/v2";

        #[cfg(feature = "async")]
        let client = reqwest::Client::new();

        #[cfg(feature = "blocking")]
        let client = reqwest::blocking::Client::new();

        Self {
            auth_key: auth_key.into(),
            base_url: default_base_url.to_owned(),
            client,
        }
    }

    /// Sets the base url for the client.
    ///
    /// The default url is <https://www.steamgriddb.com/api/v2>
    ///
    /// ### Examples
    ///
    /// ```
    /// use steamgriddb_api::client::Client;
    /// # fn main() {
    /// let mut client = Client::new("my_auth_key");
    /// client.set_base_url("https://localhost:8080/api/v2");
    /// assert_eq!("https://localhost:8080/api/v2", client.base_url());
    /// # }
    /// ```
    pub fn set_base_url<S>(&mut self, base_url: S)
    where
        S: Into<String>,
    {
        self.base_url = base_url.into();
    }

    /// Gets the base url for the client.
    ///
    /// The default url is <https://www.steamgriddb.com/api/v2>
    ///
    /// ### Examples
    ///
    /// ```
    /// use steamgriddb_api::client::Client;
    /// # fn main() {
    /// let mut client = Client::new("my_auth_key");    
    /// assert_eq!("https://www.steamgriddb.com/api/v2", client.base_url());
    /// # }
    /// ```
    pub fn base_url(&'_ self) -> &'_ str {
        self.base_url.as_str()
    }

    /// Gets the auth key for the client.
    ///
    /// ### Examples
    ///
    /// ```
    /// use steamgriddb_api::client::Client;
    /// # fn main() {
    /// let client = Client::new("my_auth_key");
    /// assert_eq!("my_auth_key", client.get_auth_key());
    /// # }
    /// ```
    pub fn get_auth_key(&self) -> &'_ str {
        self.auth_key.as_str()
    }

    /// Sets the auth key for the client.
    ///
    /// ### Examples
    ///
    /// ```
    /// use steamgriddb_api::client::Client;
    /// # fn main() {
    /// let mut client = Client::new("my_auth_key");
    /// client.set_auth_key("another_key");
    /// assert_eq!("another_key", client.get_auth_key());
    /// # }
    /// ```
    pub fn set_auth_key<S>(&mut self, auth_key: S)
    where
        S: Into<String>,
    {
        self.auth_key = auth_key.into();
    }

    /// Fetches images given a game id and a query type.
    ///    
    /// ### Examples
    /// The Query type decides which kind of images to fetch.
    ///
    /// ```no_run
    /// use steamgriddb_api::client::Client;
    /// use steamgriddb_api::query_parameters::QueryType::*;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut client = Client::new("my_auth_key");
    /// let grid_images = client.get_images_for_id(7993, &Grid(None)).await?;
    /// let hero_images = client.get_images_for_id(7993, &Hero(None)).await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// Query parameters can be given to specify which images to fetch.
    ///
    /// ```no_run
    /// use steamgriddb_api::client::Client;
    /// use steamgriddb_api::query_parameters::GridDimentions::*;
    /// use steamgriddb_api::query_parameters::QueryType::*;
    /// use steamgriddb_api::query_parameters::GridQueryParameters;    
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut client = Client::new("my_auth_key");
    /// let mut parameters = GridQueryParameters::default();
    /// parameters.dimentions = Some(&[D600x900,D512x512]);
    /// let filtered_grid_images = client.get_images_for_id(7993, &Grid(Some(parameters))).await?;    
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(feature = "async")]
    pub async fn get_images_for_id(
        &self,
        game_id: usize,
        query: &QueryType<'_>,
    ) -> Result<Vec<Image>, Box<dyn std::error::Error>> {
        let url = get_images_by_game_id_url(self.base_url.as_str(), game_id, query);
        let response = self
            .make_request::<InnerImagesSingleIdResponse>(url.as_str())
            .await?;
        Ok(response_to_result(response)?)
    }

    #[cfg(feature = "blocking")]
    pub fn get_images_for_id(
        &self,
        game_id: usize,
        query: &QueryType<'_>,
    ) -> Result<Vec<Image>, Box<dyn std::error::Error>> {
        let url = get_images_by_game_id_url(self.base_url.as_str(), game_id, query);
        let response = self.make_request::<InnerImagesSingleIdResponse>(url.as_str())?;
        Ok(response_to_result(response)?)
    }
    /// Fetches images given a list game id's and a query type.
    ///
    /// The resulting list will be a SteamGridDbResult<Image> for each id.
    ///            
    /// ### Examples
    /// One image will be fetched for each id.
    ///
    /// ```no_run
    /// use steamgriddb_api::client::Client;
    /// use steamgriddb_api::query_parameters::QueryType::*;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut client = Client::new("my_auth_key");
    /// let ids = [7993,5153400];
    /// let grid_images = client.get_images_for_ids(&ids, &Grid(None)).await?;
    /// assert_eq!(ids.len(), grid_images.len());
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(feature = "async")]
    pub async fn get_images_for_ids(
        &self,
        game_id: &[usize],
        query: &QueryType<'_>,
    ) -> Result<Vec<SteamGridDbResult<Image>>, Box<dyn std::error::Error>> {
        if game_id.is_empty() {
            return Ok(Vec::new());
        }

        if game_id.len() == 1 {
            let single_result = self.get_images_for_id(game_id[0], query).await?;
            if !single_result.is_empty() {
                return Ok(vec![SteamGridDbResult::Ok(single_result[0].clone())]);
            }
        }

        let url = get_images_by_game_ids_url(self.base_url.as_str(), game_id, query);

        let resposse = self
            .make_request::<InnerImagesMultipleIdsResponse>(url.as_str())
            .await?;
        Ok(response_to_result_flat(resposse)?)
    }

    #[cfg(feature = "blocking")]
    pub fn get_images_for_ids(
        &self,
        game_id: &[usize],
        query: &QueryType<'_>,
    ) -> Result<Vec<SteamGridDbResult<Image>>, Box<dyn std::error::Error>> {
        if game_id.is_empty() {
            return Ok(Vec::new());
        }

        if game_id.len() == 1 {
            let single_result = self.get_images_for_id(game_id[0], query)?;
            if !single_result.is_empty() {
                return Ok(vec![SteamGridDbResult::Ok(single_result[0].clone())]);
            }
        }

        let url = get_images_by_game_ids_url(self.base_url.as_str(), game_id, query);

        let resposse = self.make_request::<InnerImagesMultipleIdsResponse>(url.as_str())?;
        Ok(response_to_result_flat(resposse)?)
    }

    /// Search for games given a search query.
    ///     
    /// The search query will be url encoded, so that it will be safe to use.           
    ///     
    /// ### Examples
    ///
    /// ```no_run
    /// use steamgriddb_api::client::Client;
    /// use steamgriddb_api::query_parameters::QueryType::*;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut client = Client::new("my_auth_key");    
    /// let search_results = client.search("Celeste").await?;
    /// let first_result = search_results.iter().next().ok_or("None found")?;
    /// assert_eq!(first_result.name, "Celeste");
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(feature = "async")]
    pub async fn search(
        &self,
        query: &str,
    ) -> Result<Vec<SearchResult>, Box<dyn std::error::Error>> {
        let url = get_search_url(self.base_url.as_str(), query);
        let response = self.make_request::<InnerSearchResult>(url.as_str()).await?;
        Ok(response_to_result(response)?)
    }

    #[cfg(feature = "blocking")]
    pub fn search(&self, query: &str) -> Result<Vec<SearchResult>, Box<dyn std::error::Error>> {
        let url = get_search_url(self.base_url.as_str(), query);
        let response = self.make_request::<InnerSearchResult>(url.as_str())?;
        Ok(response_to_result(response)?)
    }
    /// Fetches images given a platform type, a platform specific game id and a query type.
    ///    
    /// ### Examples
    ///    
    /// ```no_run
    /// use steamgriddb_api::client::Client;
    /// use steamgriddb_api::query_parameters::Platform::*;
    /// use steamgriddb_api::query_parameters::QueryType::*;
    /// use steamgriddb_api::query_parameters::GridQueryParameters;    
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut client = Client::new("my_auth_key");    
    /// let platform = EpicGameStore;
    /// let epic_games_images = client.get_images_for_platform_id(&platform, "Salt", &Grid(None)).await?;    
    /// # Ok(())
    /// # }
    /// ```

    #[cfg(feature = "async")]
    pub async fn get_images_for_platform_id(
        &self,
        platform: &Platform,
        game_id: &str,
        qeury: &QueryType<'_>,
    ) -> Result<Vec<Image>, Box<dyn std::error::Error>> {
        let url = get_images_by_platform_id_url(self.base_url.as_str(), platform, game_id, qeury);
        let response = self
            .make_request::<InnerImagesSingleIdResponse>(url.as_str())
            .await?;
        Ok(response_to_result(response)?)
    }

    #[cfg(feature = "blocking")]
    pub fn get_images_for_platform_id(
        &self,
        platform: &Platform,
        game_id: &str,
        qeury: &QueryType<'_>,
    ) -> Result<Vec<Image>, Box<dyn std::error::Error>> {
        let url = get_images_by_platform_id_url(self.base_url.as_str(), platform, game_id, qeury);
        let response = self.make_request::<InnerImagesSingleIdResponse>(url.as_str())?;
        Ok(response_to_result(response)?)
    }
    /// Fetches images given a platform type, a platform specific game ids and a query type.
    ///    
    /// The resulting list will be a SteamGridDbResult<Image> for each id.
    ///    
    /// ### Examples
    ///    
    /// ```no_run
    /// use steamgriddb_api::client::Client;
    /// use steamgriddb_api::query_parameters::Platform::*;
    /// use steamgriddb_api::query_parameters::QueryType::*;
    /// use steamgriddb_api::query_parameters::GridQueryParameters;    
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>>{
    /// let mut client = Client::new("my_auth_key");    
    /// let platform = EpicGameStore;
    /// let ids = ["Salt", "Turkey"];
    /// let epic_games_images = client.get_images_for_platform_ids(&platform, &ids, &Grid(None)).await?;  
    /// # Ok(())  
    /// # }
    /// ```
    #[cfg(feature = "async")]
    pub async fn get_images_for_platform_ids(
        &self,
        platform: &Platform,
        game_id: &[&str],
        qeury: &QueryType<'_>,
    ) -> Result<Vec<SteamGridDbResult<Image>>, Box<dyn std::error::Error>> {
        let url = get_images_by_platform_ids_url(self.base_url.as_str(), platform, game_id, qeury);
        let resposse = self
            .make_request::<InnerImagesMultipleIdsResponse>(url.as_str())
            .await?;
        Ok(response_to_result_flat(resposse)?)
    }

    #[cfg(feature = "blocking")]
    pub fn get_images_for_platform_ids(
        &self,
        platform: &Platform,
        game_id: &[&str],
        qeury: &QueryType<'_>,
    ) -> Result<Vec<SteamGridDbResult<Image>>, Box<dyn std::error::Error>> {
        let url = get_images_by_platform_ids_url(self.base_url.as_str(), platform, game_id, qeury);
        let resposse = self.make_request::<InnerImagesMultipleIdsResponse>(url.as_str())?;
        Ok(response_to_result_flat(resposse)?)
    }
    /// Fetch information about a game given a game id.
    ///    
    /// ### Examples
    ///    
    /// ```no_run
    /// use steamgriddb_api::client::Client;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut client = Client::new("my_auth_key");    
    /// let game_info = client.get_game_info_for_id(13136).await?;    
    /// assert_eq!(game_info.name, "Celeste");
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(feature = "async")]
    pub async fn get_game_info_for_id(
        &self,
        game_id: usize,
    ) -> Result<GameInfo, Box<dyn std::error::Error>> {
        let url = get_gameinfo_by_game_id_url(self.base_url.as_str(), game_id);
        let response = self.make_request::<GameInfo>(url.as_str()).await?;
        Ok(response)
    }

    #[cfg(feature = "blocking")]
    pub fn get_game_info_for_id(
        &self,
        game_id: usize,
    ) -> Result<GameInfo, Box<dyn std::error::Error>> {
        let url = get_gameinfo_by_game_id_url(self.base_url.as_str(), game_id);
        let response = self.make_request::<GameInfo>(url.as_str())?;
        Ok(response)
    }
    /// Fetch information about a game given a steam game id.
    ///    
    /// ### Examples
    ///    
    /// ```no_run
    /// use steamgriddb_api::client::Client;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut client = Client::new("my_auth_key");    
    /// let game_info = client.get_game_by_steam_app_id(361420).await?;    
    /// assert_eq!(game_info.name, "Astroneer");
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(feature = "async")]
    pub async fn get_game_by_steam_app_id(
        &self,
        steam_app_id: usize,
    ) -> Result<GameInfo, Box<dyn std::error::Error>> {
        let url = get_game_by_steam_app_id_url(self.base_url.as_str(), steam_app_id);
        let response = self.make_request::<GameInfo>(url.as_str()).await?;
        Ok(response)
    }

    #[cfg(feature = "blocking")]
    pub fn get_game_by_steam_app_id(
        &self,
        steam_app_id: usize,
    ) -> Result<GameInfo, Box<dyn std::error::Error>> {
        let url = get_game_by_steam_app_id_url(self.base_url.as_str(), steam_app_id);
        let response = self.make_request::<GameInfo>(url.as_str())?;
        Ok(response)
    }

    #[cfg(feature = "async")]
    async fn make_request<'de, T>(&self, url: &str) -> Result<T, Box<dyn std::error::Error>>
    where
        T: DeserializeOwned,
    {
        Ok(self
            .client
            .get(url)
            .bearer_auth(self.auth_key.as_str())
            .send()
            .await?
            .json::<T>()
            .await?)
    }

    #[cfg(feature = "blocking")]
    fn make_request<'de, T>(&self, url: &str) -> Result<T, Box<dyn std::error::Error>>
    where
        T: DeserializeOwned,
    {
        Ok(self
            .client
            .get(url)
            .bearer_auth(self.auth_key.as_str())
            .send()?
            .json::<T>()?)
    }

    /// Get a SteamStaticUrls that contains the expected urls for the official Steam store images.
    pub fn get_official_steam_images_static(steam_app_id: &str) -> SteamStaticUrls {
        SteamStaticUrls::new(steam_app_id)
    }

    /// Get a SteamStaticUrls that contains the expected urls for the official Steam store images.
    pub fn get_official_steam_images(&self, steam_app_id: &str) -> SteamStaticUrls {
        Self::get_official_steam_images_static(steam_app_id)
    }
}