Skip to content

Commit 926847f

Browse files
committed
Add SPARK_ACCESS_TOKEN environment variable support
Add package support to get the access token to be used for API calls from the SPARK_ACCESS_TOKEN environment variable, if available and if an access toke isn’t manually provided.
1 parent 02ebc89 commit 926847f

File tree

1 file changed

+58
-8
lines changed

1 file changed

+58
-8
lines changed

ciscosparkapi/__init__.py

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
"""Python API wrapper for the Cisco Spark APIs."""
2+
3+
14
# Versioneer version control
25
from ._version import get_versions
36
__version__ = get_versions()['version']
47
del get_versions
58

9+
10+
import os
11+
612
from exceptions import ciscosparkapiException, SparkApiError
713
from restsession import RestSession
814
from api.accesstokens import AccessToken, AccessTokensAPI
@@ -15,22 +21,66 @@
1521
from api.webhooks import Webhook, WebhooksAPI
1622

1723

18-
# Default base URL
1924
DEFAULT_BASE_URL = 'https://api.ciscospark.com/v1/'
2025

2126

2227
class CiscoSparkAPI(object):
2328
"""Cisco Spark API wrapper class."""
2429

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+
"""
2668
# 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}
3282

33-
# Create API session
83+
# Create the API session
3484
# All of the API calls associated with a CiscoSparkAPI object will
3585
# leverage a single RESTful 'session' connecting to the Cisco Spark
3686
# cloud.

0 commit comments

Comments
 (0)