-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added http-client capability and a reqwest implementation (#285)
* added http-outbound capability Signed-off-by: Jiaxiao Zhou <[email protected]>
- Loading branch information
Showing
31 changed files
with
608 additions
and
131 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "slight-http-client" | ||
version = "0.1.0" | ||
edition = { workspace = true } | ||
authors = { workspace = true } | ||
license = { workspace = true } | ||
repository = { workspace = true } | ||
|
||
[lib] | ||
doctest = false | ||
|
||
[dependencies] | ||
anyhow = { workspace = true } | ||
async-trait = { workspace = true } | ||
wit-bindgen-wasmtime = { workspace = true } | ||
wit-error-rs = { workspace = true } | ||
slight-common = { path = "../common" } | ||
reqwest = { version = "0.11" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use reqwest::Client; | ||
|
||
use http_client::*; | ||
use slight_common::impl_resource; | ||
wit_bindgen_wasmtime::export!({paths: ["../../wit/http-client.wit"], async: *}); | ||
wit_error_rs::impl_error!(http_client::HttpError); | ||
wit_error_rs::impl_from!(anyhow::Error, http_client::HttpError::UnexpectedError); | ||
|
||
#[derive(Clone, Default)] | ||
pub struct HttpClient { | ||
client: Client, | ||
} | ||
|
||
impl HttpClient { | ||
pub fn new() -> Self { | ||
Self { | ||
client: Client::new(), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl http_client::HttpClient for HttpClient { | ||
async fn request(&mut self, req: Request<'_>) -> Result<Response, HttpError> { | ||
let mut builder = self.client.request(req.method.into(), req.uri); | ||
for header in req.headers { | ||
builder = builder.header(header.0, header.1); | ||
} | ||
let res = builder.send().await?; | ||
|
||
let status = res.status().as_u16(); | ||
let mut headers = vec![]; | ||
for (name, value) in res.headers().iter() { | ||
headers.push(( | ||
name.as_str().to_string(), | ||
value.to_str().unwrap().to_string(), | ||
)); | ||
} | ||
let body = Some(res.bytes().await?.to_vec()); | ||
Ok(Response { | ||
status, | ||
headers: Some(headers), | ||
body, | ||
}) | ||
} | ||
} | ||
|
||
impl_resource!( | ||
HttpClient, | ||
http_client::add_to_linker, | ||
"http-client".to_string() | ||
); | ||
|
||
impl From<http_client::Method> for reqwest::Method { | ||
fn from(method: http_client::Method) -> Self { | ||
match method { | ||
http_client::Method::Get => reqwest::Method::GET, | ||
http_client::Method::Post => reqwest::Method::POST, | ||
http_client::Method::Put => reqwest::Method::PUT, | ||
http_client::Method::Delete => reqwest::Method::DELETE, | ||
http_client::Method::Head => reqwest::Method::HEAD, | ||
http_client::Method::Options => reqwest::Method::OPTIONS, | ||
http_client::Method::Patch => reqwest::Method::PATCH, | ||
} | ||
} | ||
} | ||
|
||
impl From<reqwest::Error> for http_client::HttpError { | ||
fn from(e: reqwest::Error) -> Self { | ||
Self::UnexpectedError(e.to_string()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.