|
| 1 | +"""Python API wrapper for the Cisco Spark APIs.""" |
| 2 | + |
| 3 | + |
1 | 4 | # Versioneer version control |
2 | 5 | from ._version import get_versions |
3 | 6 | __version__ = get_versions()['version'] |
4 | 7 | del get_versions |
5 | 8 |
|
| 9 | + |
| 10 | +import os |
| 11 | + |
6 | 12 | from exceptions import ciscosparkapiException, SparkApiError |
7 | 13 | from restsession import RestSession |
8 | 14 | from api.accesstokens import AccessToken, AccessTokensAPI |
|
15 | 21 | from api.webhooks import Webhook, WebhooksAPI |
16 | 22 |
|
17 | 23 |
|
18 | | -# Default base URL |
19 | 24 | DEFAULT_BASE_URL = 'https://api.ciscospark.com/v1/' |
20 | 25 |
|
21 | 26 |
|
22 | 27 | class CiscoSparkAPI(object): |
23 | 28 | """Cisco Spark API wrapper class.""" |
24 | 29 |
|
25 | | - def __init__(self, access_token, base_url=DEFAULT_BASE_URL, timeout=None): |
| 30 | + def __init__(self, access_token=None, base_url=DEFAULT_BASE_URL, |
| 31 | + timeout=60): |
| 32 | + """Init a new CiscoSparkAPI object. |
| 33 | +
|
| 34 | + An access token must be used when interacting with the Cisco Spark API. |
| 35 | + This package supports two methods for you to provide that access token: |
| 36 | +
|
| 37 | + 1. You may manually specify the access token via the access_token |
| 38 | + argument, when creating a new CiscoSparkAPI object. |
| 39 | +
|
| 40 | + 2. If an access_token argument is not supplied, the package checks |
| 41 | + for a SPARK_ACCESS_TOKEN environment variable, and if available, |
| 42 | + it uses the value of this environment variable as the access_token |
| 43 | + when new CiscoSparkAPI objects are created. |
| 44 | +
|
| 45 | + A ciscosparkapiException is raised if an access token is not provided |
| 46 | + via one of these two methods. |
| 47 | +
|
| 48 | + Args: |
| 49 | + access_token(unicode, str): The access token to be used for API |
| 50 | + calls to the Cisco Spark service. Defaults to checking for a |
| 51 | + SPARK_ACCESS_TOKEN environment variable. |
| 52 | + base_url(unicode, str): The base URL to be prefixed to the |
| 53 | + individual API endpoint suffixes. |
| 54 | + Defaults to ciscosparkapi.DEFAULT_BASE_URL. |
| 55 | + timeout(int): Timeout (in seconds) for RESTful HTTP requests. |
| 56 | + Defaults to 60 seconds. |
| 57 | +
|
| 58 | + Returns: |
| 59 | + CiscoSparkAPI: A new CiscoSparkAPI connection object. |
| 60 | +
|
| 61 | + Raises: |
| 62 | + AssertionError: If the parameter types are incorrect. |
| 63 | + ciscosparkapiException: If an access token is not provided via the |
| 64 | + access_token argument or SPARK_ACCESS_TOKEN environement |
| 65 | + variable. |
| 66 | +
|
| 67 | + """ |
26 | 68 | # Process args |
27 | | - assert isinstance(access_token, basestring) |
28 | | - # Process kwargs |
29 | | - session_args = {} |
30 | | - if timeout: |
31 | | - session_args['timeout'] = timeout |
| 69 | + assert access_token is None or isinstance(access_token, basestring) |
| 70 | + assert isinstance(base_url, basestring) |
| 71 | + assert isinstance(timeout, int) |
| 72 | + spark_access_token = os.environ.get('SPARK_ACCESS_TOKEN', None) |
| 73 | + access_token = access_token if access_token else spark_access_token |
| 74 | + if not access_token: |
| 75 | + error_message = "You must provide an access token to interact " \ |
| 76 | + "with the Cisco Spark APIs, either via the " \ |
| 77 | + "access_token argument or via a " \ |
| 78 | + "SPARK_ACCESS_TOKEN environement variable. " \ |
| 79 | + "None provided." |
| 80 | + raise ciscosparkapiException(error_message) |
| 81 | + session_args = {u'timeout': timeout} |
32 | 82 |
|
33 | | - # Create API session |
| 83 | + # Create the API session |
34 | 84 | # All of the API calls associated with a CiscoSparkAPI object will |
35 | 85 | # leverage a single RESTful 'session' connecting to the Cisco Spark |
36 | 86 | # cloud. |
|
0 commit comments