diff options
author | David Blajda <blajda@hotmail.com> | 2018-12-15 06:21:52 +0000 |
---|---|---|
committer | David Blajda <blajda@hotmail.com> | 2018-12-15 06:21:52 +0000 |
commit | a4c842eae14bef20d3d04ee4313251344edf431f (patch) | |
tree | 1685f11fd6bbe9f85cb51d479770b04692250c0c /src/kraken | |
parent | 8615cc2f030240ba2982dba893fe63f11a0c8a88 (diff) |
Deserialize Urls and Dates. Also implement custom Id types
Diffstat (limited to 'src/kraken')
-rw-r--r-- | src/kraken/endpoints.rs | 6 | ||||
-rw-r--r-- | src/kraken/mod.rs | 40 | ||||
-rw-r--r-- | src/kraken/models.rs | 32 |
3 files changed, 55 insertions, 23 deletions
diff --git a/src/kraken/endpoints.rs b/src/kraken/endpoints.rs index 2dbc8d1..7ae273c 100644 --- a/src/kraken/endpoints.rs +++ b/src/kraken/endpoints.rs @@ -9,10 +9,10 @@ impl Client { -> impl Future<Item=Clip, Error=reqwest::Error> { let url = String::from("https://") + API_DOMAIN + "/kraken/clips/" + id; - let client = self.create_reqwest_client(); + let request = self.inner.client.get(&url); + let request = self.apply_standard_headers(request); - client - .get(&url) + request .send() .map(|mut res| res.json::<Clip>()) .and_then(|json| json) diff --git a/src/kraken/mod.rs b/src/kraken/mod.rs index 2015781..59d00c0 100644 --- a/src/kraken/mod.rs +++ b/src/kraken/mod.rs @@ -1,33 +1,53 @@ use reqwest::header; -use reqwest::r#async::{Chunk, Decoder, Request, Response}; +use std::sync::Arc; +use reqwest::r#async::RequestBuilder; use reqwest::r#async::Client as ReqwestClient; +pub use super::types; mod endpoints; mod models; + const ACCEPT: &str = "application/vnd.twitchtv.v5+json"; pub const API_DOMAIN: &str = "api.twitch.tv"; +#[derive(Clone)] pub struct Client { + inner: Arc<ClientRef>, +} + +struct ClientRef { id: String, + client: ReqwestClient } impl Client { pub fn new(id: &str) -> Client { Client { - id: id.to_owned(), + inner: Arc::new(ClientRef { + id: id.to_owned(), + client: ReqwestClient::new(), + }) + } + } + + pub fn new_with_client(id: &str, client: ReqwestClient) -> Client { + Client { + inner: Arc::new(ClientRef { + id: id.to_owned(), + client: client, + }) } } - fn create_reqwest_client(&self) -> ReqwestClient { - let mut headers = header::HeaderMap::new(); - let auth_key = &self.id; - let client_header = header::HeaderValue::from_str(auth_key).unwrap(); + fn apply_standard_headers(&self, request: RequestBuilder) + -> RequestBuilder + { + let client_header = header::HeaderValue::from_str(&self.inner.id).unwrap(); let accept_header = header::HeaderValue::from_str(ACCEPT).unwrap(); - headers.insert("Client-ID", client_header); - headers.insert("Accept", accept_header); - let client = ReqwestClient::builder().default_headers(headers).build().unwrap(); - client + request + .header("Accept", accept_header) + .header("Client-ID", client_header) } } diff --git a/src/kraken/models.rs b/src/kraken/models.rs index 13c524c..4863641 100644 --- a/src/kraken/models.rs +++ b/src/kraken/models.rs @@ -1,12 +1,19 @@ extern crate serde_json; extern crate chrono; +extern crate url; + +use url::Url; +use chrono::{DateTime, Utc}; +use super::types::{UserId, VideoId}; #[derive(Debug, Deserialize)] pub struct Clip { pub slug: String, pub tracking_id: String, - pub url: String, - pub embed_url: String, + #[serde(with = "url_serde")] + pub url: Url, + #[serde(with = "url_serde")] + pub embed_url: Url, pub embed_html: String, pub broadcaster: UserData, pub curator: UserData, @@ -16,29 +23,34 @@ pub struct Clip { pub title: String, pub views: i32, pub duration: f32, - pub created_at: String, + pub created_at: DateTime<Utc>, pub thumbnails: Thumbnails, } #[derive(Debug, Deserialize)] pub struct Thumbnails { - pub medium: String, - pub small: String, - pub tiny: String, + #[serde(with = "url_serde")] + pub medium: Url, + #[serde(with = "url_serde")] + pub small: Url, + #[serde(with = "url_serde")] + pub tiny: Url, } #[derive(Debug, Deserialize)] pub struct UserData { - pub id: String, + pub id: UserId, pub name: String, pub display_name: String, - pub channel_url: String, + #[serde(with = "url_serde")] + pub channel_url: Url, pub logo: String, } #[derive(Debug, Deserialize)] pub struct Vod { - pub id: String, - pub url: String, + pub id: VideoId, + #[serde(with = "url_serde")] + pub url: Url, } |