mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
feat: initial http and eco support
This commit is contained in:
parent
bd3727d7fe
commit
285bd7fe6e
8 changed files with 263 additions and 0 deletions
37
crates/lib/src/http.rs
Normal file
37
crates/lib/src/http.rs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
use crate::GDErrorKind::{PacketSend, ProtocolFormat, SocketConnect};
|
||||
use crate::{GDResult, TimeoutSettings};
|
||||
use reqwest::blocking::*;
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::net::SocketAddr;
|
||||
|
||||
pub struct HttpClient {
|
||||
client: Client,
|
||||
address: String,
|
||||
}
|
||||
|
||||
impl HttpClient {
|
||||
pub fn new(address: &SocketAddr, timeout_settings: &Option<TimeoutSettings>) -> GDResult<Self>
|
||||
where Self: Sized {
|
||||
let client = Client::builder()
|
||||
.connect_timeout(TimeoutSettings::get_connect_or_default(timeout_settings))
|
||||
.timeout(TimeoutSettings::get_connect_or_default(timeout_settings))
|
||||
.build()
|
||||
.map_err(|e| SocketConnect.context(e))?;
|
||||
|
||||
Ok(Self {
|
||||
client,
|
||||
address: format!("http://{}:{}", address.ip(), address.port()),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn concat_path(&self, path: &str) -> String { format!("{}{}", self.address, path) }
|
||||
|
||||
pub fn request<T: DeserializeOwned>(&mut self, path: &str) -> GDResult<T> {
|
||||
self.client
|
||||
.get(self.concat_path(path))
|
||||
.send()
|
||||
.map_err(|e| PacketSend.context(e))?
|
||||
.json::<T>()
|
||||
.map_err(|e| ProtocolFormat.context(e))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue