bmitch / relogio

Terminal clock in Rust
MIT License
3 stars 2 forks source link

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

Open robbystk opened 5 years ago

robbystk commented 5 years ago

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

robbystk commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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.