use super::{OAuth, OAuthClient, OAuthToken};
use crate::framework::endpoint::{HerokuEndpoint, Method};
pub struct OAuthCreate<'a> {
pub params: OAuthCreateParams<'a>,
}
#[cfg(feature = "builder")]
impl<'a> OAuthCreate<'a> {
pub fn new(scope: Vec<&'a str>) -> OAuthCreate<'a> {
OAuthCreate {
params: OAuthCreateParams {
scope: scope,
client: None,
description: None,
expires_in: None,
},
}
}
pub fn client(&mut self, client: &'a str) -> &mut Self {
self.params.client = Some(client);
self
}
pub fn description(&mut self, description: &'a str) -> &mut Self {
self.params.description = Some(description);
self
}
pub fn expires_in(&mut self, expires_in: u32) -> &mut Self {
self.params.expires_in = Some(expires_in);
self
}
pub fn build(&self) -> OAuthCreate<'a> {
OAuthCreate {
params: OAuthCreateParams {
scope: self.params.scope.clone(),
client: self.params.client,
description: self.params.description,
expires_in: self.params.expires_in,
},
}
}
}
#[derive(Serialize, Clone, Debug)]
pub struct OAuthCreateParams<'a> {
pub scope: Vec<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub client: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<&'a str>,
pub expires_in: Option<u32>,
}
impl<'a> HerokuEndpoint<OAuth, (), OAuthCreateParams<'a>> for OAuthCreate<'a> {
fn method(&self) -> Method {
Method::Post
}
fn path(&self) -> String {
format!("oauth/authorizations")
}
fn body(&self) -> Option<OAuthCreateParams<'a>> {
Some(self.params.clone())
}
}
pub struct OAuthRegenerate<'a> {
pub oauth_id: &'a str,
}
#[cfg(feature = "builder")]
impl<'a> OAuthRegenerate<'a> {
pub fn new(oauth_id: &'a str) -> OAuthRegenerate<'a> {
OAuthRegenerate { oauth_id }
}
}
impl<'a> HerokuEndpoint<OAuth> for OAuthRegenerate<'a> {
fn method(&self) -> Method {
Method::Post
}
fn path(&self) -> String {
format!(
"oauth/authorizations/{}/actions/regenerate-tokens",
self.oauth_id
)
}
}
pub struct OAuthClientCreate<'a> {
pub params: OAuthClientCreateParams<'a>,
}
#[cfg(feature = "builder")]
impl<'a> OAuthClientCreate<'a> {
pub fn new(name: &'a str, redirect_uri: &'a str) -> OAuthClientCreate<'a> {
OAuthClientCreate {
params: OAuthClientCreateParams { name, redirect_uri },
}
}
}
#[derive(Serialize, Clone, Debug)]
pub struct OAuthClientCreateParams<'a> {
pub name: &'a str,
pub redirect_uri: &'a str,
}
impl<'a> HerokuEndpoint<OAuthClient, (), OAuthClientCreateParams<'a>> for OAuthClientCreate<'a> {
fn method(&self) -> Method {
Method::Post
}
fn path(&self) -> String {
format!("oauth/clients")
}
fn body(&self) -> Option<OAuthClientCreateParams<'a>> {
Some(self.params.clone())
}
}
pub struct OAuthClientRotateCredentials<'a> {
pub client_id: &'a str,
}
#[cfg(feature = "builder")]
impl<'a> OAuthClientRotateCredentials<'a> {
pub fn new(client_id: &'a str) -> OAuthClientRotateCredentials<'a> {
OAuthClientRotateCredentials { client_id }
}
}
impl<'a> HerokuEndpoint<OAuthClient> for OAuthClientRotateCredentials<'a> {
fn method(&self) -> Method {
Method::Post
}
fn path(&self) -> String {
format!(
"oauth/clients/{}/actions/rotate-credentials",
self.client_id
)
}
}
pub struct OAuthTokenCreate<'a> {
pub params: OAuthTokenCreateParams<'a>,
}
#[cfg(feature = "builder")]
impl<'a> OAuthTokenCreate<'a> {
pub fn new(
client_secret: &'a str,
grant_code: &'a str,
grant_type: &'a str,
refresh_token: &'a str,
) -> OAuthTokenCreate<'a> {
OAuthTokenCreate {
params: OAuthTokenCreateParams {
client: Client {
secret: client_secret,
},
grant: Grant {
code: grant_code,
type_field: grant_type,
},
refresh_token: RefreshToken {
token: refresh_token,
},
},
}
}
}
#[derive(Serialize, Clone, Debug)]
pub struct OAuthTokenCreateParams<'a> {
pub client: Client<'a>,
pub grant: Grant<'a>,
pub refresh_token: RefreshToken<'a>,
}
#[derive(Serialize, Clone, Debug)]
pub struct RefreshToken<'a> {
pub token: &'a str,
}
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
pub struct Grant<'a> {
pub code: &'a str,
#[serde(rename = "type")]
pub type_field: &'a str,
}
#[derive(Serialize, Clone, Debug)]
pub struct Client<'a> {
pub secret: &'a str,
}
impl<'a> HerokuEndpoint<OAuthToken, (), OAuthTokenCreateParams<'a>> for OAuthTokenCreate<'a> {
fn method(&self) -> Method {
Method::Post
}
fn path(&self) -> String {
format!("oauth/tokens")
}
fn body(&self) -> Option<OAuthTokenCreateParams<'a>> {
Some(self.params.clone())
}
}