Skip to content

Commit f63c418

Browse files
committed
Add crate docs
1 parent 0aec839 commit f63c418

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

src/client.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@ use super::{Api, Result};
22
use reqwest::RequestBuilder;
33
use serde_json::Value;
44

5+
/// Client for accessing Listen Notes API.
56
pub struct Client<'a> {
7+
/// HTTP client.
68
client: reqwest::Client,
9+
/// API context.
710
api: Api<'a>,
811
}
912

1013
impl Client<'_> {
14+
/// Creates new Listen API Client.
15+
///
16+
/// To access production API:
17+
/// ```
18+
/// let client = podcast_api::Client::new(reqwest::Client::new(), Some("YOUR-API-KEY"));
19+
/// ```
20+
/// To access mock API:
21+
/// ```
22+
/// let client = podcast_api::Client::new(reqwest::Client::new(), None);
23+
/// ```
1124
pub fn new<'a>(client: reqwest::Client, id: Option<&'a str>) -> Client<'a> {
1225
Client {
1326
client,
@@ -19,30 +32,41 @@ impl Client<'_> {
1932
}
2033
}
2134

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

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

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

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

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

60+
/// Calls [`GET /episodes/{id}`](https://www.listennotes.com/api/docs/#get-api-v2-episodes-id) with supplied parameters.
4261
pub async fn episode(&self, id: &str, parameters: &Value) -> Result<Value> {
4362
self.get(&format!("episodes/{}", id), parameters).await
63+
}
64+
65+
/// Calls [`POST /episodes`](https://www.listennotes.com/api/docs/#post-api-v2-episodes) with supplied parameters.
4466
pub async fn episodes(&self, parameters: &Value) -> Result<Value> {
4567
self.post("episodes", parameters).await
68+
}
69+
4670
async fn get(&self, endpoint: &str, parameters: &Value) -> Result<Value> {
4771
let request = self
4872
.client
@@ -74,15 +98,3 @@ impl Client<'_> {
7498
.await?)
7599
}
76100
}
77-
78-
trait AddField {
79-
fn with(&self, key: &str, value: &str) -> Self;
80-
}
81-
82-
impl AddField for Value {
83-
fn with(&self, key: &str, value: &str) -> Self {
84-
let mut p = self.clone();
85-
p[key] = json!(value);
86-
p
87-
}
88-
}

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
/// Error for API calls from [`Client`](super::Client).
12
#[derive(Debug)]
23
pub enum Error {
4+
/// Error from http client.
35
Reqwest(reqwest::Error),
6+
/// Error from JSON creation/processing.
47
Json(serde_json::Error),
58
}
69

src/lib.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
1+
//! Official library for accessing the [Listen API](https://www.listennotes.com/api) by [Listen Notes](https://www.listennotes.com).
2+
//!
3+
//! # Quick Start Example
4+
//!
5+
//! ```
6+
//! use serde_json::json;
7+
//!
8+
//! #[tokio::main]
9+
//! async fn main() {
10+
//! // Api Key (None => Test API, Some(key) => Production API)
11+
//! let api_key = None;
12+
//!
13+
//! // Create client
14+
//! let client = podcast_api::Client::new(reqwest::Client::new(), api_key);
15+
//!
16+
//! // Call API
17+
//! match client
18+
//! .typeahead(&json!({
19+
//! "q": "startup",
20+
//! "show_podcasts": 1
21+
//! }))
22+
//! .await
23+
//! {
24+
//! Ok(response) => {
25+
//! println!("Successfully called \"typeahead\" endpoint.");
26+
//! println!("Response Body:");
27+
//! println!("{:?}", response);
28+
//! }
29+
//! Err(err) => {
30+
//! println!("Error calling \"typeahead\" endpoint:");
31+
//! println!("{:?},", err);
32+
//! }
33+
//! };
34+
//! }
35+
//! ```
36+
#![deny(missing_docs)]
37+
138
mod api;
239
mod client;
340
mod error;
@@ -6,4 +43,5 @@ use api::Api;
643

744
pub use client::Client;
845
pub use error::Error;
46+
/// Result for API calls from [`Client`]
947
pub type Result<T> = std::result::Result<T, error::Error>;

0 commit comments

Comments
 (0)