home-assistant-ecosystem / python-opendata-transport

Python client for interacting with transport.opendata.ch
MIT License
11 stars 9 forks source link

Catch RateLimit/Throttle as as exception? #3

Closed vikramgorla closed 6 years ago

vikramgorla commented 6 years ago

Fabian,

It looks like async_get_data is proceeding to get connections info even if the response is not 200 (e.g,. ratelimiting with http 429) - do you think it helps if an error is raised when the quota is exceeded?

Sample exception raised in homeassistant when the ratelimiter of opendata.ch blocks you

Update for sensor.sbb_home_work fails
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 224, in async_update_ha_state
    yield from self.async_device_update()
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 351, in async_device_update
    yield from self.async_update()
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/sensor/swiss_public_transport.py", line 129, in async_update
    await self._opendata.async_get_data()
  File "/usr/local/lib/python3.6/site-packages/opendata_transport/__init__.py", line 47, in async_get_data
    self.from_id = data['from']['id']
KeyError: 'from'

Response from opendata.ch

Request URL: http://transport.opendata.ch/v1/connections?from=Bex&to=Vevey
Request Method: GET
Status Code: 429 Too Many Requests

{"errors":[{"message":"Rate limit error from timetable.search.ch: Too many requests, try again tomorrow."}]}

Cheers,

fabaff commented 6 years ago

Yes, it would. I never ran into issues with the rate limit because I only needed the data for one connection.

vikramgorla commented 6 years ago

Thank you Fabian, I will submit a pull request and see how can we reduce number of calls made to api (e.g,. call the api only just before the next known train time in a rate optimized mode controlled by a flag,...) in home assistant.