use crate::client::{IdkollenError, PollOptions, WaitError};
use crate::models::{AgeVerificationRequest, AgeVerificationStatus};
use crate::models::{
BankIdSeAuthRequest, BankIdSePhoneAuthRequest, BankIdSePhoneSignRequest, BankIdSeSignRequest,
BankIdSeStatus, BankIdSeVerifyRequest, BankIdSeVerifyResponse,
};
use std::time::Instant;
#[cfg(feature = "async")]
use crate::client::IdkollenClient;
#[cfg(feature = "async")]
pub struct BankIdSeEndpoint<'a>(pub(crate) &'a IdkollenClient);
#[cfg(feature = "async")]
impl BankIdSeEndpoint<'_> {
pub async fn auth(&self, req: BankIdSeAuthRequest) -> Result<BankIdSeStatus, IdkollenError> {
self.0.post("/v3/bankid-se/auth", &req).await
}
pub async fn phone_auth(
&self,
req: BankIdSePhoneAuthRequest,
) -> Result<BankIdSeStatus, IdkollenError> {
self.0.post("/v3/bankid-se/phone/auth", &req).await
}
pub async fn sign(&self, req: BankIdSeSignRequest) -> Result<BankIdSeStatus, IdkollenError> {
self.0.post("/v3/bankid-se/sign", &req).await
}
pub async fn phone_sign(
&self,
req: BankIdSePhoneSignRequest,
) -> Result<BankIdSeStatus, IdkollenError> {
self.0.post("/v3/bankid-se/phone/sign", &req).await
}
pub async fn verify(
&self,
req: BankIdSeVerifyRequest,
) -> Result<BankIdSeVerifyResponse, IdkollenError> {
self.0.post("/v3/bankid-se/verify", &req).await
}
pub async fn age_verification(
&self,
req: AgeVerificationRequest,
) -> Result<AgeVerificationStatus, IdkollenError> {
self.0.post("/v3/bankid-se/age-verification", &req).await
}
pub async fn auth_status(&self, id: &str) -> Result<BankIdSeStatus, IdkollenError> {
self.0.get(&format!("/v3/bankid-se/auth/{}", id)).await
}
pub async fn sign_status(&self, id: &str) -> Result<BankIdSeStatus, IdkollenError> {
self.0.get(&format!("/v3/bankid-se/sign/{}", id)).await
}
pub async fn age_verification_status(
&self,
id: &str,
) -> Result<AgeVerificationStatus, IdkollenError> {
self.0
.get(&format!("/v3/bankid-se/age-verification/{}", id))
.await
}
pub async fn cancel_auth(&self, id: &str) -> Result<(), IdkollenError> {
self.0.delete(&format!("/v3/bankid-se/auth/{}", id)).await
}
pub async fn cancel_sign(&self, id: &str) -> Result<(), IdkollenError> {
self.0.delete(&format!("/v3/bankid-se/sign/{}", id)).await
}
pub async fn cancel_age_verification(&self, id: &str) -> Result<(), IdkollenError> {
self.0
.delete(&format!("/v3/bankid-se/age-verification/{}", id))
.await
}
pub async fn wait_for_auth(
&self,
id: &str,
opts: PollOptions,
) -> Result<BankIdSeStatus, WaitError> {
let deadline = Instant::now() + opts.timeout;
loop {
let status = self.auth_status(id).await?;
match status {
BankIdSeStatus::Pending(_) => {
if Instant::now() >= deadline {
return Err(WaitError::Timeout);
}
tokio::time::sleep(opts.interval).await;
},
terminal => return Ok(terminal),
}
}
}
pub async fn wait_for_sign(
&self,
id: &str,
opts: PollOptions,
) -> Result<BankIdSeStatus, WaitError> {
let deadline = Instant::now() + opts.timeout;
loop {
let status = self.sign_status(id).await?;
match status {
BankIdSeStatus::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 BankIdSeBlockingEndpoint<'a>(pub(crate) &'a IdkollenBlockingClient);
#[cfg(feature = "blocking")]
impl BankIdSeBlockingEndpoint<'_> {
pub fn auth(&self, req: BankIdSeAuthRequest) -> Result<BankIdSeStatus, IdkollenError> {
self.0.post("/v3/bankid-se/auth", &req)
}
pub fn phone_auth(
&self,
req: BankIdSePhoneAuthRequest,
) -> Result<BankIdSeStatus, IdkollenError> {
self.0.post("/v3/bankid-se/phone/auth", &req)
}
pub fn sign(&self, req: BankIdSeSignRequest) -> Result<BankIdSeStatus, IdkollenError> {
self.0.post("/v3/bankid-se/sign", &req)
}
pub fn phone_sign(
&self,
req: BankIdSePhoneSignRequest,
) -> Result<BankIdSeStatus, IdkollenError> {
self.0.post("/v3/bankid-se/phone/sign", &req)
}
pub fn verify(
&self,
req: BankIdSeVerifyRequest,
) -> Result<BankIdSeVerifyResponse, IdkollenError> {
self.0.post("/v3/bankid-se/verify", &req)
}
pub fn age_verification(
&self,
req: AgeVerificationRequest,
) -> Result<AgeVerificationStatus, IdkollenError> {
self.0.post("/v3/bankid-se/age-verification", &req)
}
pub fn auth_status(&self, id: &str) -> Result<BankIdSeStatus, IdkollenError> {
self.0.get(&format!("/v3/bankid-se/auth/{}", id))
}
pub fn sign_status(&self, id: &str) -> Result<BankIdSeStatus, IdkollenError> {
self.0.get(&format!("/v3/bankid-se/sign/{}", id))
}
pub fn age_verification_status(
&self,
id: &str,
) -> Result<AgeVerificationStatus, IdkollenError> {
self.0
.get(&format!("/v3/bankid-se/age-verification/{}", id))
}
pub fn cancel_auth(&self, id: &str) -> Result<(), IdkollenError> {
self.0.delete(&format!("/v3/bankid-se/auth/{}", id))
}
pub fn cancel_sign(&self, id: &str) -> Result<(), IdkollenError> {
self.0.delete(&format!("/v3/bankid-se/sign/{}", id))
}
pub fn cancel_age_verification(&self, id: &str) -> Result<(), IdkollenError> {
self.0
.delete(&format!("/v3/bankid-se/age-verification/{}", id))
}
pub fn wait_for_auth(&self, id: &str, opts: PollOptions) -> Result<BankIdSeStatus, WaitError> {
let deadline = Instant::now() + opts.timeout;
loop {
let status = self.auth_status(id)?;
match status {
BankIdSeStatus::Pending(_) => {
if Instant::now() >= deadline {
return Err(WaitError::Timeout);
}
std::thread::sleep(opts.interval);
},
terminal => return Ok(terminal),
}
}
}
pub fn wait_for_sign(&self, id: &str, opts: PollOptions) -> Result<BankIdSeStatus, WaitError> {
let deadline = Instant::now() + opts.timeout;
loop {
let status = self.sign_status(id)?;
match status {
BankIdSeStatus::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),
}
}
}
}