-
Notifications
You must be signed in to change notification settings - Fork 22
feat: JSON RPC conversion layer #375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
72901b9
feat: serialize json request body
gregorydemay e989118
refactor: move to inside http
gregorydemay ee9b6bc
refactor: json feature activate http
gregorydemay 5749ade
refactor: use client with JSON RPC request
gregorydemay a81555b
refactor: use client with JSON RPC response
gregorydemay 1f60ae6
fix: tests (start)
gregorydemay 3e3b396
clippy, formatting
gregorydemay 1fecc50
filter response
gregorydemay ff500a1
Use HTTP conversion as filter response
gregorydemay fce09ef
rename filter to convert
gregorydemay 839b9c4
convert request service
gregorydemay c46b457
rename generic parameters
gregorydemay 0430d63
use converter for JSON RPC request
gregorydemay 74f2f88
use converter for JSON RPC response
gregorydemay d63226d
use converter for HTTP request
gregorydemay dded3a4
use converter for HTTP response
gregorydemay 3fef789
clean-up canhttp
gregorydemay c54ee98
use explicit error
gregorydemay afb732e
remove filter feature
gregorydemay 911a4f1
Filter HTTP response
gregorydemay 10d5676
Use response filter
gregorydemay ea3b788
fix json comparison
gregorydemay 258ddbe
simplify call
gregorydemay 955170f
added TODOs
gregorydemay 3ba8602
fix docs
gregorydemay 34ab9df
lint
gregorydemay c75550e
split convert module between request/response
gregorydemay bb8b111
Merge branch 'main' into gdemay/XC-287-json-rpc-layer
gregorydemay c608a04
doc for convert module
gregorydemay 1cd3403
doc for http module
gregorydemay 708c9fc
make JsonRequestConverter generic
gregorydemay 98fdb2e
docs for JSON module
gregorydemay 5bac12c
docs for client
gregorydemay 55db8d9
use `all-features` flag
gregorydemay bb68f2c
linting
gregorydemay 1a4e772
unit tests for JSON RPC layer
gregorydemay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,148 @@ | ||
| //! Fallible conversion from one type to another that can be used as a tower middleware. | ||
| //! | ||
| //! # Examples | ||
| //! | ||
| //! ## To convert requests | ||
| //! | ||
| //! A converter can be used to convert request types: | ||
| //! * If the result of the conversion is [`Ok`], the converted type will be forwarded to the inner service. | ||
| //! * If the result of the conversion is [`Err`], the error will be returned and the inner service will *not* be called. | ||
| //! | ||
| //! When used to convert requests (with [`ConvertRequestLayer`], the functionality offered by [`Convert`] is similar to that of | ||
| //! [`Predicate`](https://docs.rs/tower/0.5.2/tower/filter/trait.Predicate.html) in that it can act as a *filter*. The main difference is that the error does not need to be boxed. | ||
| //! | ||
| //! ```rust | ||
| //! use std::convert::Infallible; | ||
| //! use canhttp::convert::{Convert, ConvertServiceBuilder}; | ||
| //! use tower::{ServiceBuilder, Service, ServiceExt}; | ||
| //! | ||
| //! async fn bare_bone_service(request: Vec<u8>) -> Result<Vec<u8>, Infallible> { | ||
| //! Ok(request) | ||
| //! } | ||
| //! | ||
| //! struct UsefulRequest(Vec<u8>); | ||
| //! | ||
| //! #[derive(Clone)] | ||
| //! struct UsefulRequestConverter; | ||
| //! | ||
| //! impl Convert<UsefulRequest> for UsefulRequestConverter { | ||
| //! type Output = Vec<u8>; | ||
| //! type Error = Infallible; | ||
| //! | ||
| //! fn try_convert(&mut self, input: UsefulRequest) -> Result<Self::Output, Self::Error> { | ||
| //! Ok(input.0) | ||
| //! } | ||
| //! } | ||
| //! | ||
| //! # #[tokio::main] | ||
| //! # async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| //! let mut service = ServiceBuilder::new() | ||
| //! .convert_request(UsefulRequestConverter) | ||
| //! .service_fn(bare_bone_service); | ||
| //! | ||
| //! let request = UsefulRequest(vec![42]); | ||
| //! | ||
| //! let response = service | ||
| //! .ready() | ||
| //! .await? | ||
| //! .call(request) | ||
| //! .await?; | ||
| //! | ||
| //! assert_eq!(response, vec![42_u8]); | ||
| //! # Ok(()) | ||
| //! # } | ||
| //! ``` | ||
| //! | ||
| //! ## To convert responses | ||
| //! | ||
| //! A converter can be used to convert response types: | ||
| //! ```rust | ||
| //! use std::convert::Infallible; | ||
| //! use canhttp::convert::{Convert, ConvertServiceBuilder}; | ||
| //! use tower::{ServiceBuilder, Service, ServiceExt}; | ||
| //! | ||
| //! async fn bare_bone_service(request: Vec<u8>) -> Result<Vec<u8>, Infallible> { | ||
| //! Ok(request) | ||
| //! } | ||
| //! | ||
| //! #[derive(Debug, PartialEq)] | ||
| //! struct UsefulResponse(Vec<u8>); | ||
| //! | ||
| //! #[derive(Clone)] | ||
| //! struct UsefulResponseConverter; | ||
| //! | ||
| //! impl Convert<Vec<u8>> for UsefulResponseConverter { | ||
| //! type Output = UsefulResponse; | ||
| //! type Error = Infallible; | ||
| //! | ||
| //! fn try_convert(&mut self, input: Vec<u8>) -> Result<Self::Output, Self::Error> { | ||
| //! Ok(UsefulResponse(input)) | ||
| //! } | ||
| //! } | ||
| //! | ||
| //! # #[tokio::main] | ||
| //! # async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| //! let mut service = ServiceBuilder::new() | ||
| //! .convert_response(UsefulResponseConverter) | ||
| //! .service_fn(bare_bone_service); | ||
| //! | ||
| //! let request = vec![42]; | ||
| //! | ||
| //! let response = service | ||
| //! .ready() | ||
| //! .await? | ||
| //! .call(request) | ||
| //! .await?; | ||
| //! | ||
| //! assert_eq!(response, UsefulResponse(vec![42_u8])); | ||
| //! # Ok(()) | ||
| //! # } | ||
| //! ``` | ||
|
|
||
| pub use request::{ConvertRequest, ConvertRequestLayer}; | ||
| pub use response::{ConvertResponse, ConvertResponseLayer}; | ||
|
|
||
| mod request; | ||
| mod response; | ||
|
|
||
| use tower::ServiceBuilder; | ||
| use tower_layer::Stack; | ||
|
|
||
| /// Fallible conversion from one type to another. | ||
| pub trait Convert<Input> { | ||
ninegua marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// Converted type if the conversion succeeds. | ||
| type Output; | ||
| /// Error type if the conversion fails | ||
| type Error; | ||
|
|
||
| /// Try to convert an instance of the input type to the output type. | ||
| /// The conversion may fail, in which case an error is returned. | ||
| fn try_convert(&mut self, response: Input) -> Result<Self::Output, Self::Error>; | ||
gregorydemay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// Extension trait that adds methods to [`tower::ServiceBuilder`] for adding middleware | ||
| /// based on fallible conversion between types. | ||
| pub trait ConvertServiceBuilder<L> { | ||
gregorydemay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// Convert the request type. | ||
| /// | ||
| /// See the [module docs](crate::convert) for examples. | ||
| fn convert_request<C>(self, f: C) -> ServiceBuilder<Stack<ConvertRequestLayer<C>, L>>; | ||
|
|
||
| /// Convert the response type. | ||
| /// | ||
| /// See the [module docs](crate::convert) for examples. | ||
| fn convert_response<C>(self, f: C) -> ServiceBuilder<Stack<ConvertResponseLayer<C>, L>>; | ||
| } | ||
|
|
||
| impl<L> ConvertServiceBuilder<L> for ServiceBuilder<L> { | ||
| fn convert_request<C>(self, converter: C) -> ServiceBuilder<Stack<ConvertRequestLayer<C>, L>> { | ||
| self.layer(ConvertRequestLayer::new(converter)) | ||
| } | ||
|
|
||
| fn convert_response<C>( | ||
| self, | ||
| converter: C, | ||
| ) -> ServiceBuilder<Stack<ConvertResponseLayer<C>, L>> { | ||
| self.layer(ConvertResponseLayer::new(converter)) | ||
| } | ||
| } | ||
This file contains hidden or 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,63 @@ | ||
| use crate::convert::Convert; | ||
| use futures_util::future; | ||
| use std::task::{Context, Poll}; | ||
| use tower::Service; | ||
| use tower_layer::Layer; | ||
|
|
||
| /// Convert request of a service into another type, where the conversion may fail. | ||
| /// | ||
| /// This [`Layer`] produces instances of the [`ConvertRequest`] service. | ||
| /// | ||
| /// [`Layer`]: tower::Layer | ||
| #[derive(Debug, Clone)] | ||
| pub struct ConvertRequestLayer<C> { | ||
| converter: C, | ||
| } | ||
|
|
||
| impl<C> ConvertRequestLayer<C> { | ||
| /// Returns a new [`ConvertRequestLayer`] | ||
| pub fn new(converter: C) -> Self { | ||
| Self { converter } | ||
| } | ||
| } | ||
|
|
||
| /// Convert requests into another type and forward the converted type to the inner service | ||
| /// *only if* the conversion was successful. | ||
| #[derive(Debug, Clone)] | ||
| pub struct ConvertRequest<S, C> { | ||
| inner: S, | ||
| converter: C, | ||
| } | ||
|
|
||
| impl<S, C: Clone> Layer<S> for ConvertRequestLayer<C> { | ||
| type Service = ConvertRequest<S, C>; | ||
|
|
||
| fn layer(&self, inner: S) -> Self::Service { | ||
| Self::Service { | ||
| inner, | ||
| converter: self.converter.clone(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<S, Converter, Request, NewRequest, Error> Service<NewRequest> for ConvertRequest<S, Converter> | ||
| where | ||
| Converter: Convert<NewRequest, Output = Request>, | ||
| S: Service<Request, Error = Error>, | ||
| Converter::Error: Into<Error>, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We cannot use |
||
| { | ||
| type Response = S::Response; | ||
| type Error = S::Error; | ||
| type Future = future::Either<S::Future, future::Ready<Result<S::Response, S::Error>>>; | ||
|
|
||
| fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
| self.inner.poll_ready(cx) | ||
| } | ||
|
|
||
| fn call(&mut self, new_req: NewRequest) -> Self::Future { | ||
| match self.converter.try_convert(new_req) { | ||
| Ok(request) => future::Either::Left(self.inner.call(request)), | ||
| Err(err) => future::Either::Right(future::ready(Err(err.into()))), | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is that the service uses the
CustomErrortype, so that any additional layer that may produce errors need to be mapped to the service error type. This forces the user of the library to handle errors explicitly.