Skip to content
This repository has been archived by the owner on Aug 14, 2019. It is now read-only.

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
Add native-tls + hyper-native-tls (see hyperium/hyper#1009) for now
  • Loading branch information
mmitteregger committed Apr 1, 2017
1 parent 3926ba9 commit 3a9cffd
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ keywords = ["twitch"]

[dependencies]
# Http client for Twitch REST API
hyper = "0.7"
hyper = "0.10"
hyper-native-tls = "0.2"
native-tls = "0.1"
# Escaping of query parameters
url = "0.5"
url = "1.4"
# JSON serialization and deserialization
serde = "0.8"
serde_json = "0.8"
serde_derive = "0.8"
serde = "0.9"
serde_json = "0.9"
serde_derive = "0.9"
21 changes: 21 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::io::Error as IoError;
use hyper::Url;
use hyper::client::response::Response;
use hyper::error::Error as HyperError;
use hyper_native_tls::ServerError as HyperNativeTlsServerError;
use native_tls::Error as NativeTlsError;
use serde_json::error::Error as JsonError;

/// Result type from methods that can have Twitch Client Errors.
Expand All @@ -16,6 +18,7 @@ use self::Error::{
Unauthorized,
Io,
Hyper,
Tls,
Deserialization,
};

Expand All @@ -37,6 +40,8 @@ pub enum Error {
Io(IoError),
/// An `hyper::error::Error` that occurred while trying to use the hyper library.
Hyper(HyperError),
/// An `hyper::error::Error` that occurred while trying to use the hyper library.
Tls(NativeTlsError),
/// An `serde_json::error::Error` that occurred while trying to deserialize a json response string.
Deserialization(JsonError),
}
Expand All @@ -54,6 +59,7 @@ impl StdError for Error {
Unauthorized(ref _url) => "Tried to access an secured resource prior to authentication",
Io(ref e) => e.description(),
Hyper(ref e) => e.description(),
Tls(ref e) => e.description(),
Deserialization(ref e) => e.description(),
}
}
Expand Down Expand Up @@ -83,6 +89,21 @@ impl From<HyperError> for Error {
}
}

impl From<HyperNativeTlsServerError> for Error {
fn from(err: HyperNativeTlsServerError) -> Error {
match err {
HyperNativeTlsServerError::Io(e) => Io(e),
HyperNativeTlsServerError::Tls(e) => Tls(e),
}
}
}

impl From<NativeTlsError> for Error {
fn from(err: NativeTlsError) -> Error {
Tls(err)
}
}

impl From<JsonError> for Error {
fn from(err: JsonError) -> Error {
Deserialization(err)
Expand Down
15 changes: 11 additions & 4 deletions src/http.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::io::Read;
use hyper;
use hyper::net::HttpsConnector;
use hyper::Url;
use hyper::header::{Headers, Accept, qitem};
use hyper::mime::{Mime, TopLevel, SubLevel};
use hyper::status::{StatusCode, StatusClass};
use hyper_native_tls::NativeTlsClient;

use error::{Result, Error};

Expand All @@ -24,11 +26,16 @@ pub struct TwitchHttpClient {

impl TwitchHttpClient {

pub fn new() -> TwitchHttpClient {
TwitchHttpClient {
pub fn new() -> Result<TwitchHttpClient> {
let ssl = try!(NativeTlsClient::new());
let connector = HttpsConnector::new(ssl);
let hyper_client = hyper::Client::with_connector(connector);

let twitch_http_client = TwitchHttpClient {
client_id: None,
hyper_client: hyper::Client::new()
}
hyper_client: hyper_client,
};
Ok(twitch_http_client)
}

pub fn set_client_id(&mut self, client_id: &str) {
Expand Down
15 changes: 10 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
//! ```
#[macro_use] extern crate hyper;
extern crate hyper_native_tls;
extern crate native_tls;
extern crate url;
extern crate serde;
#[macro_use] extern crate serde_derive;
Expand Down Expand Up @@ -72,10 +74,13 @@ impl TwitchClient {
///
/// It is highly recommended to specify a client id to avoid being rate limited by Twitch
/// with the `with_client_id` method.
pub fn new() -> TwitchClient {
TwitchClient {
http_client: TwitchHttpClient::new(),
}
pub fn new() -> Result<TwitchClient> {
let http_client = try!(TwitchHttpClient::new());

let twitch_client = TwitchClient {
http_client: http_client,
};
Ok(twitch_client)
}

/// Sets the Twitch client id.
Expand Down Expand Up @@ -337,7 +342,7 @@ mod tests {

fn create_test_twitch_client() -> TwitchClient {
let auth = read_auth();
let mut twitch_client = TwitchClient::new();
let mut twitch_client = TwitchClient::new().unwrap();

match auth {
Some(auth) => {
Expand Down
11 changes: 6 additions & 5 deletions src/param.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Parameters for `TwitchClient` methods.
use std::string::ToString;
use std::borrow::Cow;
use url::percent_encoding::utf8_percent_encode;
use url::percent_encoding::FORM_URLENCODED_ENCODE_SET;
use url::percent_encoding::QUERY_ENCODE_SET;

use http::IntoQueryString;

Expand Down Expand Up @@ -297,8 +298,8 @@ fn params_into_query_string(params: Vec<(&str, Option<String>)>) -> String {
query_string
}

fn encode(param_value: &str) -> String {
utf8_percent_encode(param_value, FORM_URLENCODED_ENCODE_SET).replace("%20", "+")
fn encode(param_value: &str) -> Cow<str> {
utf8_percent_encode(param_value, QUERY_ENCODE_SET).collect()
}


Expand Down Expand Up @@ -333,7 +334,7 @@ mod tests {
fn test_string_params_should_be_escaped_correctly() {
let params = StreamsParams::new()
.with_game("StarCraft II: Heart of the Swarm");
assert_eq!(params.into_query_string(), "?game=StarCraft+II%3A+Heart+of+the+Swarm");
assert_eq!(params.into_query_string(), "?game=StarCraft%20II:%20Heart%20of%20the%20Swarm");
}

#[test]
Expand All @@ -348,7 +349,7 @@ mod tests {
let params = StreamsParams::new()
.with_channel("StarCraft I")
.with_channel("StarCraft II");
assert_eq!(params.into_query_string(), "?channel=StarCraft+I%2CStarCraft+II");
assert_eq!(params.into_query_string(), "?channel=StarCraft%20I,StarCraft%20II");
}

#[test]
Expand Down

0 comments on commit 3a9cffd

Please sign in to comment.