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