-
Notifications
You must be signed in to change notification settings - Fork 15
Generic convert_response for API reponse handling #1292
Conversation
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.
which is used in place of all the
Is that the plan as what is supposed be in this PR already? Because we're only changing one method here.
It wasn't exactly the plan for this PR but I do agree it would make sense to include here or soon after. It will probably be a more involved change and whether or not its done here depends on your suggestion. |
The idea in this PR seems to be fine but it's hard to tell how useful the abstraction is when only convert one use. If you think it will fit then I'm fine with it. |
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.
Seems fine to me. Although I would
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.
I don't think the PR implements my suggestion linked in the description.
Specifically:
- In general, for
Result<T, E>
, weT: Serialize
because we convert it to JSON, so I don't really understand why we need the trait boundT: WarpReplyConverting
- instead I suggest we should usewarp::reply::json(ok_value)
. - The suggestion was about using the
E: WarpReplyConverting
for converting errors into warp replies. For this, we already have a few error types that implement this (PartialValidationError
,ValidationError
,FeeError
). Furthermore, you could easilyimpl WarpReplyConverting for anyhow::Error
where it returns a 500 error.
In the end, if we had any result where the Ok
type implements Serialize
(i.e. can be converted into JSON), then the convert_response
would just convert into JSON for us, and then the Error
type would just need to implement WarpReplyConverting
and the convert_response
could be used everywhere.
In general, my suggestion was aiming at deduplicate methods like:
pub fn response<T: Serialize>(result: Result<T, OrderQuoteError>) -> impl Reply {
match result {
Ok(response) => reply::with_status(reply::json(&response), StatusCode::OK),
Err(err) => {
let (reply, status) = err.convert_to_reply();
reply::with_status(reply, status)
}
}
}
Where instead of using OrderQuoteError
, you make it generic on the error type E where E: WarpReplyConverting
.
edit: Some other methods that could be de-deduplicated by making this generic on the error type:
orderbook::api::get_user_orders::response
orderbook::api::get_trades::get_trades_response
orderbook::api::get_solvable_orders::get_solvable_orders_response
orderbook::api::get_solvable_orders_v2::get_solvable_orders_response
orderbook::api::get_orders::get_orders_response
orderbook::api::get_orders_by_tx::get_orders_by_tx_response
- Maybe more.
Thanks @nlordell for clarifying here. I will attempt to implement your suggested change, but I would point out that in several cases we are actually converting the UPDATE: I would argue that both <T, E> in this context should be In each of the examples to follow, we aim to demonstrate why gp-v2-services/orderbook/src/api/get_solvable_orders_v2.rs Lines 11 to 22 in 863f899
gp-v2-services/orderbook/src/api/get_order_by_uid.rs Lines 12 to 26 in 863f899
gp-v2-services/orderbook/src/api/get_solvable_orders.rs Lines 11 to 19 in 863f899
gp-v2-services/orderbook/src/api/create_order.rs Lines 17 to 32 in 863f899
gp-v2-services/orderbook/src/api/cancel_order.rs Lines 32 to 69 in 863f899
gp-v2-services/orderbook/src/api/get_fee_info.rs Lines 37 to 53 in 863f899
gp-v2-services/orderbook/src/api/get_markets.rs Lines 69 to 86 in 863f899
The only methods that fit naturally into the generic structure you propose are
So I am seeing 7/10 that don't fit the description. Anyway, I will continue attempting to fit this together. |
Codecov Report
@@ Coverage Diff @@
## main #1292 +/- ##
==========================================
- Coverage 67.73% 67.65% -0.09%
==========================================
Files 138 138
Lines 28936 28853 -83
==========================================
- Hits 19601 19521 -80
+ Misses 9335 9332 -3 |
I have now implemented a combination of both possibilities with |
Hmm, I'm not sure I agree. In all of those examples, I see a: Ok(reply::with_status(reply::json(...), StatusCode::OK)) Just to pick a single example: gp-v2-services/orderbook/src/api/get_markets.rs Lines 69 to 86 in 863f899
With the proposed convert_response(result.map(|estimate| AmountEstimateResult {
amount: estimate.out_amount,
token: query.market.quote_token,
})) I only see two that are problematic and those are |
Nice, I see that this little thingy convert_response(result.map(|estimate| AmountEstimateResult {
amount: estimate.out_amount,
token: query.market.quote_token,
})) is the key to adapting this in all places. thanks. However, in this place we see that the patter can also not be generalized since we are matching an option here gp-v2-services/orderbook/src/api/get_order_by_uid.rs Lines 12 to 26 in 863f899
|
Based on the suggestion here
We write a generic method
convert_response
which is used in place of all the*_response(result: Result<XXX>)
methods where appropriate.This method expects the
Err( . )
route to implementWarpReplyConverting
. Additional work will have to go into generically handling theOk( . )
route - which is still lingering around for some reply conversions and will be addressed in a follow up PR.Test Plan
No logical changes - existing CI.