Skip to content

Commit 810c52c

Browse files
committed
Add User-Agent header
1 parent 300d43f commit 810c52c

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/client.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ use reqwest::RequestBuilder;
33
use serde_json::Value;
44
use std::time::Duration;
55

6+
static DEFAULT_USER_AGENT: &str = "api-podcast-rust";
7+
68
/// Client for accessing Listen Notes API.
79
pub struct Client<'a> {
810
/// HTTP client.
911
client: reqwest::Client,
1012
/// API context.
1113
api: Api<'a>,
14+
/// User Agent Header for API calls.
15+
user_agent: &'a str,
1216
}
1317

1418
impl Client<'_> {
@@ -35,18 +39,28 @@ impl Client<'_> {
3539
} else {
3640
Api::Mock
3741
},
42+
user_agent: DEFAULT_USER_AGENT,
3843
}
3944
}
4045

4146
/// Creates new Listen API Client with user provided HTTP Client.
42-
pub fn new_custom(client: reqwest::Client, id: Option<&str>) -> Client {
47+
pub fn new_custom<'a>(
48+
client: reqwest::Client,
49+
id: Option<&'a str>,
50+
user_agent: Option<&'a str>,
51+
) -> Client<'a> {
4352
Client {
4453
client,
4554
api: if let Some(id) = id {
4655
Api::Production(id)
4756
} else {
4857
Api::Mock
4958
},
59+
user_agent: if let Some(user_agent) = user_agent {
60+
user_agent
61+
} else {
62+
DEFAULT_USER_AGENT
63+
},
5064
}
5165
}
5266

@@ -110,6 +124,7 @@ impl Client<'_> {
110124
} else {
111125
request
112126
}
127+
.header("User-Agent", self.user_agent)
113128
.send()
114129
.await?
115130
.json()

0 commit comments

Comments
 (0)