-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement middleware types to allow intercepting client & request han…
…dling (#232) * Implement middleware types to allow intercepting client & request handling Signed-off-by: Thomas Farr <[email protected]> * Add changelog entry Signed-off-by: Thomas Farr <[email protected]> * Relax position of clone bounds Signed-off-by: Thomas Farr <[email protected]> * Move is_send_sync check behind test cfg Signed-off-by: Thomas Farr <[email protected]> * Rename RequestPipeline to RequestHandlerChain Signed-off-by: Thomas Farr <[email protected]> --------- Signed-off-by: Thomas Farr <[email protected]>
- Loading branch information
Showing
13 changed files
with
693 additions
and
7 deletions.
There are no files selected for viewing
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,47 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
use super::InitializerResult; | ||
use crate::BoxError; | ||
use reqwest::ClientBuilder; | ||
|
||
pub trait ClientInitializer: 'static { | ||
type Result: InitializerResult<ClientBuilder>; | ||
|
||
fn init(self, client: ClientBuilder) -> Self::Result; | ||
} | ||
|
||
impl<F, R> ClientInitializer for F | ||
where | ||
F: FnOnce(ClientBuilder) -> R + 'static, | ||
R: InitializerResult<ClientBuilder>, | ||
{ | ||
type Result = R; | ||
|
||
fn init(self, client: ClientBuilder) -> Self::Result { | ||
self(client) | ||
} | ||
} | ||
|
||
pub(crate) trait BoxedClientInitializer { | ||
fn init(self: Box<Self>, client: ClientBuilder) -> Result<ClientBuilder, BoxError<'static>>; | ||
} | ||
|
||
impl<T> BoxedClientInitializer for T | ||
where | ||
T: ClientInitializer + Sized, | ||
{ | ||
fn init(self: Box<Self>, client: ClientBuilder) -> Result<ClientBuilder, BoxError<'static>> { | ||
ClientInitializer::init(*self, client) | ||
.into_result() | ||
.map_err(Into::into) | ||
} | ||
} |
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,44 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
mod client; | ||
mod request; | ||
|
||
use crate::BoxError; | ||
use std::convert::Infallible; | ||
|
||
pub use client::*; | ||
pub use request::*; | ||
|
||
pub trait InitializerResult<T> { | ||
type Error: Into<BoxError<'static>>; | ||
|
||
fn into_result(self) -> Result<T, Self::Error>; | ||
} | ||
|
||
impl<T, E> InitializerResult<T> for Result<T, E> | ||
where | ||
E: Into<BoxError<'static>>, | ||
{ | ||
type Error = E; | ||
|
||
fn into_result(self) -> Result<T, Self::Error> { | ||
self | ||
} | ||
} | ||
|
||
impl<T> InitializerResult<T> for T { | ||
type Error = Infallible; | ||
|
||
fn into_result(self) -> Result<T, Infallible> { | ||
Ok(self) | ||
} | ||
} |
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,86 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
use super::InitializerResult; | ||
use crate::BoxError; | ||
use reqwest::RequestBuilder; | ||
|
||
pub trait RequestInitializer: std::fmt::Debug + Send + Sync + 'static { | ||
type Result: InitializerResult<RequestBuilder>; | ||
|
||
fn init(&self, request: RequestBuilder) -> Self::Result; | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct RequestInitializerFn<F>(F); | ||
|
||
pub fn request_initializer_fn<F>(f: F) -> RequestInitializerFn<F> { | ||
RequestInitializerFn(f) | ||
} | ||
|
||
impl<F> std::fmt::Debug for RequestInitializerFn<F> { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct(stringify!(RequestInitializerFn)).finish() | ||
} | ||
} | ||
|
||
impl<F, R> RequestInitializer for RequestInitializerFn<F> | ||
where | ||
F: Fn(RequestBuilder) -> R + Send + Sync + 'static, | ||
R: InitializerResult<RequestBuilder>, | ||
{ | ||
type Result = R; | ||
|
||
fn init(&self, request: RequestBuilder) -> Self::Result { | ||
self.0(request) | ||
} | ||
} | ||
|
||
impl<R> RequestInitializer for std::sync::Arc<R> | ||
where | ||
R: RequestInitializer, | ||
{ | ||
type Result = R::Result; | ||
|
||
fn init(&self, request: RequestBuilder) -> Self::Result { | ||
self.as_ref().init(request) | ||
} | ||
} | ||
|
||
impl<R> RequestInitializer for std::sync::Arc<dyn RequestInitializer<Result = R>> | ||
where | ||
R: InitializerResult<RequestBuilder> + 'static, | ||
{ | ||
type Result = R; | ||
|
||
fn init(&self, request: RequestBuilder) -> Self::Result { | ||
self.as_ref().init(request) | ||
} | ||
} | ||
|
||
pub(crate) trait BoxedRequestInitializer: | ||
dyn_clone::DynClone + std::fmt::Debug + Send + Sync + 'static | ||
{ | ||
fn init(&self, request: RequestBuilder) -> Result<RequestBuilder, BoxError<'static>>; | ||
} | ||
|
||
impl<T> BoxedRequestInitializer for T | ||
where | ||
T: RequestInitializer + Clone, | ||
{ | ||
fn init(&self, request: RequestBuilder) -> Result<RequestBuilder, BoxError<'static>> { | ||
RequestInitializer::init(self, request) | ||
.into_result() | ||
.map_err(Into::into) | ||
} | ||
} | ||
|
||
dyn_clone::clone_trait_object!(BoxedRequestInitializer); |
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,20 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
mod initializers; | ||
mod request_handler; | ||
|
||
pub use async_trait::async_trait; | ||
pub use initializers::*; | ||
pub use request_handler::*; | ||
|
||
pub(crate) type BoxFuture<'a, T> = | ||
std::pin::Pin<Box<dyn std::future::Future<Output = T> + Send + 'a>>; |
Oops, something went wrong.