Factor out Client site constructors with macro
This commit is contained in:
parent
cc8645da8f
commit
593a08e017
|
@ -17,6 +17,32 @@ pub struct Client {
|
||||||
redirect_uri: Option<String>,
|
redirect_uri: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! site_constructors {
|
||||||
|
(
|
||||||
|
$(
|
||||||
|
#[$attr:meta]
|
||||||
|
$ident:ident => ($auth_uri:expr, $token_uri:expr)
|
||||||
|
),*
|
||||||
|
) => {
|
||||||
|
$(
|
||||||
|
#[$attr]
|
||||||
|
pub fn $ident<S: Into<String>>(
|
||||||
|
client_id: S,
|
||||||
|
client_secret: S,
|
||||||
|
redirect_uri: Option<S>
|
||||||
|
) -> Self {
|
||||||
|
Client {
|
||||||
|
auth_uri: String::from($auth_uri),
|
||||||
|
token_uri: String::from($token_uri),
|
||||||
|
client_id: client_id.into(),
|
||||||
|
client_secret: client_secret.into(),
|
||||||
|
redirect_uri: redirect_uri.map(Into::into),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
/// Creates an OAuth 2.0 client.
|
/// Creates an OAuth 2.0 client.
|
||||||
pub fn new<S: Into<String>>(
|
pub fn new<S: Into<String>>(
|
||||||
|
@ -32,40 +58,22 @@ impl Client {
|
||||||
|
|
||||||
client_id: client_id.into(),
|
client_id: client_id.into(),
|
||||||
client_secret: client_secret.into(),
|
client_secret: client_secret.into(),
|
||||||
redirect_uri: redirect_uri.map(Into::<String>::into),
|
redirect_uri: redirect_uri.map(Into::into),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a Google OAuth 2.0 client.
|
site_constructors!{
|
||||||
pub fn google<S: Into<String>>(
|
#[doc = "Creates a Google OAuth 2.0 client."]
|
||||||
client_id: S,
|
google => (
|
||||||
client_secret: S,
|
"https://accounts.google.com/o/oauth2/auth",
|
||||||
redirect_uri: Option<S>
|
"https://accounts.google.com/o/oauth2/token"
|
||||||
) -> Self {
|
),
|
||||||
Client {
|
|
||||||
auth_uri: String::from("https://accounts.google.com/o/oauth2/auth"),
|
|
||||||
token_uri: String::from("https://accounts.google.com/o/oauth2/token"),
|
|
||||||
|
|
||||||
client_id: client_id.into(),
|
#[doc = "Creates a GitHub OAuth 2.0 client."]
|
||||||
client_secret: client_secret.into(),
|
github => (
|
||||||
redirect_uri: redirect_uri.map(Into::<String>::into),
|
"https://github.com/login/oauth/authorize",
|
||||||
}
|
"https://github.com/login/oauth/access_token"
|
||||||
}
|
)
|
||||||
|
|
||||||
/// Creates a GitHub OAuth 2.0 client.
|
|
||||||
pub fn github<S: Into<String>>(
|
|
||||||
client_id: S,
|
|
||||||
client_secret: S,
|
|
||||||
redirect_uri: Option<S>
|
|
||||||
) -> Self {
|
|
||||||
Client {
|
|
||||||
auth_uri: String::from("https://github.com/login/oauth/authorize"),
|
|
||||||
token_uri: String::from("https://github.com/login/oauth/access_token"),
|
|
||||||
|
|
||||||
client_id: client_id.into(),
|
|
||||||
client_secret: client_secret.into(),
|
|
||||||
redirect_uri: redirect_uri.map(Into::<String>::into),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Constructs an authorization request URI.
|
/// Constructs an authorization request URI.
|
||||||
|
|
Loading…
Reference in New Issue