use crate::client::{IdkollenError, PollOptions, WaitError};
use crate::models::{AgeVerificationRequest, AgeVerificationStatus};
use crate::models::{FtnAuthRequest, FtnStatus};
use std::time::Instant;
#[cfg(feature = "async")]
use crate::client::IdkollenClient;
#[cfg(feature = "async")]
pub struct FtnEndpoint<'a>(pub(crate) &'a IdkollenClient);
#[cfg(feature = "async")]
impl FtnEndpoint<'_> {
pub async fn auth(&self, req: FtnAuthRequest) -> Result<FtnStatus, IdkollenError> {
self.0.post("/v3/ftn/auth", &req).await
}
pub async fn age_verification(
&self,
req: AgeVerificationRequest,
) -> Result<AgeVerificationStatus, IdkollenError> {
self.0.post("/v3/ftn/age-verification", &req).await
}
pub async fn auth_status(&self, id: &str) -> Result<FtnStatus, IdkollenError> {
self.0.get(&format!("/v3/ftn/auth/{}", id)).await
}
pub async fn age_verification_status(
&self,
id: &str,
) -> Result<AgeVerificationStatus, IdkollenError> {
self.0
.get(&format!("/v3/ftn/age-verification/{}", id))
.await
}
pub async fn cancel_auth(&self, id: &str) -> Result<(), IdkollenError> {
self.0.delete(&format!("/v3/ftn/auth/{}", id)).await
}
pub async fn cancel_age_verification(&self, id: &str) -> Result<(), IdkollenError> {
self.0
.delete(&format!("/v3/ftn/age-verification/{}", id))
.await
}
pub async fn wait_for_auth(&self, id: &str, opts: PollOptions) -> Result<FtnStatus, WaitError> {
let deadline = Instant::now() + opts.timeout;
loop {
let status = self.auth_status(id).await?;
match status {
FtnStatus::Pending(_) => {
if Instant::now() >= deadline {
return Err(WaitError::Timeout);
}
tokio::time::sleep(opts.interval).await;
},
terminal => return Ok(terminal),
}
}
}
pub async fn wait_for_age_verification(
&self,
id: &str,
opts: PollOptions,
) -> Result<AgeVerificationStatus, WaitError> {
let deadline = Instant::now() + opts.timeout;
loop {
let status = self.age_verification_status(id).await?;
match status {
AgeVerificationStatus::Pending(_) => {
if Instant::now() >= deadline {
return Err(WaitError::Timeout);
}
tokio::time::sleep(opts.interval).await;
},
terminal => return Ok(terminal),
}
}
}
}
#[cfg(feature = "blocking")]
use crate::client::IdkollenBlockingClient;
#[cfg(feature = "blocking")]
pub struct FtnBlockingEndpoint<'a>(pub(crate) &'a IdkollenBlockingClient);
#[cfg(feature = "blocking")]
impl FtnBlockingEndpoint<'_> {
pub fn auth(&self, req: FtnAuthRequest) -> Result<FtnStatus, IdkollenError> {
self.0.post("/v3/ftn/auth", &req)
}
pub fn age_verification(
&self,
req: AgeVerificationRequest,
) -> Result<AgeVerificationStatus, IdkollenError> {
self.0.post("/v3/ftn/age-verification", &req)
}
pub fn auth_status(&self, id: &str) -> Result<FtnStatus, IdkollenError> {
self.0.get(&format!("/v3/ftn/auth/{}", id))
}
pub fn age_verification_status(
&self,
id: &str,
) -> Result<AgeVerificationStatus, IdkollenError> {
self.0.get(&format!("/v3/ftn/age-verification/{}", id))
}
pub fn cancel_auth(&self, id: &str) -> Result<(), IdkollenError> {
self.0.delete(&format!("/v3/ftn/auth/{}", id))
}
pub fn cancel_age_verification(&self, id: &str) -> Result<(), IdkollenError> {
self.0.delete(&format!("/v3/ftn/age-verification/{}", id))
}
pub fn wait_for_auth(&self, id: &str, opts: PollOptions) -> Result<FtnStatus, WaitError> {
let deadline = Instant::now() + opts.timeout;
loop {
let status = self.auth_status(id)?;
match status {
FtnStatus::Pending(_) => {
if Instant::now() >= deadline {
return Err(WaitError::Timeout);
}
std::thread::sleep(opts.interval);
},
terminal => return Ok(terminal),
}
}
}
pub fn wait_for_age_verification(
&self,
id: &str,
opts: PollOptions,
) -> Result<AgeVerificationStatus, WaitError> {
let deadline = Instant::now() + opts.timeout;
loop {
let status = self.age_verification_status(id)?;
match status {
AgeVerificationStatus::Pending(_) => {
if Instant::now() >= deadline {
return Err(WaitError::Timeout);
}
std::thread::sleep(opts.interval);
},
terminal => return Ok(terminal),
}
}
}
}