4
4
Classes:
5
5
AccessToken: Models a Spark 'access token' JSON object as a native Python
6
6
object.
7
- AccessTokensAPI: Wrappers the Cisco Spark AccessTokens -API and exposes the
8
- API calls as Python method calls that return native Python objects.
7
+ AccessTokensAPI: Wraps the Cisco Spark Access-Tokens -API and exposes the
8
+ APIs as native Python methods that return native Python objects.
9
9
10
10
"""
11
11
26
26
27
27
import requests
28
28
29
+ from ciscosparkapi .responsecodes import EXPECTED_RESPONSE_CODE
29
30
from ciscosparkapi .sparkdata import SparkData
30
31
from ciscosparkapi .utils import (
31
- validate_base_url ,
32
32
check_response_code ,
33
+ check_type ,
34
+ dict_from_items_with_values ,
33
35
extract_and_parse_json ,
36
+ validate_base_url ,
34
37
)
35
- from ciscosparkapi .responsecodes import EXPECTED_RESPONSE_CODE
36
38
37
39
38
40
__author__ = "Chris Lunsford"
@@ -48,10 +50,10 @@ class AccessToken(SparkData):
48
50
"""Model a Spark 'access token' JSON object as a native Python object."""
49
51
50
52
def __init__ (self , json ):
51
- """Init a new AccessToken data object from a JSON dictionary or string.
53
+ """Init a new AccessToken data object from a dictionary or JSON string.
52
54
53
55
Args:
54
- json(dict, basestring): Input JSON object .
56
+ json(dict, basestring): Input dictionary or JSON string .
55
57
56
58
Raises:
57
59
TypeError: If the input object is not a dictionary or string.
@@ -61,59 +63,62 @@ def __init__(self, json):
61
63
62
64
@property
63
65
def access_token (self ):
64
- """Cisco Spark access_token ."""
66
+ """Cisco Spark access token ."""
65
67
return self ._json .get ('access_token' )
66
68
67
69
@property
68
70
def expires_in (self ):
69
- """Access token expires_in number of seconds."""
71
+ """Access token expiry time (in seconds) ."""
70
72
return self ._json .get ('expires_in' )
71
73
72
74
@property
73
75
def refresh_token (self ):
74
- """refresh_token used to request a new/refreshed access_token ."""
76
+ """Refresh token used to request a new/refreshed access token ."""
75
77
return self ._json .get ('refresh_token' )
76
78
77
79
@property
78
80
def refresh_token_expires_in (self ):
79
- """refresh_token_expires_in number of seconds."""
81
+ """Refresh token expiry time (in seconds) ."""
80
82
return self ._json .get ('refresh_token_expires_in' )
81
83
82
84
83
85
class AccessTokensAPI (object ):
84
86
"""Cisco Spark Access-Tokens-API wrapper class.
85
87
86
- Wrappers the Cisco Spark Access-Tokens-API and exposes the API calls as
87
- Python method calls that return native Python objects.
88
+ Wraps the Cisco Spark Access-Tokens-API and exposes the APIs as native
89
+ Python methods that return native Python objects.
88
90
89
91
"""
90
92
91
93
def __init__ (self , base_url , timeout = None ):
92
- """Init a new AccessTokensAPI object with the provided RestSession.
94
+ """Initialize an AccessTokensAPI object with the provided RestSession.
93
95
94
96
Args:
95
97
base_url(basestring): The base URL the API endpoints.
96
98
timeout(int): Timeout in seconds for the API requests.
97
99
98
100
Raises:
99
- AssertionError : If the parameter types are incorrect.
101
+ TypeError : If the parameter types are incorrect.
100
102
101
103
"""
102
- assert isinstance (base_url , basestring )
103
- assert timeout is None or isinstance (timeout , int )
104
+ check_type (base_url , basestring , may_be_none = False )
105
+ check_type (timeout , int )
106
+
104
107
super (AccessTokensAPI , self ).__init__ ()
108
+
105
109
self ._base_url = str (validate_base_url (base_url ))
106
110
self ._timeout = timeout
107
111
self ._endpoint_url = urllib .parse .urljoin (self .base_url , API_ENDPOINT )
108
- self ._request_kwargs = {}
109
- self ._request_kwargs ["timeout" ] = timeout
112
+ self ._request_kwargs = {"timeout" : timeout }
110
113
111
114
@property
112
115
def base_url (self ):
116
+ """The base URL the API endpoints."""
113
117
return self ._base_url
114
118
115
119
@property
116
120
def timeout (self ):
121
+ """Timeout in seconds for the API requests."""
117
122
return self ._timeout
118
123
119
124
def get (self , client_id , client_secret , code , redirect_uri ):
@@ -123,8 +128,7 @@ def get(self, client_id, client_secret, code, redirect_uri):
123
128
invoke the APIs.
124
129
125
130
Args:
126
- client_id(basestring): Provided when you created your
127
- integration.
131
+ client_id(basestring): Provided when you created your integration.
128
132
client_secret(basestring): Provided when you created your
129
133
integration.
130
134
code(basestring): The Authorization Code provided by the user
@@ -133,40 +137,41 @@ def get(self, client_id, client_secret, code, redirect_uri):
133
137
process.
134
138
135
139
Returns:
136
- AccessToken: With the access token provided by the Cisco Spark
137
- cloud.
140
+ AccessToken: An AccessToken object with the access token provided
141
+ by the Cisco Spark cloud.
138
142
139
143
Raises:
140
- AssertionError : If the parameter types are incorrect.
144
+ TypeError : If the parameter types are incorrect.
141
145
SparkApiError: If the Cisco Spark cloud returns an error.
142
146
143
147
"""
144
- # Process args
145
- assert isinstance (client_id , basestring )
146
- assert isinstance (client_secret , basestring )
147
- assert isinstance (code , basestring )
148
- assert isinstance (redirect_uri , basestring )
149
- # Build request parameters
150
- data = {}
151
- data ["grant_type" ] = "authorization_code"
152
- data ["client_id" ] = client_id
153
- data ["client_secret" ] = client_secret
154
- data ["code" ] = code
155
- data ["redirect_uri" ] = redirect_uri
148
+ check_type (client_id , basestring , may_be_none = False )
149
+ check_type (client_secret , basestring , may_be_none = False )
150
+ check_type (code , basestring , may_be_none = False )
151
+ check_type (redirect_uri , basestring , may_be_none = False )
152
+
153
+ post_data = dict_from_items_with_values (
154
+ grant_type = "authorization_code" ,
155
+ client_id = client_id ,
156
+ client_secret = client_secret ,
157
+ code = code ,
158
+ redirect_uri = redirect_uri ,
159
+ )
160
+
156
161
# API request
157
- response = requests .post (self ._endpoint_url , data = data ,
162
+ response = requests .post (self ._endpoint_url , data = post_data ,
158
163
** self ._request_kwargs )
159
164
check_response_code (response , EXPECTED_RESPONSE_CODE ['POST' ])
160
165
json_data = extract_and_parse_json (response )
166
+
161
167
# Return a AccessToken object created from the response JSON data
162
168
return AccessToken (json_data )
163
169
164
170
def refresh (self , client_id , client_secret , refresh_token ):
165
- """Return a refreshed Access Token via the provided refresh_token.
171
+ """Return a refreshed Access Token from the provided refresh_token.
166
172
167
173
Args:
168
- client_id(basestring): Provided when you created your
169
- integration.
174
+ client_id(basestring): Provided when you created your integration.
170
175
client_secret(basestring): Provided when you created your
171
176
integration.
172
177
refresh_token(basestring): Provided when you requested the Access
@@ -177,24 +182,26 @@ def refresh(self, client_id, client_secret, refresh_token):
177
182
cloud.
178
183
179
184
Raises:
180
- AssertionError : If the parameter types are incorrect.
185
+ TypeError : If the parameter types are incorrect.
181
186
SparkApiError: If the Cisco Spark cloud returns an error.
182
187
183
188
"""
184
- # Process args
185
- assert isinstance (client_id , basestring )
186
- assert isinstance (client_secret , basestring )
187
- assert isinstance (refresh_token , basestring )
188
- # Build request parameters
189
- data = {}
190
- data ["grant_type" ] = "refresh_token"
191
- data ["client_id" ] = client_id
192
- data ["client_secret" ] = client_secret
193
- data ["refresh_token" ] = refresh_token
189
+ check_type (client_id , basestring , may_be_none = False )
190
+ check_type (client_secret , basestring , may_be_none = False )
191
+ check_type (refresh_token , basestring , may_be_none = False )
192
+
193
+ post_data = dict_from_items_with_values (
194
+ grant_type = "refresh_token" ,
195
+ client_id = client_id ,
196
+ client_secret = client_secret ,
197
+ refresh_token = refresh_token ,
198
+ )
199
+
194
200
# API request
195
- response = requests .post (self ._endpoint_url , data = data ,
201
+ response = requests .post (self ._endpoint_url , data = post_data ,
196
202
** self ._request_kwargs )
197
203
check_response_code (response , EXPECTED_RESPONSE_CODE ['POST' ])
198
204
json_data = extract_and_parse_json (response )
205
+
199
206
# Return a AccessToken object created from the response JSON data
200
207
return AccessToken (json_data )
0 commit comments