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
use super::{Api, Error, Result};
use http::StatusCode;
use reqwest::RequestBuilder;
use serde_json::Value;
use std::time::Duration;

static DEFAULT_USER_AGENT: &str = "api-podcast-rust";

/// Client for accessing Listen Notes API.
pub struct Client<'a> {
    /// HTTP client.
    client: reqwest::Client,
    /// API context.
    api: Api<'a>,
    /// User Agent Header for API calls.
    user_agent: &'a str,
}

#[derive(Debug)]
/// Response and request context for API call.
pub struct Response {
    /// HTTP response.
    pub response: reqwest::Response,
    /// HTTP request that resulted in this response.
    pub request: reqwest::Request,
}

impl Response {
    /// Get JSON data object from [`reqwest::Response`].
    pub async fn json(self) -> Result<Value> {
        Ok(self.response.json().await?)
    }
}

impl Client<'_> {
    /// Creates new Listen API Client.
    ///
    /// Uses default HTTP client with 30 second timeouts.
    ///
    /// To access production API:
    /// ```
    /// let client = podcast_api::Client::new(Some("YOUR-API-KEY"));
    /// ```
    /// To access mock API:
    /// ```
    /// let client = podcast_api::Client::new(None);
    /// ```
    pub fn new(id: Option<&str>) -> Client {
        Client {
            client: reqwest::ClientBuilder::new()
                .timeout(Duration::from_secs(30))
                .build()
                .expect("Client::new()"),
            api: if let Some(id) = id {
                Api::Production(id)
            } else {
                Api::Mock
            },
            user_agent: DEFAULT_USER_AGENT,
        }
    }

    /// Creates new Listen API Client with user provided HTTP Client.
    pub fn new_custom<'a>(client: reqwest::Client, id: Option<&'a str>, user_agent: Option<&'a str>) -> Client<'a> {
        Client {
            client,
            api: if let Some(id) = id {
                Api::Production(id)
            } else {
                Api::Mock
            },
            user_agent: if let Some(user_agent) = user_agent {
                user_agent
            } else {
                DEFAULT_USER_AGENT
            },
        }
    }

    /// Calls [`GET /search`](https://www.listennotes.com/api/docs/#get-api-v2-search) with supplied parameters.
    pub async fn search(&self, parameters: &Value) -> Result<Response> {
        self.get("search", parameters).await
    }

    /// Calls [`GET /typeahead`](https://www.listennotes.com/api/docs/#get-api-v2-typeahead) with supplied parameters.
    pub async fn typeahead(&self, parameters: &Value) -> Result<Response> {
        self.get("typeahead", parameters).await
    }

    /// Calls [`GET /best_podcasts`](https://www.listennotes.com/api/docs/#get-api-v2-best_podcasts) with supplied parameters.
    pub async fn fetch_best_podcasts(&self, parameters: &Value) -> Result<Response> {
        self.get("best_podcasts", parameters).await
    }

    /// Calls [`GET /podcasts/{id}`](https://www.listennotes.com/api/docs/#get-api-v2-podcasts-id) with supplied parameters.
    pub async fn fetch_podcast_by_id(&self, id: &str, parameters: &Value) -> Result<Response> {
        self.get(&format!("podcasts/{}", id), parameters).await
    }

    /// Calls [`POST /podcasts`](https://www.listennotes.com/api/docs/#post-api-v2-podcasts) with supplied parameters.
    pub async fn batch_fetch_podcasts(&self, parameters: &Value) -> Result<Response> {
        self.post("podcasts", parameters).await
    }

    /// Calls [`GET /episodes/{id}`](https://www.listennotes.com/api/docs/#get-api-v2-episodes-id) with supplied parameters.
    pub async fn fetch_episode_by_id(&self, id: &str, parameters: &Value) -> Result<Response> {
        self.get(&format!("episodes/{}", id), parameters).await
    }

    /// Calls [`POST /episodes`](https://www.listennotes.com/api/docs/#post-api-v2-episodes) with supplied parameters.
    pub async fn batch_fetch_episodes(&self, parameters: &Value) -> Result<Response> {
        self.post("episodes", parameters).await
    }

    /// Calls [`GET /curated_podcasts/{id}`](https://www.listennotes.com/api/docs/#get-api-v2-curated_podcasts-id) with supplied parameters.
    pub async fn fetch_curated_podcasts_list_by_id(&self, id: &str, parameters: &Value) -> Result<Response> {
        self.get(&format!("curated_podcasts/{}", id), parameters).await
    }

    /// Calls [`GET /curated_podcasts`](https://www.listennotes.com/api/docs/#get-api-v2-curated_podcasts) with supplied parameters.
    pub async fn fetch_curated_podcasts_lists(&self, parameters: &Value) -> Result<Response> {
        self.get("curated_podcasts", parameters).await
    }

    /// Calls [`GET /genres`](https://www.listennotes.com/api/docs/#get-api-v2-genres) with supplied parameters.
    pub async fn fetch_podcast_genres(&self, parameters: &Value) -> Result<Response> {
        self.get("genres", parameters).await
    }

    /// Calls [`GET /regions`](https://www.listennotes.com/api/docs/#get-api-v2-regions) with supplied parameters.
    pub async fn fetch_podcast_regions(&self, parameters: &Value) -> Result<Response> {
        self.get("regions", parameters).await
    }

    /// Calls [`GET /languages`](https://www.listennotes.com/api/docs/#get-api-v2-languages) with supplied parameters.
    pub async fn fetch_podcast_languages(&self, parameters: &Value) -> Result<Response> {
        self.get("languages", parameters).await
    }

    /// Calls [`GET /just_listen`](https://www.listennotes.com/api/docs/#get-api-v2-just_listen) with supplied parameters.
    pub async fn just_listen(&self, parameters: &Value) -> Result<Response> {
        self.get("just_listen", parameters).await
    }

    /// Calls [`GET /podcasts/{id}/recommendations`](https://www.listennotes.com/api/docs/#get-api-v2-podcasts-id-recommendations) with supplied parameters.
    pub async fn fetch_recommendations_for_podcast(&self, id: &str, parameters: &Value) -> Result<Response> {
        self.get(&format!("podcasts/{}/recommendations", id), parameters).await
    }

    /// Calls [`GET /episodes/{id}/recommendations`](https://www.listennotes.com/api/docs/#get-api-v2-episodes-id-recommendations) with supplied parameters.
    pub async fn fetch_recommendations_for_episode(&self, id: &str, parameters: &Value) -> Result<Response> {
        self.get(&format!("episodes/{}/recommendations", id), parameters).await
    }

    /// Calls [`GET /playlists/{id}`](https://www.listennotes.com/api/docs/#get-api-v2-playlists-id) with supplied parameters.
    pub async fn fetch_playlist_by_id(&self, id: &str, parameters: &Value) -> Result<Response> {
        self.get(&format!("playlists/{}", id), parameters).await
    }

    /// Calls [`GET /playlists`](https://www.listennotes.com/api/docs/#get-api-v2-playlists) with supplied parameters.
    pub async fn fetch_my_playlists(&self, parameters: &Value) -> Result<Response> {
        self.get("playlists", parameters).await
    }

    /// Calls [`POST /podcasts/submit`](https://www.listennotes.com/api/docs/#post-api-v2-podcasts-submit) with supplied parameters.
    pub async fn submit_podcast(&self, parameters: &Value) -> Result<Response> {
        self.post("podcasts/submit", parameters).await
    }

    /// Calls [`DELETE /podcasts/{id}`](https://www.listennotes.com/api/docs/#delete-api-v2-podcasts-id) with supplied parameters.
    pub async fn delete_podcast(&self, id: &str, parameters: &Value) -> Result<Response> {
        self.delete(&format!("podcasts/{}", id), parameters).await
    }

    async fn get(&self, endpoint: &str, parameters: &Value) -> Result<Response> {
        let request = self
            .client
            .get(format!("{}/{}", self.api.url(), endpoint))
            .query(parameters);

        Ok(self.request(request).await?)
    }

    async fn post(&self, endpoint: &str, parameters: &Value) -> Result<Response> {
        let request = self
            .client
            .post(format!("{}/{}", self.api.url(), endpoint))
            .header("Content-Type", "application/x-www-form-urlencoded")
            .body(Self::urlencoded_from_json(parameters));

        Ok(self.request(request).await?)
    }

    async fn delete(&self, endpoint: &str, parameters: &Value) -> Result<Response> {
        let request = self
            .client
            .delete(format!("{}/{}", self.api.url(), endpoint))
            .query(parameters);

        Ok(self.request(request).await?)
    }

    async fn request(&self, request: RequestBuilder) -> Result<Response> {
        let request = if let Api::Production(key) = self.api {
            request.header("X-ListenAPI-Key", key)
        } else {
            request
        }
        .header("User-Agent", self.user_agent)
        .build()?;

        let response = self
            .client
            .execute(request.try_clone().expect(
                "Error can remain unhandled because we're not using streams, which are the try_clone fail condition",
            ))
            .await;

        match &response {
            Ok(response) => match response.status() {
                StatusCode::NOT_FOUND => return Err(Error::NotFoundError),
                StatusCode::UNAUTHORIZED => return Err(Error::AuthenticationError),
                StatusCode::TOO_MANY_REQUESTS => return Err(Error::RateLimitError),
                StatusCode::BAD_REQUEST => return Err(Error::InvalidRequestError),
                StatusCode::INTERNAL_SERVER_ERROR => return Err(Error::ListenApiError),
                _ => {}
            },
            Err(err) => {
                if err.is_connect() || err.is_timeout() {
                    return Err(Error::ApiConnectionError);
                }
            }
        };

        Ok(Response {
            response: response?,
            request,
        })
    }

    fn urlencoded_from_json(json: &Value) -> String {
        if let Some(v) = json.as_object() {
            v.iter()
                .map(|(key, value)| {
                    format!(
                        "{}={}",
                        key,
                        match value {
                            Value::String(s) => s.to_owned(), // serde_json String(_) formatter includes the quotations marks, this doesn't
                            _ => format!("{}", value),
                        }
                    )
                })
                .collect::<Vec<String>>()
                .join("&")
        } else {
            String::new()
        }
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;
    #[test]
    fn urlencoded_from_json() {
        assert_eq!(
            super::Client::urlencoded_from_json(&json!({
                "a": 1,
                "b": true,
                "c": "test_string"
            })),
            "a=1&b=true&c=test_string"
        );
    }
}