use crate::error::{Result, SmartIdClientError};
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub(crate) enum SmartIdAPIResponse<T> {
Success(T),
Error(SmartIDErrorResponse),
}
impl<T> SmartIdAPIResponse<T> {
pub fn into_result(self) -> Result<T> {
match self {
SmartIdAPIResponse::Success(data) => Ok(data),
SmartIdAPIResponse::Error(error) => match error.status {
400 => Err(SmartIdClientError::BadRequestException),
401 => Err(
SmartIdClientError::RelyingPartyAccountConfigurationException(
"Request is unauthorized",
),
),
403 => Err(
SmartIdClientError::RelyingPartyAccountConfigurationException(
"Request is forbidden",
),
),
404 => Err(SmartIdClientError::NotFoundException),
471 => Err(SmartIdClientError::NoSuitableAccountOfRequestedTypeFoundException),
472 => Err(SmartIdClientError::PersonShouldViewSmartIdPortalException),
480 => Err(SmartIdClientError::ClientOutdatedException),
580 => Err(SmartIdClientError::SystemIsUnderMaintenanceException),
_ => Err(SmartIdClientError::SmartIDAPIException(format!(
"Unknown error: {}",
error.status
))),
},
}
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub(crate) struct SmartIDErrorResponse {
#[serde(rename = "type")]
pub error_type: String,
pub title: String,
pub status: i64,
pub detail: String,
pub code: i64,
pub message: String,
}