-
Notifications
You must be signed in to change notification settings - Fork 23
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 28 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
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,33 @@ | ||
| pub use request::{ConvertRequest, ConvertRequestLayer}; | ||
| pub use response::{ConvertResponse, ConvertResponseLayer}; | ||
|
|
||
| mod request; | ||
| mod response; | ||
|
|
||
| use tower::ServiceBuilder; | ||
| use tower_layer::Stack; | ||
|
|
||
| pub trait Convert<Input> { | ||
|
ninegua marked this conversation as resolved.
|
||
| type Output; | ||
| type Error; | ||
|
|
||
| fn try_convert(&mut self, response: Input) -> Result<Self::Output, Self::Error>; | ||
|
gregorydemay marked this conversation as resolved.
|
||
| } | ||
|
|
||
| pub trait ConvertServiceBuilder<L> { | ||
|
gregorydemay marked this conversation as resolved.
|
||
| fn convert_request<C>(self, f: C) -> ServiceBuilder<Stack<ConvertRequestLayer<C>, L>>; | ||
| 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,55 @@ | ||
| use crate::convert::Convert; | ||
| use futures_util::future; | ||
| use std::task::{Context, Poll}; | ||
| use tower::Service; | ||
| use tower_layer::Layer; | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct ConvertRequestLayer<C> { | ||
| converter: C, | ||
| } | ||
|
|
||
| impl<C> ConvertRequestLayer<C> { | ||
| pub fn new(converter: C) -> Self { | ||
| Self { converter } | ||
| } | ||
| } | ||
|
|
||
| #[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()))), | ||
| } | ||
| } | ||
| } | ||
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,87 @@ | ||
| use crate::convert::Convert; | ||
| use pin_project::pin_project; | ||
| use std::future::Future; | ||
| use std::pin::Pin; | ||
| use std::task::{Context, Poll}; | ||
| use tower::Service; | ||
| use tower_layer::Layer; | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct ConvertResponseLayer<C> { | ||
| converter: C, | ||
| } | ||
|
|
||
| impl<C> ConvertResponseLayer<C> { | ||
| pub fn new(converter: C) -> Self { | ||
| Self { converter } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct ConvertResponse<S, C> { | ||
| inner: S, | ||
| converter: C, | ||
| } | ||
|
|
||
| impl<S, C: Clone> Layer<S> for ConvertResponseLayer<C> { | ||
| type Service = ConvertResponse<S, C>; | ||
|
|
||
| fn layer(&self, inner: S) -> Self::Service { | ||
| Self::Service { | ||
| inner, | ||
| converter: self.converter.clone(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<S, Request, Response, NewResponse, Converter> Service<Request> | ||
| for ConvertResponse<S, Converter> | ||
| where | ||
| S: Service<Request, Response = Response>, | ||
| Converter: Convert<Response, Output = NewResponse> + Clone, | ||
| Converter::Error: Into<S::Error>, | ||
| { | ||
| type Response = NewResponse; | ||
| type Error = S::Error; | ||
| type Future = ResponseFuture<S::Future, Converter>; | ||
|
|
||
| fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
| self.inner.poll_ready(cx) | ||
| } | ||
|
|
||
| fn call(&mut self, req: Request) -> Self::Future { | ||
| ResponseFuture { | ||
| response_future: self.inner.call(req), | ||
| converter: self.converter.clone(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[pin_project] | ||
| pub struct ResponseFuture<F, Converter> { | ||
| #[pin] | ||
| response_future: F, | ||
| converter: Converter, | ||
| } | ||
| impl<F, Filter, Response, NewResponse, Error> Future for ResponseFuture<F, Filter> | ||
| where | ||
| F: Future<Output = Result<Response, Error>>, | ||
| Filter: Convert<Response, Output = NewResponse>, | ||
| Filter::Error: Into<Error>, | ||
| { | ||
| type Output = Result<NewResponse, Error>; | ||
|
|
||
| fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
| let this = self.project(); | ||
| let result_fut = this.response_future.poll(cx); | ||
| match result_fut { | ||
| Poll::Ready(result) => match result { | ||
| Ok(response) => { | ||
| Poll::Ready(this.converter.try_convert(response).map_err(Into::into)) | ||
| } | ||
| Err(err) => Poll::Ready(Err(err)), | ||
| }, | ||
| Poll::Pending => Poll::Pending, | ||
| } | ||
| } | ||
| } |
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,9 @@ | ||
| pub use request::{ | ||
| HttpJsonRpcRequest, JsonRequestConversionError, JsonRequestConverter, JsonRpcRequestBody, | ||
| }; | ||
| pub use response::{ | ||
| HttpJsonRpcResponse, JsonResponseConversionError, JsonResponseConverter, JsonRpcResult, | ||
| }; | ||
|
|
||
| mod request; | ||
| mod response; | ||
|
gregorydemay marked this conversation as resolved.
|
||
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.