|
| 1 | +"""Extend API Client |
| 2 | +
|
| 3 | +Copyright (C) 2015, Digium, Inc. |
| 4 | +Matthew Jordan <[email protected]> |
| 5 | +""" |
| 6 | + |
| 7 | +from requests.auth import HTTPDigestAuth |
| 8 | +import requests |
| 9 | + |
| 10 | +class HTTPException(Exception): |
| 11 | + """An exception raised if a non-success response is returned |
| 12 | + """ |
| 13 | + |
| 14 | + def __init__(self, message, status_code): |
| 15 | + super(HTTPException, self).__init__(message) |
| 16 | + self.status_code = status_code |
| 17 | + |
| 18 | + |
| 19 | +class ExtendAPIError(Exception): |
| 20 | + """An exception raised if an error is returned from the Extend API |
| 21 | + """ |
| 22 | + |
| 23 | + def __init__(self, message, error_code): |
| 24 | + super(ExtendAPIError, self).__init__(message) |
| 25 | + self.error_code = error_code |
| 26 | + |
| 27 | + |
| 28 | +class Query(object): |
| 29 | + """A query sent to the Extend API |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__(self, name, client): |
| 33 | + self._path = ['switchvox', name] |
| 34 | + self._client = client |
| 35 | + |
| 36 | + def __getattr__(self, name): |
| 37 | + self._path.append(name) |
| 38 | + return self |
| 39 | + |
| 40 | + def __call__(self, **kwargs): |
| 41 | + """Invoke the query |
| 42 | + """ |
| 43 | + request_json = {'request': {'method': '.'.join(self._path), |
| 44 | + 'parameters': kwargs}} |
| 45 | + return self._client(query=request_json) |
| 46 | + |
| 47 | + |
| 48 | +class Client(object): |
| 49 | + """A client back to the Switchvox Extend API |
| 50 | + """ |
| 51 | + |
| 52 | + def __init__(self, address, username, password): |
| 53 | + """Create a new client connection |
| 54 | +
|
| 55 | + Keyword Arguments: |
| 56 | + address The address of the Switchvox server |
| 57 | + username The admin username |
| 58 | + password The admin password |
| 59 | + """ |
| 60 | + |
| 61 | + self._address = address |
| 62 | + self._session = requests.Session() |
| 63 | + self._session.auth = HTTPDigestAuth(username, password) |
| 64 | + |
| 65 | + def close(self): |
| 66 | + """Close the client connection |
| 67 | + """ |
| 68 | + self._session.close() |
| 69 | + |
| 70 | + def __call__(self, query): |
| 71 | + """Execute a query object |
| 72 | + """ |
| 73 | + |
| 74 | + # Since Switchvox uses self signed certs, we need to not |
| 75 | + # verify the cert |
| 76 | + response = self._session.post('https://' + self._address + '/json', |
| 77 | + json=query, |
| 78 | + verify=False) |
| 79 | + |
| 80 | + if (response.status_code / 100 != 2): |
| 81 | + raise HTTPException(response.reason, response.status_code) |
| 82 | + |
| 83 | + json = response.json().get('response') |
| 84 | + |
| 85 | + errors = json.get('errors') |
| 86 | + if errors: |
| 87 | + raise ExtendAPIError(errors['error']['message'], |
| 88 | + int(errors['error']['code'])) |
| 89 | + return json |
| 90 | + |
| 91 | + def __getattr__(self, name): |
| 92 | + """Intercept an attribute retrieval and invoke the API call |
| 93 | + """ |
| 94 | + return Query(name, self) |
| 95 | + |
| 96 | + def __enter__(self): |
| 97 | + return self |
| 98 | + |
| 99 | + def __exit__(self, exc_type, exc_value, exc_tb): |
| 100 | + return self.close() |
0 commit comments