use std::fmt;
use std::sync::Arc;
use reqwest::{Client as RwClient, Method};
use crate::lang::Language;
use crate::{Builder, Config, Result};
#[must_use]
#[derive(Clone)]
pub struct Client {
pub(crate) config: Arc<Config>,
pub lang: Language,
}
impl Client {
#[inline]
pub fn new() -> Self {
Builder::new().build()
}
#[inline]
pub const fn builder() -> Builder {
Builder::new()
}
#[inline]
#[must_use]
pub fn api_key(&self) -> Option<&str> {
self.config.api_key.as_deref()
}
#[inline]
#[must_use]
pub fn user_agent(&self) -> &str {
self.config.user_agent.as_str()
}
#[inline]
#[must_use]
pub fn base_url(&self) -> &str {
self.config.base_url.as_str()
}
#[inline]
#[must_use]
pub fn client(&self) -> &RwClient {
&self.config.client
}
pub async fn health(&self) -> Result<bool> {
#[derive(Debug, serde::Deserialize)]
pub struct Health {
pub healthy: bool,
}
let request = self.config.create(Method::GET, "/v1/health/");
let response = self.config.send(request).await?;
let content = response.json::<Health>().await?;
Ok(content.healthy)
}
}
impl Default for Client {
#[inline]
fn default() -> Self {
Self::builder().build()
}
}
impl fmt::Debug for Client {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.config, f)
}
}
#[cfg(test)]
mod test {
use crate::{Client, Result};
#[test]
fn build() -> Result<()> {
let _ = Client::new();
let _ = Client::builder().build();
let _ = Client::default();
Ok(())
}
#[tokio::test]
async fn health() -> Result<()> {
let glide = Client::default();
let healthy = glide.health().await?;
assert!(healthy);
Ok(())
}
}