use crate::token::Jwt; use crate::transport::Transport; use super::{ApiRequest, ApiResponse, BuildableRequest}; use crate::protobufs::custom::CTwoFactor_Time_Request; use crate::protobufs::service_twofactor::*; const SERVICE_NAME: &str = "ITwoFactorService"; #[derive(Debug)] pub struct TwoFactorClient where T: Transport, { transport: T, } impl TwoFactorClient where T: Transport, { #[must_use] pub fn new(transport: T) -> Self { Self { transport } } pub fn add_authenticator( &mut self, req: CTwoFactor_AddAuthenticator_Request, access_token: &Jwt, ) -> anyhow::Result> { let req = ApiRequest::new(SERVICE_NAME, "AddAuthenticator", 1, req) .with_access_token(access_token); let resp = self .transport .send_request::( req, )?; Ok(resp) } pub fn finalize_authenticator( &mut self, req: CTwoFactor_FinalizeAddAuthenticator_Request, access_token: &Jwt, ) -> anyhow::Result> { let req = ApiRequest::new(SERVICE_NAME, "FinalizeAddAuthenticator", 1, req) .with_access_token(access_token); let resp = self .transport .send_request::( req, )?; Ok(resp) } pub fn remove_authenticator( &mut self, req: CTwoFactor_RemoveAuthenticator_Request, access_token: &Jwt, ) -> anyhow::Result> { let req = ApiRequest::new(SERVICE_NAME, "RemoveAuthenticator", 1, req) .with_access_token(access_token); let resp = self .transport .send_request::( req, )?; Ok(resp) } pub fn query_status( &mut self, req: CTwoFactor_Status_Request, access_token: &Jwt, ) -> anyhow::Result> { let req = ApiRequest::new(SERVICE_NAME, "QueryStatus", 1, req).with_access_token(access_token); let resp = self .transport .send_request::(req)?; Ok(resp) } pub fn query_time(&mut self) -> anyhow::Result> { let req = ApiRequest::new(SERVICE_NAME, "QueryTime", 1, CTwoFactor_Time_Request::new()); let resp = self .transport .send_request::(req)?; Ok(resp) } } macro_rules! impl_buildable_req { ($type:ty, $needs_auth:literal) => { impl BuildableRequest for $type { fn method() -> reqwest::Method { reqwest::Method::POST } fn requires_access_token() -> bool { $needs_auth } } }; } impl_buildable_req!(CTwoFactor_AddAuthenticator_Request, true); impl_buildable_req!(CTwoFactor_FinalizeAddAuthenticator_Request, true); impl_buildable_req!(CTwoFactor_RemoveAuthenticator_Request, true); impl_buildable_req!(CTwoFactor_Status_Request, true); impl_buildable_req!(CTwoFactor_Time_Request, false);