Skip to content

Commit

Permalink
Add utility for making unmodeled requests with the orchestrator (smit…
Browse files Browse the repository at this point in the history
…hy-lang#2975)

This PR adds a utility for making unmodeled requests with the
orchestrator, which is a prerequisite for getting aws-config completely
off of the old generic middleware client.

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._
  • Loading branch information
jdisanti authored Sep 12, 2023
1 parent 7bf8837 commit cb79a68
Show file tree
Hide file tree
Showing 5 changed files with 503 additions and 7 deletions.
14 changes: 14 additions & 0 deletions rust-runtime/aws-smithy-runtime-api/src/client/orchestrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,20 @@ impl<E> OrchestratorError<E> {
}
}
}

/// Maps the error type in `ErrorKind::Operation`
#[doc(hidden)]
pub fn map_operation_error<E2>(self, map: impl FnOnce(E) -> E2) -> OrchestratorError<E2> {
let kind = match self.kind {
ErrorKind::Connector { source } => ErrorKind::Connector { source },
ErrorKind::Operation { err } => ErrorKind::Operation { err: map(err) },
ErrorKind::Interceptor { source } => ErrorKind::Interceptor { source },
ErrorKind::Response { source } => ErrorKind::Response { source },
ErrorKind::Timeout { source } => ErrorKind::Timeout { source },
ErrorKind::Other { source } => ErrorKind::Other { source },
};
OrchestratorError { kind }
}
}

fn convert_dispatch_error<O>(
Expand Down
15 changes: 14 additions & 1 deletion rust-runtime/aws-smithy-runtime-api/src/client/runtime_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use std::borrow::Cow;
use std::fmt::Debug;
use std::sync::Arc;

const DEFAULT_ORDER: Order = Order::Overrides;

/// Runtime plugin ordering.
///
/// There are two runtime plugin "levels" that run in the following order:
Expand Down Expand Up @@ -70,7 +72,7 @@ pub trait RuntimePlugin: Debug + Send + Sync {
/// service runtime plugins will run before [`Overrides`](Order::Overrides)
/// service runtime plugins.
fn order(&self) -> Order {
Order::Overrides
DEFAULT_ORDER
}

/// Optionally returns additional config that should be added to the [`ConfigBag`](aws_smithy_types::config_bag::ConfigBag).
Expand Down Expand Up @@ -139,6 +141,7 @@ impl RuntimePlugin for SharedRuntimePlugin {
pub struct StaticRuntimePlugin {
config: Option<FrozenLayer>,
runtime_components: Option<RuntimeComponentsBuilder>,
order: Option<Order>,
}

impl StaticRuntimePlugin {
Expand All @@ -158,9 +161,19 @@ impl StaticRuntimePlugin {
self.runtime_components = Some(runtime_components);
self
}

/// Changes the order of this runtime plugin.
pub fn with_order(mut self, order: Order) -> Self {
self.order = Some(order);
self
}
}

impl RuntimePlugin for StaticRuntimePlugin {
fn order(&self) -> Order {
self.order.unwrap_or(DEFAULT_ORDER)
}

fn config(&self) -> Option<FrozenLayer> {
self.config.clone()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,36 @@
*/

use aws_smithy_http::body::SdkBody;
use aws_smithy_runtime_api::client::connectors::{HttpConnector, HttpConnectorFuture};
use aws_smithy_runtime_api::client::orchestrator::HttpRequest;
use std::fmt::Debug;
use std::sync::{Arc, Mutex};
use tokio::sync::oneshot;

#[derive(Debug)]
struct Inner {
response: Option<http::Response<SdkBody>>,
sender: Option<oneshot::Sender<HttpRequest>>,
}

/// Test Connection to capture a single request
#[derive(Debug, Clone)]
pub struct CaptureRequestHandler(Arc<Mutex<Inner>>);

#[derive(Debug)]
struct Inner {
_response: Option<http::Response<SdkBody>>,
_sender: Option<oneshot::Sender<HttpRequest>>,
impl HttpConnector for CaptureRequestHandler {
fn call(&self, request: HttpRequest) -> HttpConnectorFuture {
let mut inner = self.0.lock().unwrap();
inner
.sender
.take()
.expect("already sent")
.send(request)
.expect("channel not ready");
HttpConnectorFuture::ready(Ok(inner
.response
.take()
.expect("could not handle second request")))
}
}

/// Receiver for [`CaptureRequestHandler`](CaptureRequestHandler)
Expand Down Expand Up @@ -71,13 +88,13 @@ pub fn capture_request(
let (tx, rx) = oneshot::channel();
(
CaptureRequestHandler(Arc::new(Mutex::new(Inner {
_response: Some(response.unwrap_or_else(|| {
response: Some(response.unwrap_or_else(|| {
http::Response::builder()
.status(200)
.body(SdkBody::empty())
.expect("unreachable")
})),
_sender: Some(tx),
sender: Some(tx),
}))),
CaptureRequestReceiver { receiver: rx },
)
Expand Down
3 changes: 3 additions & 0 deletions rust-runtime/aws-smithy-runtime/src/client/orchestrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ mod auth;
pub mod endpoints;
/// Defines types that work with HTTP types
mod http;
/// Utility for making one-off unmodeled requests with the orchestrator.
#[doc(hidden)]
pub mod operation;

macro_rules! halt {
([$ctx:ident] => $err:expr) => {{
Expand Down
Loading

0 comments on commit cb79a68

Please sign in to comment.