xofbd / wqu-weather-app

A demo web application for WorldQuant University Introduction to Data Science Module
GNU General Public License v3.0
7 stars 11 forks source link

Currently used location API not reliable #5

Closed Tim-Abwao closed 4 years ago

Tim-Abwao commented 4 years ago

https://ipapi.co/{ip_address}/json sometimes returns null for longitude and latitude. e.g.

    "postal": null,
    "latitude": null,
    "longitude": null,
    "timezone": null,
    "utc_offset": null,

This then causes problems because the weather API requires longitude and latitude values. With the 'null' location values, the weather API returns

400 Bad Request The 'lat' parameter ("null") to Metno::WeatherAPI::Controller::Product::try {...} did not pass regex check

which causes an Internal Server Error in the app, with error message

json.decoder.JSONDecodeError: Extra data: line 1 column 5 (char 4)

A possible alternative location API is http://ip-api.com/

xofbd commented 4 years ago

Thanks for raising this issue. One solution is to keep our current service but properly handle a situation where no location data was found.

Can we reproduce the error? Do you have any IP address ranges where this fails?

Tim-Abwao commented 4 years ago

https://ipapi.co/197.156.137.180/json

gammaG2 commented 4 years ago

@Tim-Abwao thanks for the use-case relevant to this error.

I believe its better to fetch the lat long from the city-country name pair using geopy IF they are null in the first place.a try except block in the get_location function should do the trick!

Tim-Abwao commented 4 years ago

@gammaG2 I've tried an alternative geo-location API (in #10) which seems reliable so far with latitude and longitude data. It was simple to just switch the URL and a few lines of code.

gammaG2 commented 4 years ago

Seems straight forward!

I think irrespective of which API we use, there is a possibility that the api response might be something unexpected once in a blue moon.

To address such issues, would a decorator function be a good idea to handle exceptions in each of the api dependant feature functions? @xofbd @Tim-Abwao

xofbd commented 4 years ago

Good conversations going one.

Crazy how it doesn't show the lat and lon for Nairobi, a city with several million people! Anyways I recommend:

  1. using a new service
  2. a decorator to handle those external API having some missing info. We should start putting any utility functions in a new utils.py file.