use http_body_util::{BodyExt, Full};
use hyper::{
body::{Body, Bytes, Incoming},
client::conn::http1::{self, SendRequest},
Request, Response,
};
use hyper_util::rt::TokioIo;
use std::ops::{Deref, DerefMut};
use tokio::net::TcpStream;
pub struct LambdaRuntimeApiClient<ReqBody>(SendRequest<ReqBody>);
impl<ReqBody> Deref for LambdaRuntimeApiClient<ReqBody> {
type Target = SendRequest<ReqBody>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<ReqBody> DerefMut for LambdaRuntimeApiClient<ReqBody> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<ReqBody: Body + Send + 'static> LambdaRuntimeApiClient<ReqBody> {
pub async fn new() -> Self
where
ReqBody::Data: Send,
ReqBody::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
{
let address =
std::env::var("AWS_LAMBDA_RUNTIME_API").expect("Missing AWS_LAMBDA_RUNTIME_API env var");
let stream = TcpStream::connect(address)
.await
.expect("Failed to connect to runtime API");
let io = TokioIo::new(stream);
let (sender, conn) = http1::handshake(io)
.await
.expect("Failed to handshake with runtime API");
tokio::task::spawn(async move {
if let Err(err) = conn.await {
println!("Connection failed: {:?}", err);
}
});
Self(sender)
}
}
impl LambdaRuntimeApiClient<Incoming> {
pub async fn forward(req: Request<Incoming>) -> hyper::Result<Response<Full<Bytes>>> {
let res = LambdaRuntimeApiClient::new()
.await
.send_request(req)
.await
.unwrap();
let (parts, body) = res.into_parts();
let bytes = body.collect().await.unwrap().to_bytes();
Ok(Response::from_parts(parts, Full::new(bytes)))
}
}