use std::time::Duration;
use tonic::transport::Channel;
use tonic::Request;
use crate::proto::wallguard::wall_guard_client::WallGuardClient;
pub use crate::proto::wallguard::*;
mod proto;
#[derive(Clone)]
pub struct WallGuardGrpcInterface {
client: WallGuardClient<Channel>,
}
impl WallGuardGrpcInterface {
#[allow(clippy::missing_panics_doc)]
pub async fn new(addr: &str, port: u16) -> Self {
let s = format!("http://{addr}:{port}");
let Ok(channel) = Channel::from_shared(s)
.expect("Failed to parse address")
.timeout(Duration::from_secs(10))
.connect()
.await
else {
println!("Failed to connect to the server. Retrying in 10 seconds...");
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
return Box::pin(WallGuardGrpcInterface::new(addr, port)).await;
};
Self {
client: WallGuardClient::new(channel),
}
}
#[allow(clippy::missing_errors_doc)]
pub async fn login(&mut self, app_id: String, app_secret: String) -> Result<String, String> {
let response = self
.client
.login(Request::new(LoginRequest { app_id, app_secret }))
.await
.map_err(|e| e.to_string())?;
Ok(response.into_inner().token)
}
#[allow(clippy::missing_errors_doc)]
pub async fn heartbeat(&mut self, token: String) -> Result<HeartbeatResponse, String> {
self.client
.heartbeat(Request::new(HeartbeatRequest {
auth: Some(Authentication { token }),
}))
.await
.map(|r| r.into_inner())
.map_err(|e| e.to_string())
}
#[allow(clippy::missing_errors_doc)]
pub async fn handle_packets(&mut self, message: Packets) -> Result<CommonResponse, String> {
self.client
.handle_packets(Request::new(message))
.await
.map(|r| r.into_inner())
.map_err(|e| e.to_string())
}
#[allow(clippy::missing_errors_doc)]
pub async fn handle_config(
&mut self,
message: ConfigSnapshot,
) -> Result<CommonResponse, String> {
self.client
.handle_config(Request::new(message))
.await
.map(|r| r.into_inner())
.map_err(|e| e.to_string())
}
#[allow(clippy::missing_errors_doc)]
pub async fn setup_client(&mut self, request: SetupRequest) -> Result<CommonResponse, String> {
self.client
.setup(Request::new(request))
.await
.map(|response| response.into_inner())
.map_err(|e| e.to_string())
}
pub async fn device_status(&mut self, token: String) -> Result<StatusResponse, String> {
let response = self
.client
.status(Request::new(StatusRequest {
auth: Some(Authentication { token }),
}))
.await
.map(|response| response.into_inner())
.map_err(|e| e.to_string())?;
Ok(response)
}
}