use crate::{Error, OAuthClient};
use std::sync::Arc;
impl OAuthClient {
pub async fn oauth_authorize(&self) -> OauthAuthorizeUrl {
OauthAuthorizeUrl {
api_host: self.api_host.lock().await.clone(),
client_id: self.client_id.lock().await.clone(),
redirect_uri: "".to_string(),
scope: "".to_string(),
response_type: "code".to_string(),
state: None,
relogin: None,
drive: None,
}
}
}
#[derive(Debug, Clone, Default, Eq, PartialEq)]
pub struct OauthAuthorizeUrl {
pub api_host: Arc<String>,
pub client_id: Arc<String>,
pub redirect_uri: String,
pub scope: String,
pub response_type: String,
pub state: Option<String>,
pub relogin: Option<bool>,
pub drive: Option<String>,
}
impl OauthAuthorizeUrl {
pub fn api_host(mut self, api_host: impl Into<Arc<String>>) -> Self {
self.api_host = api_host.into();
self
}
pub fn client_id(mut self, client_id: impl Into<String>) -> Self {
self.client_id = Arc::new(client_id.into());
self
}
pub fn redirect_uri(mut self, redirect_uri: impl Into<String>) -> Self {
self.redirect_uri = redirect_uri.into();
self
}
pub fn scope(mut self, scope: impl Into<String>) -> Self {
self.scope = scope.into();
self
}
pub fn response_type(mut self, response_type: impl Into<String>) -> Self {
self.response_type = response_type.into();
self
}
pub fn state(mut self, state: impl Into<String>) -> Self {
self.state = Some(state.into());
self
}
pub fn relogin(mut self, relogin: bool) -> Self {
self.relogin = Some(relogin);
self
}
pub fn drive(mut self, drive: impl Into<String>) -> Self {
self.drive = Some(drive.into());
self
}
pub fn build(&self) -> crate::Result<String> {
if self.client_id.is_empty() {
return Err(Error::require_param_missing("client_id"));
}
if self.redirect_uri.is_empty() {
return Err(Error::require_param_missing("redirect_uri"));
}
if self.scope.is_empty() {
return Err(Error::require_param_missing("scope"));
}
let mut url = url::Url::parse(self.api_host.as_str())?;
url.set_path("/oauth/authorize");
url.query_pairs_mut()
.append_pair("client_id", self.client_id.as_str())
.append_pair("redirect_uri", self.redirect_uri.as_str())
.append_pair("scope", self.scope.as_str())
.append_pair("response_type", self.response_type.as_str());
if let Some(state) = &self.state {
url.query_pairs_mut().append_pair("state", state.as_str());
}
if let Some(relogin) = &self.relogin {
url.query_pairs_mut()
.append_pair("relogin", relogin.to_string().as_str());
}
if let Some(drive) = &self.drive {
url.query_pairs_mut().append_pair("drive", drive.as_str());
}
Ok(url.to_string())
}
}