use core::future::Future;
use crate::application::oauth::callback::AuthorizationCallback;
use crate::application::oauth::redeem::TokenEndpoint;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Fetched {
pub status: u16,
pub body: Vec<u8>,
}
impl Fetched {
#[must_use]
pub const fn is_success(&self) -> bool {
self.status >= 200 && self.status < 300
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum EnvironmentError {
#[error("{0}")]
Unavailable(String),
#[error("the authorization was not completed: {0}")]
Declined(String),
}
pub trait OAuthEnvironment: Send + Sync + 'static {
type Token: TokenEndpoint + Send + Sync;
fn token_endpoint(&self, url: &str) -> Self::Token;
fn get(&self, url: &str) -> impl Future<Output = Result<Fetched, EnvironmentError>> + Send;
fn post_json(
&self,
url: &str,
body: &str,
) -> impl Future<Output = Result<Fetched, EnvironmentError>> + Send;
fn authorize(
&self,
authorization_url: &str,
) -> impl Future<Output = Result<AuthorizationCallback, EnvironmentError>> + Send;
}