-
Notifications
You must be signed in to change notification settings - Fork 40
bitreq: Re-implement json-using-serde feature
#398
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
Conversation
e45e36b to
722ee7a
Compare
69deb6d to
be01458
Compare
json-using-serde featurejson-using-serde feature
bitreq/src/response.rs
Outdated
| /// ```no_run | ||
| /// use serde_json::Value; | ||
| /// | ||
| /// # fn main() -> Result<(), minreq::Error> { |
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.
| /// # fn main() -> Result<(), minreq::Error> { | |
| /// # fn main() -> Result<(), bitreq::Error> { |
bitreq/src/response.rs
Outdated
| /// # fn main() -> Result<(), minreq::Error> { | ||
| /// # let url_to_json_resource = "http://example.org/resource.json"; | ||
| /// // Value could be any type that implements Deserialize! | ||
| /// let user = minreq::get(url_to_json_resource).send()?.json::<Value>()?; |
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.
| /// let user = minreq::get(url_to_json_resource).send()?.json::<Value>()?; | |
| /// let user = bitreq::get(url_to_json_resource).send()?.json::<Value>()?; |
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.
Why are these not caught in CI? I get an error locally. Ah, is it because of #400 not being merged yet?
bitreq/tests/main.rs
Outdated
| let original_json: serde_json::Value = serde_json::from_str(JSON_SRC).unwrap(); | ||
| let response = bitreq::post(url("/echo")).with_json(&original_json).unwrap().send().unwrap(); | ||
| let actual_json: serde_json::Value = response.json().unwrap(); | ||
| assert_eq!(&actual_json, &original_json); |
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.
| assert_eq!(&actual_json, &original_json); | |
| assert_eq!(actual_json, original_json); |
bitreq/src/response.rs
Outdated
| let str = match self.as_str() { | ||
| Ok(str) => str, | ||
| Err(_) => return Err(Error::InvalidUtf8InResponse), | ||
| }; | ||
| match serde_json::from_str(str) { | ||
| Ok(json) => Ok(json), | ||
| Err(err) => Err(Error::SerdeJsonError(err)), |
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.
| let str = match self.as_str() { | |
| Ok(str) => str, | |
| Err(_) => return Err(Error::InvalidUtf8InResponse), | |
| }; | |
| match serde_json::from_str(str) { | |
| Ok(json) => Ok(json), | |
| Err(err) => Err(Error::SerdeJsonError(err)), | |
| serde_json::from_str(self.as_str()?) | |
| .map_err(Error::SerdeJsonError) |
The existing code returns the incorrect error variant. The docs say InvalidUtf8InBody which is what as_str() returns.
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.
Half right I think. Your suggestion is correct and better but AFAICT it doesn't change the logic. I used the suggestion.
This feature was removed when we gutted `minreq` after forking it. Turns out we need it to be able to use `bitreq` in `jsonrpc`. Re-implement the feature by copying code from `minreq`. After copy, fix docs to mention `bitreq` and also refactor some error logic to use `as_str()?` directly (no logic change).
be01458 to
99c948c
Compare
oleonardolima
left a comment
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.
tACK 99c948c
I've tested it locally, and with the rust-esplora-client PR (see bitcoindevkit/rust-esplora-client#137), and it works just fine.
jamillambert
left a comment
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.
ACK 99c948c
99c948c Re-implement json-using-serde feature (Tobin C. Harding) 1251953 Replace stale docs reference to minreq (Tobin C. Harding) Pull request description: This feature was removed when we gutted `minreq` after forking it. Turns out we need it to be able to use `bitreq` in `jsonrpc`. Re-implement the feature by copying code from `minreq`. Close: rust-bitcoin#393 ACKs for top commit: oleonardolima: tACK 99c948c jamillambert: ACK 99c948c Tree-SHA512: 2e22881566fd3d75e0edb70567dac0b1ed9ce66720511dcead888b2b7686fc89fb1f6bce3bd490f91997e9ade88b0665b00679c79bcef35205c6ce1117306faf
This feature was removed when we gutted
minreqafter forking it. Turns out we need it to be able to usebitreqinjsonrpc.Re-implement the feature by copying code from
minreq.Close: #393