-
Notifications
You must be signed in to change notification settings - Fork 318
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
refactor(weather): Fix timeout, reduce calls, apply shellcheck #327
base: master
Are you sure you want to change the base?
refactor(weather): Fix timeout, reduce calls, apply shellcheck #327
Conversation
Fix Darwin timeout function duration argument. Remove unnecessary API calls to “ipinfo.io”. Confirm to Google Shell style guide and appease shellcheck.
One thing that wasn't clear to me was the error checking here. I tried finding an example output of the error "====" but couldn't find any in the commit, PR, or in upstream wttr.in project. It's also unclear to me why the second conditional checks for the string "error" when the first conditional sets it; i.e. we could simply return at finding the first error, unless we actually expect the string "error" to appear in addition to "====". If you can advise on sample error output, I'd be happy to make it more robust and propagate the error message up. Also, please feel free to let me know if these types of refactors are welcomed or not. I'd be happy to continue with any other scripts or understand if you want to focus on fixes/features only. Thank you for the cool project! |
remove forgotten test string
add return when unknown location
update weather_wrapped local vars
add curl --fail flag
Ahh, I found #267, which shows the error message as:
So I suppose the check against "===" is to match this error message. I too would not be confident in exactly which string to match against as I don't know if that string is guaranteed not to change. But "===" is probably not the most obvious method for the reader, nor the most guaranteed string of the message. I also copied the curl commands, added the include flag, and we see the output has changed since reported in #267
I think the appropriate thing to do would be to pivot on the status code, especially since this results in leaking user PI. We can see the unknown error is handled by a 404, and surely the status code makes a better contract. This lead me to also search the upstream project for the capacity text, which lead to the server implementation. Given this is a potential PI leak, I suggest using |
With status code check:
With this change, I believe we can close #267, which was previously patched via #268 , but appears the API has since changed. And I believe #308 was a patch to handle the 503 capacity error message, though I am still confused by the comment that was added. |
update fetch_weather_information argument comments, missing API_URLwq
comment example raw response body
hi @bmodotdev, you're truly doing gods work. I've been planning on refactoring, but never got around to do it, so I'm very happy you're helping us. |
use tr for lowercase due to OSX bash3.2
Thank you! I just ran this on OSX, and got an error regarding locale:
I thought
Looks like this is the only script that uses that value, so I've updated it. I also received an error regarding lowercase parameter expansion. OSX is stuck on Bash3.2 because they refused to accept the change from GPLv2 to GPLv3. I've updated it to use I believe this is PR ready for review now. Edit: Please squash my fixups or I can (not sure if autosquash is enabled or not). |
Issue
No issue submitted yet.
Description of Changes
General Overview
Fix Darwin timeout function duration argument. Remove unnecessary API calls to “ipinfo.io”. Confirm to Google Shell style guide and appease shellcheck.
The Darwin timeout function makes use of variable
duration
which does not exist.One execution of the
./scripts/weather.sh
makes 4 remote API calls, 2 calls to the weather service endpoint wttr.in and 2 calls to the geodata endpoint ipinfo.io to fetch city and region. We should make use of the first weather call for both error checking and data gathering. The calls to the geodata endpoint are not necessary as the weather endpoint provides this functionality. This change results in a single API call each script execution.Exec's of awk, grep, and xargs should not be necessary, as bash parameter expansion allows us to extract data needed.
General style changes to conform to Google Shell style guide, add function argument descriptions, and changes to appease shellcheck. Reduce scope of globals to local function variables.
Shellcheck output
Testing
Tested in Ubuntu as below:
For the wrapper script, I manually adjusted
$INTERVAL
to 5, and added anprintf "updating cache "
to the interval condition, then:I will test on OS X tomorrow at the office.