Skip to content

Commit 300d43f

Browse files
committed
Split out default/custom HTTP clients
1 parent 98bc939 commit 300d43f

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/client.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::{Api, Result};
22
use reqwest::RequestBuilder;
33
use serde_json::Value;
4+
use std::time::Duration;
45

56
/// Client for accessing Listen Notes API.
67
pub struct Client<'a> {
@@ -13,15 +14,32 @@ pub struct Client<'a> {
1314
impl Client<'_> {
1415
/// Creates new Listen API Client.
1516
///
17+
/// Uses default HTTP client with 30 second timeouts.
18+
///
1619
/// To access production API:
1720
/// ```
18-
/// let client = podcast_api::Client::new(reqwest::Client::new(), Some("YOUR-API-KEY"));
21+
/// let client = podcast_api::Client::new(Some("YOUR-API-KEY"));
1922
/// ```
2023
/// To access mock API:
2124
/// ```
22-
/// let client = podcast_api::Client::new(reqwest::Client::new(), None);
25+
/// let client = podcast_api::Client::new(None);
2326
/// ```
24-
pub fn new(client: reqwest::Client, id: Option<&str>) -> Client {
27+
pub fn new(id: Option<&str>) -> Client {
28+
Client {
29+
client: reqwest::ClientBuilder::new()
30+
.timeout(Duration::from_secs(30))
31+
.build()
32+
.expect("Client::new()"),
33+
api: if let Some(id) = id {
34+
Api::Production(id)
35+
} else {
36+
Api::Mock
37+
},
38+
}
39+
}
40+
41+
/// Creates new Listen API Client with user provided HTTP Client.
42+
pub fn new_custom(client: reqwest::Client, id: Option<&str>) -> Client {
2543
Client {
2644
client,
2745
api: if let Some(id) = id {

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! let api_key = None;
1212
//!
1313
//! // Create client
14-
//! let client = podcast_api::Client::new(reqwest::Client::new(), api_key);
14+
//! let client = podcast_api::Client::new(api_key);
1515
//!
1616
//! // Call API
1717
//! match client

tests/client_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod mock {
88
use serde_json::json;
99

1010
fn client<'a>() -> podcast_api::Client<'a> {
11-
podcast_api::Client::new(reqwest::Client::new(), None)
11+
podcast_api::Client::new(None)
1212
}
1313

1414
#[test]

0 commit comments

Comments
 (0)