use serde::{Deserialize, Serialize};
use std::collections::HashMap as StdHashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Client {
headers: StdHashMap<String, String>,
options: StdHashMap<String, String>,
}
impl Client {
pub fn new() -> Self {
Self {
headers: StdHashMap::new(),
options: StdHashMap::new(),
}
}
#[cfg(feature = "array-tuples")]
pub fn with_headers<F>(mut self, f: F) -> Self
where
F: FnOnce() -> hashbrown::HashMap<&'static str, &'static str>,
{
let headers_map = f();
for (k, v) in headers_map {
self.headers.insert(k.to_string(), v.to_string());
}
self
}
#[cfg(feature = "array-tuples")]
pub fn with_options<F>(mut self, f: F) -> Self
where
F: FnOnce() -> hashbrown::HashMap<&'static str, &'static str>,
{
let options_map = f();
for (k, v) in options_map {
self.options.insert(k.to_string(), v.to_string());
}
self
}
}
impl Default for Client {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Database {
pub host: String,
pub port: String,
pub database: String,
pub user: String,
pub password: Option<String>,
}
impl Database {
#[cfg(feature = "array-tuples")]
pub fn connect<F>(f: F) -> Self
where
F: FnOnce() -> hashbrown::HashMap<&'static str, &'static str>,
{
let config = f();
Self {
host: config.get("host").unwrap_or(&"localhost").to_string(),
port: config.get("port").unwrap_or(&"5432").to_string(),
database: config.get("database").unwrap_or(&"postgres").to_string(),
user: config.get("user").unwrap_or(&"postgres").to_string(),
password: config.get("password").map(|s| s.to_string()),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiClient {
endpoint: Option<String>,
auth: Option<StdHashMap<String, String>>,
rate_limit: Option<StdHashMap<String, String>>,
}
impl ApiClient {
pub fn new() -> Self {
Self {
endpoint: None,
auth: None,
rate_limit: None,
}
}
pub fn endpoint<S: Into<String>>(mut self, url: S) -> Self {
self.endpoint = Some(url.into());
self
}
#[cfg(feature = "array-tuples")]
pub fn auth<F>(mut self, f: F) -> Self
where
F: FnOnce() -> hashbrown::HashMap<&'static str, &'static str>,
{
let auth_map = f();
let mut auth = StdHashMap::new();
for (k, v) in auth_map {
auth.insert(k.to_string(), v.to_string());
}
self.auth = Some(auth);
self
}
#[cfg(feature = "array-tuples")]
pub fn rate_limit<F>(mut self, f: F) -> Self
where
F: FnOnce() -> hashbrown::HashMap<&'static str, &'static str>,
{
let limit_map = f();
let mut rate_limit = StdHashMap::new();
for (k, v) in limit_map {
rate_limit.insert(k.to_string(), v.to_string());
}
self.rate_limit = Some(rate_limit);
self
}
pub fn build(self) -> Result<BuiltApiClient, String> {
let endpoint = self.endpoint.ok_or("endpoint is required")?;
Ok(BuiltApiClient {
endpoint,
auth: self.auth.unwrap_or_default(),
rate_limit: self.rate_limit.unwrap_or_default(),
})
}
}
impl Default for ApiClient {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BuiltApiClient {
pub endpoint: String,
pub auth: StdHashMap<String, String>,
pub rate_limit: StdHashMap<String, String>,
}