use bytes::Bytes;
use tokio::sync::mpsc;
use tokio::sync::oneshot;
use crate::error::{Error, Result};
use crate::headers::Headers;
use crate::response::Response;
use crate::transport::h3::driver::DriverCommand;
#[derive(Clone)]
pub struct H3Handle {
command_tx: mpsc::Sender<DriverCommand>,
}
impl H3Handle {
pub fn new(command_tx: mpsc::Sender<DriverCommand>) -> Self {
Self { command_tx }
}
pub async fn send_request(
&self,
method: http::Method,
uri: &http::Uri,
headers: Vec<(String, String)>,
body: Option<Bytes>,
) -> Result<Response> {
let (response_tx, response_rx) = oneshot::channel();
let command = DriverCommand::SendRequest {
method,
uri: uri.clone(),
headers,
body,
response_tx,
};
self.command_tx
.send(command)
.await
.map_err(|_| Error::HttpProtocol("H3 Driver channel closed".into()))?;
let stream_response = response_rx
.await
.map_err(|_| Error::HttpProtocol("H3 Response channel closed".into()))??;
Ok(Response::new(
stream_response.status,
Headers::from(stream_response.headers),
stream_response.body,
"HTTP/3".to_string(),
))
}
}