use std::net::SocketAddr;
use bytes::Bytes;
use oxihttp_core::OxiHttpError;
use oxiquic_h3::{H3Client as RawH3Client, H3ClientBuilder};
pub use oxiquic_h3::{H3Request, H3Response};
pub struct H3ConnectionBuilder {
server_name: String,
tls_config: Option<rustls::ClientConfig>,
}
impl H3ConnectionBuilder {
#[must_use]
pub fn new(server_name: impl Into<String>) -> Self {
Self {
server_name: server_name.into(),
tls_config: None,
}
}
#[must_use]
pub fn with_tls_config(mut self, cfg: rustls::ClientConfig) -> Self {
self.tls_config = Some(cfg);
self
}
pub async fn connect(self, addr: SocketAddr) -> Result<H3Connection, OxiHttpError> {
let tls_config = self
.tls_config
.ok_or_else(|| OxiHttpError::H3("H3Connection requires a TLS config".into()))?;
let inner = H3ClientBuilder::new()
.with_server_name(&self.server_name)
.with_tls_config(tls_config)
.connect(addr)
.await
.map_err(|e| OxiHttpError::H3(e.to_string()))?;
Ok(H3Connection { inner })
}
}
pub struct H3Connection {
inner: RawH3Client,
}
impl H3Connection {
pub async fn get(&mut self, uri: &str) -> Result<H3Response, OxiHttpError> {
self.inner
.get(uri)
.await
.map_err(|e| OxiHttpError::H3(e.to_string()))
}
pub async fn post(&mut self, uri: &str, body: Bytes) -> Result<H3Response, OxiHttpError> {
self.inner
.post(uri, body)
.await
.map_err(|e| OxiHttpError::H3(e.to_string()))
}
pub async fn head(&mut self, uri: &str) -> Result<H3Response, OxiHttpError> {
self.inner
.head(uri)
.await
.map_err(|e| OxiHttpError::H3(e.to_string()))
}
pub async fn put(&mut self, uri: &str, body: Bytes) -> Result<H3Response, OxiHttpError> {
self.inner
.put(uri, body)
.await
.map_err(|e| OxiHttpError::H3(e.to_string()))
}
pub async fn delete(&mut self, uri: &str) -> Result<H3Response, OxiHttpError> {
self.inner
.delete(uri)
.await
.map_err(|e| OxiHttpError::H3(e.to_string()))
}
pub async fn request(
&mut self,
req: H3Request,
body: Option<Bytes>,
) -> Result<H3Response, OxiHttpError> {
self.inner
.request(req, body)
.await
.map_err(|e| OxiHttpError::H3(e.to_string()))
}
pub async fn close(self) -> Result<(), OxiHttpError> {
self.inner
.close()
.await
.map_err(|e| OxiHttpError::H3(e.to_string()))
}
}