backtrackbaba / cowin

Python wrapper for CoWin API's
MIT License
42 stars 18 forks source link

API is unable to fetch data by pincode #20

Open babvin opened 3 years ago

babvin commented 3 years ago

from cowin_api import CoWinAPI

pin_code = ["570021","560066"] date = '03-05-2021' # Optional. Default value is today's date min_age_limit = 18 # Optional. By default returns centers without filtering by min_age_limit for pin in pin_code: cowin = CoWinAPI() print(pin, type(pin)) try: available_centers = cowin.get_availability_by_pincode(pin_code, date, min_age_limit) print(available_centers) except Exception as Error: print("Error for pin,", pin, Error)

570021 <class 'str'> Error for pin, 570021 'HTTPError' object has no attribute 'get' 560066 <class 'str'> Error for pin, 560066 'HTTPError' object has no attribute 'get'

backtrackbaba commented 3 years ago

Hi @babvin, this is a general issue with CoWin where it throws a 401 error at times. I ran the sample you provided and was able to execute it successfully.

For now, try to retry in case you get an Exception. There is an open PR #16 which will be merged soon and would give you an option to retry automatically.

Also, the exception handling in the library is not too good currently. We are working on it and would be releasing a v1.0.0 with the updated breaking changes which would also address the exceptions being raised.

clatonhendricks commented 3 years ago

I've noticed this error when you try to fetch data outside of India, I have to use a VPN to go around it. I've added try-exception block to get around the issue and provide an useful msg to the user. Would appreciate if exception is thron from the API.

backtrackbaba commented 3 years ago

Hi @clatonhendricks, there's a geofencing enabled by the CoWin team which forces you to use Indian IPs to fetch the data.

About the useful error message, apologies for the issue, that's an oversight/mistake from my end which I've fixed in the latest code on master. This will propagate the exceptions thrown by the requests library based on which you can handle. I'll be deploying it to PyPi today as v1.0 as it has some breaking changes.

I'll notify here as soon as it is pushed to PyPi