Skip to content

Moon data from US Naval Observatory with Reqwest and Serde_json#22

Open
robbystk wants to merge 13 commits into
bmitch:masterfrom
robbystk:moon
Open

Moon data from US Naval Observatory with Reqwest and Serde_json#22
robbystk wants to merge 13 commits into
bmitch:masterfrom
robbystk:moon

Conversation

@robbystk

@robbystk robbystk commented Sep 7, 2019

Copy link
Copy Markdown

Here is a function that fetches data on the moon phase from USNO and returns the raw json.

@robbystk robbystk mentioned this pull request Sep 7, 2019
Comment thread src/time_calculations.rs Outdated
Reqwest includes the `json()` method on a response which uses Serde's
`DeserializeOwned` trait on whatever type you want to deserialize to.
So we make up some structs and an enum to hold the data from the json.
The enum is problematic because the moon phases in the json have spaces
in them, and enum variants are not allowed to contain spaces.  Manually
implementing `Deserialize` for the enum seems to be the most-elegant way
around that.
@robbystk robbystk changed the title WIP: Moon data from US Naval Observatory with Reqwest WIP: Moon data from US Naval Observatory with Reqwest and Serde Sep 8, 2019
@robbystk

robbystk commented Sep 9, 2019

Copy link
Copy Markdown
Author

I realized there is an easier, more-elegant way to process the json data. Instead of making custom data structures to hold all the data, we can parse the json with serde_json::from_str(), then just pick out what we need. This will also remove serde as a dependency.

Therefore, I will close this PR for now, and re-open it when the alternate solution is complete.

@robbystk robbystk closed this Sep 9, 2019
@robbystk

Copy link
Copy Markdown
Author

I've streamlined the json parsing and removed a lot of extraneous stuff. This still has a test that hits USNO's API but it's ignored. A solution to that would be to use the reqwest_mock crate, but maybe that should be another PR?

@robbystk robbystk reopened this Sep 30, 2019
@robbystk robbystk changed the title WIP: Moon data from US Naval Observatory with Reqwest and Serde Moon data from US Naval Observatory with Reqwest and serde_json Sep 30, 2019
@robbystk robbystk changed the title Moon data from US Naval Observatory with Reqwest and serde_json Moon data from US Naval Observatory with Reqwest and Serde_json Sep 30, 2019
@robbystk

Copy link
Copy Markdown
Author

The test suite now uses a stub to avoid hitting USNO's API. There is still an ignored test that does hit it, which you may remove if you wish.

I just realized that there's another way to do this. The way I did it was to create a MyClient struct that simply wraps a client whether it's real or a stub. It then has a method called get_usno_json that uses the encapsulated client to perform the request. Another way to do it would be make the get_usno_json function accept a reqwest_mock::GenericClient as an argument, then a real or stub client can be passed in as appropriate. I think the latter implementation might be more-readable than having to declare and impl a mostly-empty struct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants