Skip to content

Commit 143bc94

Browse files
committed
Initial AccessTokensAPI wrapper
Add wrapper classes for the Cisco Spark Access-Tokens-API.
1 parent a8dc9d2 commit 143bc94

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed

ciscosparkapi/api/accesstokens.py

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
"""Cisco Spark Access-Tokens-API wrapper classes.
2+
3+
Classes:
4+
AccessToken: Models a Spark 'access token' JSON object as a native Python
5+
object.
6+
AccessTokensAPI: Wrappers the Cisco Spark AccessTokens-API and exposes the
7+
API calls as Python method calls that return native Python objects.
8+
9+
"""
10+
11+
12+
import urlparse
13+
14+
import requests
15+
16+
from ciscosparkapi.helper import utf8, ERC, validate_base_url, \
17+
check_response_code, extract_and_parse_json
18+
from ciscosparkapi.sparkdata import SparkData
19+
20+
21+
API_ENDPOINT = u"access_token"
22+
23+
24+
class AccessToken(SparkData):
25+
"""Model a Spark 'access token' JSON object as a native Python object."""
26+
27+
def __init__(self, json):
28+
"""Init a new AccessToken data object from a JSON dictionary or string.
29+
30+
Args:
31+
json(dict, unicode, str): Input JSON object.
32+
33+
Raises:
34+
TypeError: If the input object is not a dictionary or string.
35+
36+
"""
37+
super(AccessToken, self).__init__(json)
38+
39+
@property
40+
def access_token(self):
41+
"""Cisco Spark access_token."""
42+
return self._json.get(u'access_token')
43+
44+
@property
45+
def expires_in(self):
46+
"""Access token expires_in number of seconds."""
47+
return self._json.get(u'expires_in')
48+
49+
@property
50+
def refresh_token(self):
51+
"""refresh_token used to request a new/refreshed access_token."""
52+
return self._json.get(u'refresh_token')
53+
54+
@property
55+
def refresh_token_expires_in(self):
56+
"""refresh_token_expires_in number of seconds."""
57+
return self._json.get(u'refresh_token_expires_in')
58+
59+
60+
class AccessTokensAPI(object):
61+
"""Cisco Spark Access-Tokens-API wrapper class.
62+
63+
Wrappers the Cisco Spark Access-Tokens-API and exposes the API calls as
64+
Python method calls that return native Python objects.
65+
66+
"""
67+
68+
def __init__(self, base_url, timeout=None):
69+
"""Init a new AccessTokensAPI object with the provided RestSession.
70+
71+
Args:
72+
base_url(unicode, str): The base URL the API endpoints.
73+
timeout(int): Timeout in seconds for the API requests.
74+
75+
Raises:
76+
AssertionError: If the parameter types are incorrect.
77+
78+
"""
79+
assert isinstance(base_url, basestring)
80+
assert timeout is None or isinstance(timeout, basestring)
81+
super(AccessTokensAPI, self).__init__()
82+
self._base_url = validate_base_url(base_url)
83+
self._timeout = timeout
84+
self._endpoint_url = urlparse.urljoin(self.base_url, API_ENDPOINT)
85+
self._request_kwargs = {}
86+
self._request_kwargs[u"timeout"] = timeout
87+
88+
@property
89+
def base_url(self):
90+
return self._base_url
91+
92+
@property
93+
def timeout(self):
94+
return self._timeout
95+
96+
def get(self, client_id, client_secret, code, redirect_uri):
97+
"""Exchange an Authorization Code for an Access Token.
98+
99+
Exchange an Authorization Code for an Access Token that can be used to
100+
invoke the APIs.
101+
102+
Args:
103+
client_id(unicode, str): Provided when you created your
104+
integration.
105+
client_secret(unicode, str): Provided when you created your
106+
integration.
107+
code(unicode, str): The Authorization Code provided by the user
108+
OAuth process.
109+
redirect_uri(unicode, str): The redirect URI used in the user OAuth
110+
process.
111+
112+
Returns:
113+
AccessToken: With the access token provided by the Cisco Spark
114+
cloud.
115+
116+
Raises:
117+
AssertionError: If the parameter types are incorrect.
118+
SparkApiError: If the Cisco Spark cloud returns an error.
119+
120+
"""
121+
# Process args
122+
assert isinstance(client_id, basestring)
123+
assert isinstance(client_secret, basestring)
124+
assert isinstance(code, basestring)
125+
assert isinstance(redirect_uri, basestring)
126+
# Build request parameters
127+
data = {}
128+
data[u"grant_type"] = u"authorization_code"
129+
data[u"client_id"] = utf8(client_id)
130+
data[u"client_secret"] = utf8(client_secret)
131+
data[u"code"] = utf8(code)
132+
data[u"redirect_uri"] = utf8(redirect_uri)
133+
# API request
134+
response = requests.post(self._endpoint_url, data=data,
135+
**self._request_kwargs)
136+
check_response_code(response, ERC['POST'])
137+
json_data = extract_and_parse_json(response)
138+
# Return a AccessToken object created from the response JSON data
139+
return AccessToken(json_data)
140+
141+
def refresh(self, client_id, client_secret, refresh_token):
142+
"""Return a refreshed Access Token via the provided refresh_token.
143+
144+
Args:
145+
client_id(unicode, str): Provided when you created your
146+
integration.
147+
client_secret(unicode, str): Provided when you created your
148+
integration.
149+
refresh_token(unicode, str): Provided when you requested the Access
150+
Token.
151+
152+
Returns:
153+
AccessToken: With the access token provided by the Cisco Spark
154+
cloud.
155+
156+
Raises:
157+
AssertionError: If the parameter types are incorrect.
158+
SparkApiError: If the Cisco Spark cloud returns an error.
159+
160+
"""
161+
# Process args
162+
assert isinstance(client_id, basestring)
163+
assert isinstance(client_secret, basestring)
164+
assert isinstance(refresh_token, basestring)
165+
# Build request parameters
166+
data = {}
167+
data[u"grant_type"] = u"refresh_token"
168+
data[u"client_id"] = utf8(client_id)
169+
data[u"client_secret"] = utf8(client_secret)
170+
data[u"refresh_token"] = utf8(refresh_token)
171+
# API request
172+
response = requests.post(self._endpoint_url, data=data,
173+
**self._request_kwargs)
174+
check_response_code(response, ERC['POST'])
175+
json_data = extract_and_parse_json(response)
176+
# Return a AccessToken object created from the response JSON data
177+
return AccessToken(json_data)

0 commit comments

Comments
 (0)