use crate::api::endpoint::{Endpoint, HttpMethod};
use crate::models::Client;
use crate::response::{EmptyResponse, SiteResponse};
use serde::Serialize;
use serde_json::Value;
#[derive(Debug, Clone)]
pub struct GetClients {
pub site_id: String,
pub offset: Option<usize>,
pub limit: Option<usize>,
}
impl GetClients {
pub fn new(site_id: impl Into<String>) -> Self {
Self {
site_id: site_id.into(),
offset: None,
limit: None,
}
}
pub fn with_pagination(site_id: impl Into<String>, offset: usize, limit: usize) -> Self {
Self {
site_id: site_id.into(),
offset: Some(offset),
limit: Some(limit),
}
}
pub fn offset(mut self, offset: usize) -> Self {
self.offset = Some(offset);
self
}
pub fn limit(mut self, limit: usize) -> Self {
self.limit = Some(limit);
self
}
}
impl Endpoint for GetClients {
const PATH: &'static str = "sites/{site_id}/clients";
const METHOD: HttpMethod = HttpMethod::Get;
type Response = SiteResponse<Client>;
fn build_path(&self) -> String {
format!("sites/{}/clients", self.site_id)
}
fn query_params(&self) -> Vec<(&'static str, String)> {
let mut params = vec![];
if let Some(offset) = self.offset {
params.push(("offset", offset.to_string()));
}
if let Some(limit) = self.limit {
params.push(("limit", limit.to_string()));
}
params
}
}
#[derive(Debug, Clone)]
pub struct GetClient {
pub site_id: String,
pub id: String,
}
impl GetClient {
pub fn new(site_id: impl Into<String>, id: impl Into<String>) -> Self {
Self {
site_id: site_id.into(),
id: id.into(),
}
}
}
impl Endpoint for GetClient {
const PATH: &'static str = "sites/{site_id}/clients/{id}";
const METHOD: HttpMethod = HttpMethod::Get;
type Response = SiteResponse<Client>;
fn build_path(&self) -> String {
format!("sites/{}/clients/{}", self.site_id, self.id)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ClientAction {
Block,
Unblock,
Reconnect,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct ClientActionRequest {
action: ClientAction,
}
#[derive(Debug, Clone)]
pub struct ExecuteClientAction {
pub site_id: String,
pub client_id: String,
pub action: ClientAction,
}
impl ExecuteClientAction {
pub fn new(
site_id: impl Into<String>,
client_id: impl Into<String>,
action: ClientAction,
) -> Self {
Self {
site_id: site_id.into(),
client_id: client_id.into(),
action,
}
}
pub fn block(site_id: impl Into<String>, client_id: impl Into<String>) -> Self {
Self::new(site_id, client_id, ClientAction::Block)
}
pub fn unblock(site_id: impl Into<String>, client_id: impl Into<String>) -> Self {
Self::new(site_id, client_id, ClientAction::Unblock)
}
pub fn reconnect(site_id: impl Into<String>, client_id: impl Into<String>) -> Self {
Self::new(site_id, client_id, ClientAction::Reconnect)
}
}
impl Endpoint for ExecuteClientAction {
const PATH: &'static str = "sites/{site_id}/clients/{client_id}/action";
const METHOD: HttpMethod = HttpMethod::Post;
type Response = EmptyResponse;
fn build_path(&self) -> String {
format!("sites/{}/clients/{}/action", self.site_id, self.client_id)
}
fn request_body(&self) -> Result<Option<Value>, serde_json::Error> {
Ok(Some(serde_json::to_value(ClientActionRequest {
action: self.action,
})?))
}
}