Take String in Client::new

This commit is contained in:
Curtis McEnroe 2016-01-28 21:54:14 -05:00
parent ad23733ce4
commit c6aec754c3
7 changed files with 46 additions and 50 deletions

View File

@ -7,9 +7,9 @@ use inth_oauth2::provider::GitHub;
fn main() { fn main() {
let client = Client::<GitHub>::new( let client = Client::<GitHub>::new(
"01774654cd9a6051e478", String::from("01774654cd9a6051e478"),
"9f14d16d95d605e715ec1a9aecec220d2565fd5c", String::from("9f14d16d95d605e715ec1a9aecec220d2565fd5c"),
Some("https://cmcenroe.me/oauth2-paste/") Some(String::from("https://cmcenroe.me/oauth2-paste/"))
); );
let auth_uri = client.auth_uri(Some("user"), None).unwrap(); let auth_uri = client.auth_uri(Some("user"), None).unwrap();

View File

@ -7,9 +7,9 @@ use inth_oauth2::provider::Google;
fn main() { fn main() {
let client = Client::<Google>::new( let client = Client::<Google>::new(
"143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com", String::from("143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com"),
"3kZ5WomzHFlN2f_XbhkyPd3o", String::from("3kZ5WomzHFlN2f_XbhkyPd3o"),
Some("urn:ietf:wg:oauth:2.0:oob") Some(String::from("urn:ietf:wg:oauth:2.0:oob"))
); );
let auth_uri = client.auth_uri(Some("https://www.googleapis.com/auth/userinfo.email"), None) let auth_uri = client.auth_uri(Some("https://www.googleapis.com/auth/userinfo.email"), None)

View File

@ -7,9 +7,9 @@ use inth_oauth2::provider::Imgur;
fn main() { fn main() {
let client = Client::<Imgur>::new( let client = Client::<Imgur>::new(
"505c8ca804230e0", String::from("505c8ca804230e0"),
"c898d8cf28404102752b2119a3a1c6aab49899c8", String::from("c898d8cf28404102752b2119a3a1c6aab49899c8"),
Some("https://cmcenroe.me/oauth2-paste/") Some(String::from("https://cmcenroe.me/oauth2-paste/"))
); );
let auth_uri = client.auth_uri(None, None).unwrap(); let auth_uri = client.auth_uri(None, None).unwrap();

View File

@ -41,20 +41,16 @@ impl<P: Provider> Client<P> {
/// use inth_oauth2::provider::Google; /// use inth_oauth2::provider::Google;
/// ///
/// let client = Client::<Google>::new( /// let client = Client::<Google>::new(
/// "CLIENT_ID", /// String::from("CLIENT_ID"),
/// "CLIENT_SECRET", /// String::from("CLIENT_SECRET"),
/// Some("urn:ietf:wg:oauth:2.0:oob") /// Some(String::from("urn:ietf:wg:oauth:2.0:oob"))
/// ); /// );
/// ``` /// ```
pub fn new<S>( pub fn new(client_id: String, client_secret: String, redirect_uri: Option<String>) -> Self {
client_id: S,
client_secret: S,
redirect_uri: Option<S>
) -> Self where S: Into<String> {
Client { Client {
client_id: client_id.into(), client_id: client_id,
client_secret: client_secret.into(), client_secret: client_secret,
redirect_uri: redirect_uri.map(Into::into), redirect_uri: redirect_uri,
provider: PhantomData, provider: PhantomData,
} }
} }
@ -70,9 +66,9 @@ impl<P: Provider> Client<P> {
/// use inth_oauth2::provider::Google; /// use inth_oauth2::provider::Google;
/// ///
/// let client = Client::<Google>::new( /// let client = Client::<Google>::new(
/// "CLIENT_ID", /// String::from("CLIENT_ID"),
/// "CLIENT_SECRET", /// String::from("CLIENT_SECRET"),
/// Some("urn:ietf:wg:oauth:2.0:oob") /// Some(String::from("urn:ietf:wg:oauth:2.0:oob"))
/// ); /// );
/// ///
/// let auth_uri = client.auth_uri( /// let auth_uri = client.auth_uri(
@ -210,7 +206,7 @@ mod tests {
#[test] #[test]
fn auth_uri() { fn auth_uri() {
let client = Client::<Test>::new("foo", "bar", None); let client = Client::<Test>::new(String::from("foo"), String::from("bar"), None);
assert_eq!( assert_eq!(
"http://example.com/oauth2/auth?response_type=code&client_id=foo", "http://example.com/oauth2/auth?response_type=code&client_id=foo",
client.auth_uri(None, None).unwrap() client.auth_uri(None, None).unwrap()
@ -220,9 +216,9 @@ mod tests {
#[test] #[test]
fn auth_uri_with_redirect_uri() { fn auth_uri_with_redirect_uri() {
let client = Client::<Test>::new( let client = Client::<Test>::new(
"foo", String::from("foo"),
"bar", String::from("bar"),
Some("http://example.com/oauth2/callback") Some(String::from("http://example.com/oauth2/callback"))
); );
assert_eq!( assert_eq!(
"http://example.com/oauth2/auth?response_type=code&client_id=foo&redirect_uri=http%3A%2F%2Fexample.com%2Foauth2%2Fcallback", "http://example.com/oauth2/auth?response_type=code&client_id=foo&redirect_uri=http%3A%2F%2Fexample.com%2Foauth2%2Fcallback",
@ -232,7 +228,7 @@ mod tests {
#[test] #[test]
fn auth_uri_with_scope() { fn auth_uri_with_scope() {
let client = Client::<Test>::new("foo", "bar", None); let client = Client::<Test>::new(String::from("foo"), String::from("bar"), None);
assert_eq!( assert_eq!(
"http://example.com/oauth2/auth?response_type=code&client_id=foo&scope=baz", "http://example.com/oauth2/auth?response_type=code&client_id=foo&scope=baz",
client.auth_uri(Some("baz"), None).unwrap() client.auth_uri(Some("baz"), None).unwrap()
@ -241,7 +237,7 @@ mod tests {
#[test] #[test]
fn auth_uri_with_state() { fn auth_uri_with_state() {
let client = Client::<Test>::new("foo", "bar", None); let client = Client::<Test>::new(String::from("foo"), String::from("bar"), None);
assert_eq!( assert_eq!(
"http://example.com/oauth2/auth?response_type=code&client_id=foo&state=baz", "http://example.com/oauth2/auth?response_type=code&client_id=foo&state=baz",
client.auth_uri(None, Some("baz")).unwrap() client.auth_uri(None, Some("baz")).unwrap()

View File

@ -33,9 +33,9 @@
//! use inth_oauth2::provider::Google; //! use inth_oauth2::provider::Google;
//! //!
//! let client = Client::<Google>::new( //! let client = Client::<Google>::new(
//! "client_id", //! String::from("client_id"),
//! "client_secret", //! String::from("client_secret"),
//! Some("redirect_uri") //! Some(String::from("redirect_uri"))
//! ); //! );
//! ``` //! ```
//! //!
@ -44,7 +44,7 @@
//! ``` //! ```
//! # use inth_oauth2::Client; //! # use inth_oauth2::Client;
//! # use inth_oauth2::provider::Google; //! # use inth_oauth2::provider::Google;
//! # let client = Client::<Google>::new("", "", None); //! # let client = Client::<Google>::new(String::new(), String::new(), None);
//! let auth_uri = client.auth_uri(Some("scope"), Some("state")).unwrap(); //! let auth_uri = client.auth_uri(Some("scope"), Some("state")).unwrap();
//! println!("Authorize the application by clicking on the link: {}", auth_uri); //! println!("Authorize the application by clicking on the link: {}", auth_uri);
//! ``` //! ```
@ -55,7 +55,7 @@
//! use std::io; //! use std::io;
//! use inth_oauth2::{Client, Token}; //! use inth_oauth2::{Client, Token};
//! # use inth_oauth2::provider::Google; //! # use inth_oauth2::provider::Google;
//! # let client = Client::<Google>::new("", "", None); //! # let client = Client::<Google>::new(String::new(), String::new(), None);
//! //!
//! let mut code = String::new(); //! let mut code = String::new();
//! io::stdin().read_line(&mut code).unwrap(); //! io::stdin().read_line(&mut code).unwrap();
@ -70,7 +70,7 @@
//! ```no_run //! ```no_run
//! # use inth_oauth2::Client; //! # use inth_oauth2::Client;
//! # use inth_oauth2::provider::Google; //! # use inth_oauth2::provider::Google;
//! # let client = Client::<Google>::new("", "", None); //! # let client = Client::<Google>::new(String::new(), String::new(), None);
//! # let http_client = Default::default(); //! # let http_client = Default::default();
//! # let token = client.request_token(&http_client, "").unwrap(); //! # let token = client.request_token(&http_client, "").unwrap();
//! let token = client.refresh_token(&http_client, token, None).unwrap(); //! let token = client.refresh_token(&http_client, token, None).unwrap();
@ -81,7 +81,7 @@
//! ```no_run //! ```no_run
//! # use inth_oauth2::Client; //! # use inth_oauth2::Client;
//! # use inth_oauth2::provider::Google; //! # use inth_oauth2::provider::Google;
//! # let client = Client::<Google>::new("", "", None); //! # let client = Client::<Google>::new(String::new(), String::new(), None);
//! # let http_client = Default::default(); //! # let http_client = Default::default();
//! # let mut token = client.request_token(&http_client, "").unwrap(); //! # let mut token = client.request_token(&http_client, "").unwrap();
//! // Refresh token only if it has expired. //! // Refresh token only if it has expired.
@ -101,7 +101,7 @@
//! //!
//! # fn main() { //! # fn main() {
//! let client = hyper::Client::new(); //! let client = hyper::Client::new();
//! # let oauth_client = Client::<Google>::new("", "", None); //! # let oauth_client = Client::<Google>::new(String::new(), String::new(), None);
//! # let token = oauth_client.request_token(&client, "").unwrap(); //! # let token = oauth_client.request_token(&client, "").unwrap();
//! let request = client.get("https://example.com/resource") //! let request = client.get("https://example.com/resource")
//! .header(Into::<Authorization<_>>::into(&token)); //! .header(Into::<Authorization<_>>::into(&token));
@ -121,7 +121,7 @@
//! use rustc_serialize::json; //! use rustc_serialize::json;
//! # fn main() { //! # fn main() {
//! # let http_client = Default::default(); //! # let http_client = Default::default();
//! # let client = Client::<Google>::new("", "", None); //! # let client = Client::<Google>::new(String::new(), String::new(), None);
//! # let token = client.request_token(&http_client, "").unwrap(); //! # let token = client.request_token(&http_client, "").unwrap();
//! let json = json::encode(&token).unwrap(); //! let json = json::encode(&token).unwrap();
//! # } //! # }
@ -134,7 +134,7 @@
//! # use inth_oauth2::provider::Google; //! # use inth_oauth2::provider::Google;
//! # fn main() { //! # fn main() {
//! # let http_client = Default::default(); //! # let http_client = Default::default();
//! # let client = Client::<Google>::new("", "", None); //! # let client = Client::<Google>::new(String::new(), String::new(), None);
//! # let token = client.request_token(&http_client, "").unwrap(); //! # let token = client.request_token(&http_client, "").unwrap();
//! let json = serde_json::to_string(&token).unwrap(); //! let json = serde_json::to_string(&token).unwrap();
//! # } //! # }

View File

@ -13,9 +13,9 @@ fn assert_get_uri_ok(uri: &str) {
#[test] #[test]
fn google_auth_uri_ok() { fn google_auth_uri_ok() {
let client = Client::<Google>::new( let client = Client::<Google>::new(
"143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com", String::from("143225766783-ip2d9qv6sdr37276t77luk6f7bhd6bj5.apps.googleusercontent.com"),
"", String::new(),
Some("urn:ietf:wg:oauth:2.0:oob") Some(String::from("urn:ietf:wg:oauth:2.0:oob"))
); );
let auth_uri = client.auth_uri( let auth_uri = client.auth_uri(
Some("https://www.googleapis.com/auth/userinfo.email"), Some("https://www.googleapis.com/auth/userinfo.email"),
@ -27,9 +27,9 @@ fn google_auth_uri_ok() {
#[test] #[test]
fn github_auth_uri_ok() { fn github_auth_uri_ok() {
let client = Client::<GitHub>::new( let client = Client::<GitHub>::new(
"01774654cd9a6051e478", String::from("01774654cd9a6051e478"),
"", String::new(),
Some("https://cmcenroe.me/oauth2-paste/") Some(String::from("https://cmcenroe.me/oauth2-paste/"))
); );
let auth_uri = client.auth_uri(Some("user"), Some("state")).unwrap(); let auth_uri = client.auth_uri(Some("user"), Some("state")).unwrap();
assert_get_uri_ok(&auth_uri); assert_get_uri_ok(&auth_uri);
@ -38,9 +38,9 @@ fn github_auth_uri_ok() {
#[test] #[test]
fn imgur_auth_uri_ok() { fn imgur_auth_uri_ok() {
let client = Client::<Imgur>::new( let client = Client::<Imgur>::new(
"505c8ca804230e0", String::from("505c8ca804230e0"),
"", String::new(),
Some("https://cmcenroe.me/oauth2-paste/") Some(String::from("https://cmcenroe.me/oauth2-paste/"))
); );
let auth_uri = client.auth_uri(None, Some("state")).unwrap(); let auth_uri = client.auth_uri(None, Some("state")).unwrap();
assert_get_uri_ok(&auth_uri); assert_get_uri_ok(&auth_uri);

View File

@ -59,8 +59,8 @@ mod connector {
macro_rules! mock_client { macro_rules! mock_client {
($p:ty, $c:ty) => { ($p:ty, $c:ty) => {
(Client::<$p>::new( (Client::<$p>::new(
"client_id", String::from("client_id"),
"client_secret", String::from("client_secret"),
None None
), ),
hyper::Client::with_connector(<$c>::default())) hyper::Client::with_connector(<$c>::default()))