#![deprecated(since = "0.6.6", note = "The `wstd-aws` crate has been deprecated in favor of upstream support in `aws-smithy-wasm`. See https://github.com/bytecodealliance/wstd/tree/main/aws-example for details on using upstream support.")]
use anyhow::anyhow;
use aws_smithy_async::rt::sleep::{AsyncSleep, Sleep};
use aws_smithy_runtime_api::client::http::{
HttpClient, HttpConnector, HttpConnectorFuture, HttpConnectorSettings, SharedHttpConnector,
};
use aws_smithy_runtime_api::client::orchestrator::HttpRequest;
use aws_smithy_runtime_api::client::result::ConnectorError;
use aws_smithy_runtime_api::client::retries::ErrorKind;
use aws_smithy_runtime_api::client::runtime_components::RuntimeComponents;
use aws_smithy_runtime_api::http::Response;
use aws_smithy_types::body::SdkBody;
use http_body_util::{BodyStream, StreamBody};
use std::time::Duration;
use sync_wrapper::SyncStream;
use wstd::http::{Body as WstdBody, BodyExt, Client};
pub fn sleep_impl() -> impl AsyncSleep + 'static {
WstdSleep
}
#[derive(Debug)]
struct WstdSleep;
impl AsyncSleep for WstdSleep {
fn sleep(&self, duration: Duration) -> Sleep {
Sleep::new(async move {
wstd::task::sleep(wstd::time::Duration::from(duration)).await;
})
}
}
pub fn http_client() -> impl HttpClient + 'static {
WstdHttpClient
}
#[derive(Debug)]
struct WstdHttpClient;
impl HttpClient for WstdHttpClient {
fn http_connector(
&self,
settings: &HttpConnectorSettings,
_components: &RuntimeComponents,
) -> SharedHttpConnector {
let mut client = Client::new();
if let Some(timeout) = settings.connect_timeout() {
client.set_connect_timeout(timeout);
}
if let Some(timeout) = settings.read_timeout() {
client.set_first_byte_timeout(timeout);
}
SharedHttpConnector::new(WstdHttpConnector(client))
}
}
#[derive(Debug)]
struct WstdHttpConnector(Client);
impl HttpConnector for WstdHttpConnector {
fn call(&self, request: HttpRequest) -> HttpConnectorFuture {
let client = self.0.clone();
HttpConnectorFuture::new(async move {
let request = request
.try_into_http1x()
.map_err(|e| ConnectorError::other(Box::new(e), None))?;
let request =
request.map(|body| WstdBody::from_http_body(body.map_err(|e| anyhow!("{e:?}"))));
let response = client
.send(request)
.await
.map_err(|e| ConnectorError::other(e.into(), Some(ErrorKind::ClientError)))?;
Response::try_from(response.map(|wstd_body| {
let nonsync_body = wstd_body
.into_boxed_body()
.map_err(|e| e.into_boxed_dyn_error());
let nonsync_stream = BodyStream::new(nonsync_body);
let sync_stream = SyncStream::new(nonsync_stream);
let sync_body = StreamBody::new(sync_stream);
SdkBody::from_body_1_x(sync_body)
}))
.map_err(|e| ConnectorError::other(Box::new(e), None))
})
}
}