client.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import requests
  2. ConnectionError = requests.exceptions.ConnectionError
  3. class Client:
  4. base_url = 'https://api.audiovalid.com'
  5. def __init__(self, key, secret, base_url=None):
  6. self.key = key
  7. self.secret = secret
  8. if base_url:
  9. self.base_url = base_url
  10. @property
  11. def headers(self):
  12. return {
  13. 'Authorization': '{} {}'\
  14. .format(self.key, self.secret)
  15. }
  16. def put_schedule_results(self, sched_id, item_id,
  17. results, found=None,
  18. missing_files=0):
  19. url = '{}/schedule/{}/item/{}/results'.format(
  20. self.base_url,
  21. sched_id,
  22. item_id,
  23. )
  24. response = requests.put(url,
  25. json={
  26. 'results': results,
  27. 'found': found,
  28. 'missing_files': missing_files \
  29. if missing_files is not None
  30. else 0,
  31. },
  32. headers=self.headers,
  33. )
  34. return response.json()
  35. def get_schedule_pending(self):
  36. url = '{}/schedule/pending'.format(self.base_url)
  37. response = requests.get(url, headers=self.headers)
  38. return response.json()
  39. def get_channels(self):
  40. """ Return the list of TV channels """
  41. url = '{}/channel'.format(self.base_url)
  42. response = requests.get(url, headers=self.headers)
  43. return response.json()['channels']
  44. def get_calibrations(self, station=None):
  45. """ Return the calibration of a station in a box """
  46. url = '{}/device'.format(self.base_url)
  47. params = {'what': 'calibrations'}
  48. if station:
  49. params['station'] = station
  50. response = requests.get(url,
  51. headers=self.headers,
  52. params=params,
  53. )
  54. return response.json()['calibrations']