1 use crate::p3::WasiHttpView;
2 use crate::p3::bindings::Proxy;
3 use crate::p3::bindings::http::types::{ErrorCode, Request, Response};
4 use anyhow::Context as _;
5 use wasmtime::component::{Accessor, TaskExit};
6 
7 impl Proxy {
8     /// Call `wasi:http/handler#handle` on [Proxy] getting a [Response] back.
9     pub async fn handle(
10         &self,
11         store: &Accessor<impl WasiHttpView>,
12         req: impl Into<Request>,
13     ) -> wasmtime::Result<Result<(Response, TaskExit), ErrorCode>> {
14         let req = store.with(|mut store| {
15             store
16                 .data_mut()
17                 .http()
18                 .table
19                 .push(req.into())
20                 .context("failed to push request to table")
21         })?;
22         match self.wasi_http_handler().call_handle(store, req).await? {
23             (Ok(res), task) => {
24                 let res = store.with(|mut store| {
25                     store
26                         .data_mut()
27                         .http()
28                         .table
29                         .delete(res)
30                         .context("failed to delete response from table")
31                 })?;
32                 Ok(Ok((res, task)))
33             }
34             (Err(err), _) => Ok(Err(err)),
35         }
36     }
37 }
38