Well we got an http client builder lol

This commit is contained in:
Daniel Flanagan 2024-08-05 16:57:32 -05:00
parent a9e8f2e9cf
commit 851c68267e
4 changed files with 22 additions and 18 deletions

View file

@ -3,7 +3,7 @@
// TODO: handle messages // TODO: handle messages
// TODO: data persistence? (sqlite? sled?) // TODO: data persistence? (sqlite? sled?)
use std::{any::Any, fmt, future}; use std::{any::Any, future};
use discord::model::{ChannelId, Event, Message, ReadyEvent}; use discord::model::{ChannelId, Event, Message, ReadyEvent};
use tokio::sync::Mutex; use tokio::sync::Mutex;

View file

@ -30,10 +30,6 @@ pub struct Config {
pub open_ai: Option<OpenAI>, pub open_ai: Option<OpenAI>,
} }
pub struct ConfigLoadResult {
pub config: Config,
}
const CONFIG_FILE_PATH: &str = "./conf.toml"; const CONFIG_FILE_PATH: &str = "./conf.toml";
const ENCRYPTED_CONFIG_FILE_PATH: &str = "./conf.toml.sops"; const ENCRYPTED_CONFIG_FILE_PATH: &str = "./conf.toml.sops";
const TMP_CONFIG_FILE_NAME: &str = "yourcloud-conf.toml"; const TMP_CONFIG_FILE_NAME: &str = "yourcloud-conf.toml";

View file

@ -1,5 +1,7 @@
#![allow(dead_code)]
use crate::prelude::*; use crate::prelude::*;
use http_client::{Client, HeaderMap, HeaderValue, Url}; use http_client::{Client, Url};
use serde_with::skip_serializing_none; use serde_with::skip_serializing_none;
use std::collections::HashMap; use std::collections::HashMap;
@ -92,12 +94,7 @@ pub struct ServerStatus {
impl MinecraftServerStatus { impl MinecraftServerStatus {
pub fn try_new() -> Result<Self> { pub fn try_new() -> Result<Self> {
let mut headers = HeaderMap::new(); let client = Client::builder()
headers.insert("content-type", HeaderValue::from_str("application/json")?);
headers.insert("accepts", HeaderValue::from_str("application/json")?);
let client = Client::named("http_client@api.mcsrvstat.us")
.with_default_headers(headers)
.with_url(Url::parse("https://api.mcsrvstat.us/3")?) .with_url(Url::parse("https://api.mcsrvstat.us/3")?)
.build()?; .build()?;

View file

@ -76,6 +76,10 @@ impl Builder {
builder = builder.default_headers(headers); builder = builder.default_headers(headers);
} }
if let Some(ref mut base_url) = self.base_url {
base_url.set_path(&format!("{}{}", base_url.path().trim_end_matches('/'), "/"));
}
let mut middleware_builder = reqwest_middleware::ClientBuilder::new(builder.build()?); let mut middleware_builder = reqwest_middleware::ClientBuilder::new(builder.build()?);
if let Some(middleware) = self.middleware { if let Some(middleware) = self.middleware {
@ -84,17 +88,18 @@ impl Builder {
} }
} }
if let Some(otel_name) = self.name { if let Some(otel_name) = self.name.or_else(|| {
self.base_url
.as_ref()
.map(|u| u.host_str().map(|h| format!("http_client@{}", h)))
.flatten()
}) {
middleware_builder = middleware_builder =
middleware_builder.with_init(Extension(OtelName(otel_name.into()))); middleware_builder.with_init(Extension(OtelName(otel_name.into())));
} }
let client = middleware_builder.build(); let client = middleware_builder.build();
if let Some(ref mut base_url) = self.base_url {
base_url.set_path(&format!("{}{}", base_url.path().trim_end_matches('/'), "/"));
}
Ok(Client { Ok(Client {
client, client,
base_url: self.base_url, base_url: self.base_url,
@ -184,7 +189,13 @@ impl Client {
#[instrument] #[instrument]
pub async fn get_json<T: DeserializeOwned>(&self, rel_url_path: &str) -> Result<T> { pub async fn get_json<T: DeserializeOwned>(&self, rel_url_path: &str) -> Result<T> {
Ok(self.get(rel_url_path).await?.json().await?) Ok(self
.build_request(Method::GET, rel_url_path)?
.header("accepts", "application/json")
.send()
.await?
.json()
.await?)
} }
// this is a lib, so add new stuff sparingly! // this is a lib, so add new stuff sparingly!