Skip to content

Commit 644fc77

Browse files
authored
Merge pull request #5 from ListenNotes/cameron-develop
Cameron develop
2 parents 1808e1e + 550bdcd commit 644fc77

File tree

3 files changed

+174
-6
lines changed

3 files changed

+174
-6
lines changed

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ name = "podcast-api"
33
version = "0.1.0"
44
authors = ["Cameron Fyfe <[email protected]>"]
55
edition = "2018"
6+
description = "Rust bindings for the Listen Notes Podcast API"
7+
license = "MIT"
8+
keywords = ["listennotes", "podcast"]
9+
documentation = "https://docs.rs/podcast-api"
10+
repository = "https://github.com/ListenNotes/podcast-api-rust"
11+
readme = "README.md"
612

713
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
814

src/client.rs

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,36 +112,66 @@ impl Client<'_> {
112112
self.post("episodes", parameters).await
113113
}
114114

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

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

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

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

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

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

145+
/// Calls [`GET /podcasts/{id}/recommendations`](https://www.listennotes.com/api/docs/#get-api-v2-podcasts-id-recommendations) with supplied parameters.
146+
pub async fn fetch_recommendations_for_podcast(&self, id: &str, parameters: &Value) -> Result<Response> {
147+
self.get(&format!("podcasts/{}/recommendations", id), parameters).await
148+
}
149+
150+
/// Calls [`GET /episodes/{id}/recommendations`](https://www.listennotes.com/api/docs/#get-api-v2-episodes-id-recommendations) with supplied parameters.
151+
pub async fn fetch_recommendations_for_episode(&self, id: &str, parameters: &Value) -> Result<Response> {
152+
self.get(&format!("episodes/{}/recommendations", id), parameters).await
153+
}
154+
155+
/// Calls [`GET /playlists/{id}`](https://www.listennotes.com/api/docs/#get-api-v2-playlists-id) with supplied parameters.
156+
pub async fn fetch_playlist_by_id(&self, id: &str, parameters: &Value) -> Result<Response> {
157+
self.get(&format!("playlists/{}", id), parameters).await
158+
}
159+
160+
/// Calls [`GET /playlists`](https://www.listennotes.com/api/docs/#get-api-v2-playlists) with supplied parameters.
161+
pub async fn fetch_my_playlists(&self, parameters: &Value) -> Result<Response> {
162+
self.get("playlists", parameters).await
163+
}
164+
165+
/// Calls [`POST /podcasts/submit`](https://www.listennotes.com/api/docs/#post-api-v2-podcasts-submit) with supplied parameters.
166+
pub async fn submit_podcast(&self, parameters: &Value) -> Result<Response> {
167+
self.post("podcasts/submit", parameters).await
168+
}
169+
170+
/// Calls [`DELETE /podcasts/{id}`](https://www.listennotes.com/api/docs/#delete-api-v2-podcasts-id) with supplied parameters.
171+
pub async fn delete_podcast(&self, id: &str, parameters: &Value) -> Result<Response> {
172+
self.delete(&format!("podcasts/{}", id), parameters).await
173+
}
174+
145175
async fn get(&self, endpoint: &str, parameters: &Value) -> Result<Response> {
146176
let request = self
147177
.client
@@ -161,6 +191,15 @@ impl Client<'_> {
161191
Ok(self.request(request).await?)
162192
}
163193

194+
async fn delete(&self, endpoint: &str, parameters: &Value) -> Result<Response> {
195+
let request = self
196+
.client
197+
.delete(format!("{}/{}", self.api.url(), endpoint))
198+
.query(parameters);
199+
200+
Ok(self.request(request).await?)
201+
}
202+
164203
async fn request(&self, request: RequestBuilder) -> Result<Response> {
165204
let request = if let Api::Production(key) = self.api {
166205
request.header("X-ListenAPI-Key", key)

tests/client_tests.rs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,127 @@ mod mock {
290290
assert!(body["audio_length_sec"].as_i64().unwrap() > 0);
291291
});
292292
}
293+
294+
#[test]
295+
fn fetch_recommendations_for_podcast() {
296+
b!(async {
297+
let response = client()
298+
.fetch_recommendations_for_podcast("adfsddf", &json!({}))
299+
.await
300+
.unwrap();
301+
// Request
302+
assert_eq!(response.request.method(), http::Method::GET);
303+
assert_eq!(
304+
response.request.url().path(),
305+
"/api/v2/podcasts/adfsddf/recommendations"
306+
);
307+
let p = response.request.url().query_pairs();
308+
assert_eq!(p.count(), 0);
309+
// Response
310+
let body = response.json().await.unwrap();
311+
assert!(body.is_object());
312+
assert!(body["recommendations"].as_array().unwrap().len() > 0);
313+
});
314+
}
315+
316+
#[test]
317+
fn fetch_recommendations_for_episode() {
318+
b!(async {
319+
let response = client()
320+
.fetch_recommendations_for_episode("asdfasdf", &json!({}))
321+
.await
322+
.unwrap();
323+
// Request
324+
assert_eq!(response.request.method(), http::Method::GET);
325+
assert_eq!(
326+
response.request.url().path(),
327+
"/api/v2/episodes/asdfasdf/recommendations"
328+
);
329+
let p = response.request.url().query_pairs();
330+
assert_eq!(p.count(), 0);
331+
// Response
332+
let body = response.json().await.unwrap();
333+
assert!(body.is_object());
334+
assert!(body["recommendations"].as_array().unwrap().len() > 0);
335+
});
336+
}
337+
338+
#[test]
339+
fn fetch_playlist_by_id() {
340+
b!(async {
341+
let response = client().fetch_playlist_by_id("fdsafdsa", &json!({})).await.unwrap();
342+
// Request
343+
assert_eq!(response.request.method(), http::Method::GET);
344+
assert_eq!(response.request.url().path(), "/api/v2/playlists/fdsafdsa");
345+
let p = response.request.url().query_pairs();
346+
assert_eq!(p.count(), 0);
347+
// Response
348+
let body = response.json().await.unwrap();
349+
assert!(body.is_object());
350+
assert!(body["items"].as_array().unwrap().len() > 0);
351+
});
352+
}
353+
354+
#[test]
355+
fn fetch_my_playlists() {
356+
b!(async {
357+
let response = client()
358+
.fetch_my_playlists(&json!({
359+
"page": 2
360+
}))
361+
.await
362+
.unwrap();
363+
// Request
364+
assert_eq!(response.request.method(), http::Method::GET);
365+
assert_eq!(response.request.url().path(), "/api/v2/playlists");
366+
let mut p = response.request.url().query_pairs();
367+
assert_eq!(p.count(), 1);
368+
assert_eq!(p.next(), Some((Cow::Borrowed("page"), Cow::Borrowed("2"))));
369+
// Response
370+
let body = response.json().await.unwrap();
371+
assert!(body.is_object());
372+
assert!(body["playlists"].as_array().unwrap().len() > 0);
373+
});
374+
}
375+
376+
#[test]
377+
fn submit_podcast() {
378+
b!(async {
379+
let response = client()
380+
.submit_podcast(&json!({
381+
"rss": "http://myrss.com/rss"
382+
}))
383+
.await
384+
.unwrap();
385+
// Request
386+
assert_eq!(response.request.method(), http::Method::POST);
387+
assert_eq!(response.request.url().path(), "/api/v2/podcasts/submit");
388+
let mut p = form_urlencoded::parse(response.request.body().unwrap().as_bytes().unwrap());
389+
assert_eq!(p.count(), 1);
390+
assert_eq!(
391+
p.next(),
392+
Some((Cow::Borrowed("rss"), Cow::Borrowed("http://myrss.com/rss")))
393+
);
394+
// Response
395+
let body = response.json().await.unwrap();
396+
assert!(body.is_object());
397+
assert!(body["status"].as_str().unwrap().len() > 0);
398+
});
399+
}
400+
401+
#[test]
402+
fn delete_podcast() {
403+
b!(async {
404+
let response = client().delete_podcast("asdfasdfdf", &json!({})).await.unwrap();
405+
// Request
406+
assert_eq!(response.request.method(), http::Method::DELETE);
407+
assert_eq!(response.request.url().path(), "/api/v2/podcasts/asdfasdfdf");
408+
let p = response.request.url().query_pairs();
409+
assert_eq!(p.count(), 0);
410+
// Response
411+
let body = response.json().await.unwrap();
412+
assert!(body.is_object());
413+
assert!(body["status"].as_str().unwrap().len() > 0);
414+
});
415+
}
293416
}

0 commit comments

Comments
 (0)