Advertisement
Guest User

Untitled

a guest
Aug 16th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. import json
  2. import requests
  3.  
  4. BASE_URL = "https://chaos.aa.net.uk/"
  5.  
  6. class Chaos(object):
  7. """This class allows access to the Andrews & Arnold API.
  8.  
  9. Note that it is based on trial and error, there is very little
  10. official documentation about this API yet, so use at your own risk.
  11. """
  12.  
  13. def __init__(self, username, password):
  14. """Initialize the class.
  15.  
  16. Use the same credentials as on control.aa.net.uk.
  17.  
  18. Args:
  19. username: username like xx00@x
  20. password: self-explanatory
  21. """
  22. self.session = requests.session()
  23. self.session.headers["User-Agent"] = "Python Chaos Client"
  24. self.session.auth = (username, password)
  25.  
  26. def _request(self, **kwargs):
  27. """Make an API request, lets Requests check the HTTP status code
  28. then checks if the "error" string is present in the response
  29. and raises an exception if that's the case.
  30.  
  31. Args:
  32. **kwargs: will be passed as-is to python-requests
  33. Returns:
  34. a dict representation of the APi'S JSON reply
  35. Raises:
  36. Exception: the remote server returned an error
  37. """
  38.  
  39. resp = self.session.post(BASE_URL, **kwargs)
  40.  
  41. if resp.status_code != requests.codes.ok:
  42. resp.raise_for_status()
  43.  
  44. resp = resp.json()
  45.  
  46. if "error" in resp:
  47. raise APIError(resp["error"])
  48.  
  49. return resp
  50.  
  51. def info(self, **kwargs):
  52. return self._request(json={**kwargs, **{"command": "info"}})
  53.  
  54. def change(self, **kwargs):
  55. required = ["broadband", "sim", "voip"]
  56.  
  57. if not any(arg in required for arg in kwargs):
  58. raise InvalidParameters("Missing object of types: " + ", ".join(required))
  59.  
  60. return self._request(json={**kwargs, **{"command": "change"}})
  61.  
  62. def check(self, **kwargs):
  63. required = ["order"]
  64.  
  65. if not any(arg in required for arg in kwargs):
  66. raise InvalidParameters("Missing object of types: " + ", ".join(required))
  67.  
  68. return self._request(json={**kwargs, **{"command": "check"}})
  69.  
  70. def preorder(self, **kwargs):
  71. required = ["order"]
  72.  
  73. if not any(arg in required for arg in kwargs):
  74. raise InvalidParameters("Missing object of types: " + ", ".join(required))
  75.  
  76. return self._request(json={**kwargs, **{"command": "preorder"}})
  77.  
  78. def order(self, **kwargs):
  79. required = ["order"]
  80.  
  81. if not any(arg in required for arg in kwargs):
  82. raise InvalidParameters("Missing object of types: " + ", ".join(required))
  83.  
  84. return self._request(json={**kwargs, **{"command": "order"}})
  85.  
  86. def usage(self, **kwargs):
  87. required = ["broadband", "sim", "voip"]
  88.  
  89. if not any(arg in required for arg in kwargs):
  90. raise InvalidParameters("Missing object of types: " + ", ".join(allowed))
  91.  
  92. return self._request(json={**kwargs, **{"command": "usage"}})
  93.  
  94. def availability(self, **kwargs):
  95. required = ["broadband"]
  96.  
  97. if not any(arg in required for arg in kwargs):
  98. raise InvalidParameters("Missing object of types: " + ", ".join(required))
  99.  
  100. return self._request(json={**kwargs, **{"command": "availability"}})
  101.  
  102. class ChaosException(Exception):
  103. """Base class for all our exceptions.
  104. """
  105. pass
  106.  
  107. class InvalidParameters(ChaosException):
  108. """Indicates a problem with the request's arguments.
  109. """
  110. pass
  111.  
  112. class APIError(ChaosException):
  113. """Indicates an error returned by the remote server.
  114. """
  115. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement