summaryrefslogtreecommitdiff
path: root/src/client.rs
diff options
context:
space:
mode:
authorDavid Blajda <blajda@hotmail.com>2018-12-27 22:03:23 +0000
committerDavid Blajda <blajda@hotmail.com>2018-12-27 22:03:23 +0000
commit678e3d3f28cb8594204dc5e2b7597ae66a4582c7 (patch)
tree55b563cfdc09dab8f18c1f4d688ebf0c2187985c /src/client.rs
parentcb1b144e48ee357a76f551433d4886f092d259c8 (diff)
Use Id types for endpoints and implement kraken headers
Diffstat (limited to 'src/client.rs')
-rw-r--r--src/client.rs49
1 files changed, 42 insertions, 7 deletions
diff --git a/src/client.rs b/src/client.rs
index fd3af32..8f93e1e 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -21,6 +21,8 @@ pub enum RatelimitKey {
type RatelimitMap = HashMap<RatelimitKey, Ratelimit>;
const API_DOMAIN: &'static str = "api.twitch.tv";
+const AUTH_DOMAIN: &'static str = "id.twitch.tv";
+const KRAKEN_ACCEPT: &'static str = "application/vnd.twitchtv.v5+json";
pub trait PaginationTrait {
fn cursor<'a>(&'a self) -> Option<&'a str>;
@@ -68,6 +70,7 @@ pub struct UnauthClient {
id: String,
reqwest: ReqwestClient,
domain: String,
+ auth_domain: String,
ratelimits: RatelimitMap,
version: Version,
}
@@ -83,6 +86,7 @@ pub trait ClientTrait {
fn id<'a>(&'a self) -> &'a str;
fn domain<'a>(&'a self) -> &'a str;
+ fn auth_domain<'a>(&'a self) -> &'a str;
fn ratelimit<'a>(&'a self, key: RatelimitKey) -> Option<&'a Ratelimit>;
fn authenticated(&self) -> bool;
@@ -98,6 +102,10 @@ impl ClientTrait for UnauthClient {
&self.domain
}
+ fn auth_domain<'a>(&'a self) -> &'a str {
+ &self.auth_domain
+ }
+
fn ratelimit<'a>(&'a self, key: RatelimitKey) -> Option<&'a Ratelimit> {
self.ratelimits.get(&key)
}
@@ -129,6 +137,14 @@ impl ClientTrait for Client {
}
}
+ fn auth_domain<'a>(&'a self) -> &'a str {
+ use self::ClientType::*;
+ match self.inner.as_ref() {
+ Unauth(inner) => inner.auth_domain(),
+ Auth(inner) => inner.auth_domain(),
+ }
+ }
+
fn ratelimit<'a>(&'a self, key: RatelimitKey) -> Option<&'a Ratelimit> {
use self::ClientType::*;
match self.inner.as_ref() {
@@ -170,6 +186,13 @@ impl ClientTrait for AuthClient {
}
}
+ fn auth_domain<'a>(&'a self) -> &'a str {
+ match self.previous.inner.as_ref() {
+ ClientType::Auth(auth) => auth.auth_domain(),
+ ClientType::Unauth(unauth) => unauth.auth_domain(),
+ }
+ }
+
fn ratelimit<'a>(&'a self, key: RatelimitKey) -> Option<&'a Ratelimit> {
match self.previous.inner.as_ref() {
ClientType::Auth(auth) => auth.ratelimit(key),
@@ -221,6 +244,7 @@ impl Client {
id: id.to_owned(),
reqwest: reqwest,
domain: API_DOMAIN.to_owned(),
+ auth_domain: AUTH_DOMAIN.to_owned(),
ratelimits: Self::default_ratelimits(),
version: version,
}))
@@ -268,15 +292,15 @@ impl Client {
fn apply_standard_headers(&self, request: RequestBuilder)
-> RequestBuilder
{
+ let token = match self.inner.as_ref() {
+ ClientType::Auth(inner) => {
+ let auth = inner.auth_state.lock().expect("Authlock is poisoned");
+ auth.token.as_ref().map(|s| s.to_owned())
+ }
+ ClientType::Unauth(_) => None,
+ };
match self.version() {
Version::Helix => {
- let token = match self.inner.as_ref() {
- ClientType::Auth(inner) => {
- let auth = inner.auth_state.lock().expect("Authlock is poisoned");
- auth.token.as_ref().map(|s| s.to_owned())
- }
- ClientType::Unauth(_) => None,
- };
let client_header = header::HeaderValue::from_str(self.id()).unwrap();
@@ -290,6 +314,17 @@ impl Client {
request.header("Client-ID", client_header)
},
Version::Kraken => {
+ let client_header = header::HeaderValue::from_str(self.id()).unwrap();
+ let accept_header = header::HeaderValue::from_str(KRAKEN_ACCEPT).unwrap();
+
+ let request = request.header("Client-ID", client_header);
+ let request = request.header("Accept", accept_header);
+ let request = if let Some(token) = token {
+ let value = "OAuth ".to_owned() + &token;
+ let token_header = header::HeaderValue::from_str(&value).unwrap();
+ request.header("Authorization", token_header)
+ } else {request};
+
request
}
}