|
1 |
| -use super::{Api, Result}; |
| 1 | +use super::{Api, Error, Result}; |
| 2 | +use http::StatusCode; |
2 | 3 | use reqwest::RequestBuilder;
|
3 | 4 | use serde_json::Value;
|
4 | 5 | use std::time::Duration;
|
@@ -60,11 +61,7 @@ impl Client<'_> {
|
60 | 61 | }
|
61 | 62 |
|
62 | 63 | /// Creates new Listen API Client with user provided HTTP Client.
|
63 |
| - pub fn new_custom<'a>( |
64 |
| - client: reqwest::Client, |
65 |
| - id: Option<&'a str>, |
66 |
| - user_agent: Option<&'a str>, |
67 |
| - ) -> Client<'a> { |
| 64 | + pub fn new_custom<'a>(client: reqwest::Client, id: Option<&'a str>, user_agent: Option<&'a str>) -> Client<'a> { |
68 | 65 | Client {
|
69 | 66 | client,
|
70 | 67 | api: if let Some(id) = id {
|
@@ -115,6 +112,36 @@ impl Client<'_> {
|
115 | 112 | self.post("episodes", parameters).await
|
116 | 113 | }
|
117 | 114 |
|
| 115 | + /// Calls [`GET /curated_podcasts/{id}`](https://www.listennotes.com/api/docs/#post-api-v2-curated_podcasts-id) with supplied parameters. |
| 116 | + pub async fn fetch_curated_podcasts_list_by_id(&self, id: &str, parameters: &Value) -> Result<Response> { |
| 117 | + self.get(&format!("curated_podcasts/{}", id), parameters).await |
| 118 | + } |
| 119 | + |
| 120 | + /// Calls [`GET /curated_podcasts`](https://www.listennotes.com/api/docs/#post-api-v2-curated_podcasts) with supplied parameters. |
| 121 | + pub async fn fetch_curated_podcasts_lists(&self, parameters: &Value) -> Result<Response> { |
| 122 | + self.get("curated_podcasts", parameters).await |
| 123 | + } |
| 124 | + |
| 125 | + /// Calls [`GET /genres`](https://www.listennotes.com/api/docs/#post-api-v2-genres) with supplied parameters. |
| 126 | + pub async fn fetch_podcast_genres(&self, parameters: &Value) -> Result<Response> { |
| 127 | + self.get("genres", parameters).await |
| 128 | + } |
| 129 | + |
| 130 | + /// Calls [`GET /regions`](https://www.listennotes.com/api/docs/#post-api-v2-regions) with supplied parameters. |
| 131 | + pub async fn fetch_podcast_regions(&self, parameters: &Value) -> Result<Response> { |
| 132 | + self.get("regions", parameters).await |
| 133 | + } |
| 134 | + |
| 135 | + /// Calls [`GET /languages`](https://www.listennotes.com/api/docs/#post-api-v2-languages) with supplied parameters. |
| 136 | + pub async fn fetch_podcast_languages(&self, parameters: &Value) -> Result<Response> { |
| 137 | + self.get("languages", parameters).await |
| 138 | + } |
| 139 | + |
| 140 | + /// Calls [`GET /just_listen`](https://www.listennotes.com/api/docs/#post-api-v2-just_listen) with supplied parameters. |
| 141 | + pub async fn just_listen(&self, parameters: &Value) -> Result<Response> { |
| 142 | + self.get("just_listen", parameters).await |
| 143 | + } |
| 144 | + |
118 | 145 | async fn get(&self, endpoint: &str, parameters: &Value) -> Result<Response> {
|
119 | 146 | let request = self
|
120 | 147 | .client
|
@@ -143,8 +170,31 @@ impl Client<'_> {
|
143 | 170 | .header("User-Agent", self.user_agent)
|
144 | 171 | .build()?;
|
145 | 172 |
|
| 173 | + let response = self |
| 174 | + .client |
| 175 | + .execute(request.try_clone().expect( |
| 176 | + "Error can remain unhandled because we're not using streams, which are the try_clone fail condition", |
| 177 | + )) |
| 178 | + .await; |
| 179 | + |
| 180 | + match &response { |
| 181 | + Ok(response) => match response.status() { |
| 182 | + StatusCode::NOT_FOUND => return Err(Error::NotFoundError), |
| 183 | + StatusCode::UNAUTHORIZED => return Err(Error::AuthenticationError), |
| 184 | + StatusCode::TOO_MANY_REQUESTS => return Err(Error::RateLimitError), |
| 185 | + StatusCode::BAD_REQUEST => return Err(Error::InvalidRequestError), |
| 186 | + StatusCode::INTERNAL_SERVER_ERROR => return Err(Error::ListenApiError), |
| 187 | + _ => {} |
| 188 | + }, |
| 189 | + Err(err) => { |
| 190 | + if err.is_connect() || err.is_timeout() { |
| 191 | + return Err(Error::ApiConnectionError); |
| 192 | + } |
| 193 | + } |
| 194 | + }; |
| 195 | + |
146 | 196 | Ok(Response {
|
147 |
| - 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?, |
| 197 | + response: response?, |
148 | 198 | request,
|
149 | 199 | })
|
150 | 200 | }
|
|
0 commit comments