a
File size: 1,321 Bytes
2774836
 
0fcb5c2
65c3047
0fcb5c2
2774836
0fcb5c2
 
 
2774836
0fcb5c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2774836
0fcb5c2
 
 
65c3047
 
 
 
2774836
65c3047
 
 
 
0fcb5c2
 
 
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
use std::sync::Arc;

use crate::error::AppError;
use crate::services::retry::{self, OMDB_RETRY};
use serde::Deserialize;
use zeroize::Zeroizing;

#[derive(Clone)]
pub struct OmdbClient {
    api_key: Arc<Zeroizing<String>>,
    http: reqwest::Client,
}

#[derive(Debug, Deserialize)]
pub struct OmdbResponse {
    #[serde(rename = "Ratings", default)]
    pub ratings: Vec<OmdbRating>,
    #[serde(rename = "imdbRating")]
    pub imdb_rating: Option<String>,
    #[serde(rename = "Metascore")]
    pub metascore: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct OmdbRating {
    #[serde(rename = "Source")]
    pub source: String,
    #[serde(rename = "Value")]
    pub value: String,
}

impl OmdbClient {
    pub fn new(api_key: String, http: reqwest::Client) -> Self {
        Self { api_key: Arc::new(Zeroizing::new(api_key)), http }
    }

    pub async fn get_ratings(&self, imdb_id: &str) -> Result<OmdbResponse, AppError> {
        let imdb_id = imdb_id.to_owned();
        let resp = retry::send_with_retry(&OMDB_RETRY, || {
            self.http
                .get("https://www.omdbapi.com/")
                .query(&[("apikey", self.api_key.as_str()), ("i", imdb_id.as_str())])
                .send()
        })
        .await?
        .error_for_status()?;
        Ok(resp.json().await?)
    }
}