use std::sync::Arc;
use crate::core::{
CreditResponse, DeviceResponse, HttpClient, IntoBody, Json, Method, RequestOptions, Result,
};
pub mod data;
pub mod messaging;
pub mod platform;
#[derive(Clone, Debug)]
pub struct BaseService {
client: Arc<HttpClient>,
options: RequestOptions,
}
impl BaseService {
pub fn new(client: Arc<HttpClient>) -> Self {
Self {
client,
options: RequestOptions::default(),
}
}
pub fn http(&self) -> &Arc<HttpClient> {
&self.client
}
pub fn options(&self) -> &RequestOptions {
&self.options
}
pub fn with_options(&self, options: RequestOptions) -> Self {
Self {
client: self.client.clone(),
options,
}
}
pub fn url(&self, path: &str) -> String {
self.client.url(path)
}
pub async fn request(&self, method: Method, path: &str, body: impl IntoBody) -> Result<Json> {
self.client
.request_json(method, path, body.into_body(), &self.options)
.await
}
pub async fn get(&self, path: &str) -> Result<Json> {
self.client.get(path, &self.options).await
}
pub async fn get_query(&self, path: &str, query: impl IntoBody) -> Result<Json> {
let options = self.options.merge_query(query.into_body());
self.client.get(path, &options).await
}
pub async fn post(&self, path: &str, body: impl IntoBody) -> Result<Json> {
self.client
.post(path, body.into_body(), &self.options)
.await
}
pub async fn put(&self, path: &str, body: impl IntoBody) -> Result<Json> {
self.client.put(path, body.into_body(), &self.options).await
}
pub async fn patch(&self, path: &str, body: impl IntoBody) -> Result<Json> {
self.client
.patch(path, body.into_body(), &self.options)
.await
}
pub async fn delete(&self, path: &str, body: impl IntoBody) -> Result<Json> {
self.client
.delete(path, body.into_body(), &self.options)
.await
}
pub async fn download(&self, path: &str) -> Result<Vec<u8>> {
self.client
.bytes(Method::Get, path, None, &self.options)
.await
}
}
#[derive(Clone, Debug)]
pub struct DeviceProxyService {
base: BaseService,
name: String,
}
impl DeviceProxyService {
pub fn new(client: Arc<HttpClient>, name: impl Into<String>) -> Self {
Self {
base: BaseService::new(client),
name: name.into(),
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn with_options(&self, options: RequestOptions) -> Self {
Self {
base: self.base.with_options(options),
name: self.name.clone(),
}
}
pub async fn request(&self, action: &str, body: impl IntoBody) -> Result<DeviceResponse> {
let path = self.path(action);
Ok(DeviceResponse(self.base.post(&path, body).await?))
}
pub async fn queue(&self, action: &str, body: impl IntoBody) -> Result<DeviceResponse> {
let path = format!("{}/queue", self.path(action));
Ok(DeviceResponse(self.base.post(&path, body).await?))
}
fn path(&self, action: &str) -> String {
let action = action.trim_matches('/');
if action.is_empty() {
self.name.clone()
} else {
format!("{}/{}", self.name, action)
}
}
}
impl std::ops::Deref for DeviceProxyService {
type Target = BaseService;
fn deref(&self) -> &BaseService {
&self.base
}
}
#[derive(Clone, Debug)]
pub struct CreditService {
base: BaseService,
}
impl CreditService {
pub fn new(client: Arc<HttpClient>) -> Self {
Self {
base: BaseService::new(client),
}
}
pub fn with_options(&self, options: RequestOptions) -> Self {
Self {
base: self.base.with_options(options),
}
}
pub async fn request(&self, service: &str, body: impl IntoBody) -> Result<CreditResponse> {
let path = format!("consulta/{service}/credits");
Ok(CreditResponse(self.base.post(&path, body).await?))
}
pub async fn credits(&self, service: &str) -> Result<CreditResponse> {
let path = format!("consulta/{service}/credits");
Ok(CreditResponse(self.base.get(&path).await?))
}
pub(crate) async fn credit_post(
&self,
path: &str,
body: impl IntoBody,
) -> Result<CreditResponse> {
Ok(CreditResponse(self.base.post(path, body).await?))
}
}
impl std::ops::Deref for CreditService {
type Target = BaseService;
fn deref(&self) -> &BaseService {
&self.base
}
}