#[cfg(any(feature = "sync", feature = "async"))]
use crate::traits::*;
#[cfg(any(feature = "sync", feature = "async"))]
use crate::{structs::*, INSTANCE};
#[cfg(any(feature = "sync", feature = "async"))]
use std::error::Error;
#[cfg(feature = "sync")]
#[derive(Clone)]
pub struct ClientSync {
pub method: MethodSync,
pub instance: String,
}
#[cfg(feature = "sync")]
impl Default for ClientSync {
fn default() -> Self {
Self {
method: MethodSync::default(),
instance: INSTANCE.to_string(),
}
}
}
#[cfg(feature = "sync")]
impl ClientSync {
pub fn with_method(instance: String, method: MethodSync) -> Self {
Self { method, instance }
}
pub fn instance(mut self, instance: String) -> Self {
self.set_instance(instance);
self
}
pub fn set_method(&mut self, method: MethodSync) {
self.method = method;
}
pub fn method(mut self, method: MethodSync) -> Self {
self.set_method(method);
self
}
}
#[cfg(feature = "sync")]
impl ClientSyncTrait for ClientSync {
fn new(instance: String) -> Self {
Self {
method: MethodSync::default(),
instance,
}
}
fn set_instance(&mut self, instance: String) {
self.instance = instance;
}
fn get_instance(&self) -> &str {
&self.instance
}
fn fetch(&self, url: &str) -> Result<String, Box<dyn Error>> {
self.method.fetch(&format!(
"{}/{}",
self.instance,
url.trim_start_matches('/')
))
}
}
#[cfg(feature = "async")]
#[derive(Clone)]
pub struct ClientAsync {
pub method: MethodAsync,
pub instance: String,
}
#[cfg(feature = "async")]
impl Default for ClientAsync {
fn default() -> Self {
Self {
method: MethodAsync::default(),
instance: INSTANCE.to_string(),
}
}
}
#[cfg(feature = "async")]
impl ClientAsync {
pub fn new(instance: String, method: MethodAsync) -> Self {
Self { method, instance }
}
pub fn set_instance(&mut self, instance: String) {
self.instance = instance;
}
pub fn instance(mut self, instance: String) -> Self {
self.set_instance(instance);
self
}
pub fn set_method(&mut self, method: MethodAsync) {
self.method = method;
}
pub fn method(mut self, method: MethodAsync) -> Self {
self.set_method(method);
self
}
}
#[cfg(feature = "async")]
#[async_trait::async_trait]
impl ClientAsyncTrait for ClientAsync {
fn new(instance: String) -> Self {
Self {
method: MethodAsync::default(),
instance,
}
}
fn set_instance(&mut self, instance: String) {
self.instance = instance;
}
fn get_instance(&self) -> &str {
&self.instance
}
async fn fetch(&self, url: &str) -> Result<String, Box<dyn Error>> {
self.method
.fetch(&format!(
"{}/{}",
self.instance,
url.trim_start_matches('/')
))
.await
}
}
#[cfg(feature = "async")]
impl ClientAsyncClone for ClientAsync {}