44Classes:
55 AccessToken: Models a Spark 'access token' JSON object as a native Python
66 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.
99
1010"""
1111
2626
2727import requests
2828
29+ from ciscosparkapi .responsecodes import EXPECTED_RESPONSE_CODE
2930from ciscosparkapi .sparkdata import SparkData
3031from ciscosparkapi .utils import (
31- validate_base_url ,
3232 check_response_code ,
33+ check_type ,
34+ dict_from_items_with_values ,
3335 extract_and_parse_json ,
36+ validate_base_url ,
3437)
35- from ciscosparkapi .responsecodes import EXPECTED_RESPONSE_CODE
3638
3739
3840__author__ = "Chris Lunsford"
@@ -48,10 +50,10 @@ class AccessToken(SparkData):
4850 """Model a Spark 'access token' JSON object as a native Python object."""
4951
5052 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.
5254
5355 Args:
54- json(dict, basestring): Input JSON object .
56+ json(dict, basestring): Input dictionary or JSON string .
5557
5658 Raises:
5759 TypeError: If the input object is not a dictionary or string.
@@ -61,59 +63,62 @@ def __init__(self, json):
6163
6264 @property
6365 def access_token (self ):
64- """Cisco Spark access_token ."""
66+ """Cisco Spark access token ."""
6567 return self ._json .get ('access_token' )
6668
6769 @property
6870 def expires_in (self ):
69- """Access token expires_in number of seconds."""
71+ """Access token expiry time (in seconds) ."""
7072 return self ._json .get ('expires_in' )
7173
7274 @property
7375 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 ."""
7577 return self ._json .get ('refresh_token' )
7678
7779 @property
7880 def refresh_token_expires_in (self ):
79- """refresh_token_expires_in number of seconds."""
81+ """Refresh token expiry time (in seconds) ."""
8082 return self ._json .get ('refresh_token_expires_in' )
8183
8284
8385class AccessTokensAPI (object ):
8486 """Cisco Spark Access-Tokens-API wrapper class.
8587
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.
8890
8991 """
9092
9193 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.
9395
9496 Args:
9597 base_url(basestring): The base URL the API endpoints.
9698 timeout(int): Timeout in seconds for the API requests.
9799
98100 Raises:
99- AssertionError : If the parameter types are incorrect.
101+ TypeError : If the parameter types are incorrect.
100102
101103 """
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+
104107 super (AccessTokensAPI , self ).__init__ ()
108+
105109 self ._base_url = str (validate_base_url (base_url ))
106110 self ._timeout = timeout
107111 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 }
110113
111114 @property
112115 def base_url (self ):
116+ """The base URL the API endpoints."""
113117 return self ._base_url
114118
115119 @property
116120 def timeout (self ):
121+ """Timeout in seconds for the API requests."""
117122 return self ._timeout
118123
119124 def get (self , client_id , client_secret , code , redirect_uri ):
@@ -123,8 +128,7 @@ def get(self, client_id, client_secret, code, redirect_uri):
123128 invoke the APIs.
124129
125130 Args:
126- client_id(basestring): Provided when you created your
127- integration.
131+ client_id(basestring): Provided when you created your integration.
128132 client_secret(basestring): Provided when you created your
129133 integration.
130134 code(basestring): The Authorization Code provided by the user
@@ -133,40 +137,41 @@ def get(self, client_id, client_secret, code, redirect_uri):
133137 process.
134138
135139 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.
138142
139143 Raises:
140- AssertionError : If the parameter types are incorrect.
144+ TypeError : If the parameter types are incorrect.
141145 SparkApiError: If the Cisco Spark cloud returns an error.
142146
143147 """
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+
156161 # API request
157- response = requests .post (self ._endpoint_url , data = data ,
162+ response = requests .post (self ._endpoint_url , data = post_data ,
158163 ** self ._request_kwargs )
159164 check_response_code (response , EXPECTED_RESPONSE_CODE ['POST' ])
160165 json_data = extract_and_parse_json (response )
166+
161167 # Return a AccessToken object created from the response JSON data
162168 return AccessToken (json_data )
163169
164170 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.
166172
167173 Args:
168- client_id(basestring): Provided when you created your
169- integration.
174+ client_id(basestring): Provided when you created your integration.
170175 client_secret(basestring): Provided when you created your
171176 integration.
172177 refresh_token(basestring): Provided when you requested the Access
@@ -177,24 +182,26 @@ def refresh(self, client_id, client_secret, refresh_token):
177182 cloud.
178183
179184 Raises:
180- AssertionError : If the parameter types are incorrect.
185+ TypeError : If the parameter types are incorrect.
181186 SparkApiError: If the Cisco Spark cloud returns an error.
182187
183188 """
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+
194200 # API request
195- response = requests .post (self ._endpoint_url , data = data ,
201+ response = requests .post (self ._endpoint_url , data = post_data ,
196202 ** self ._request_kwargs )
197203 check_response_code (response , EXPECTED_RESPONSE_CODE ['POST' ])
198204 json_data = extract_and_parse_json (response )
205+
199206 # Return a AccessToken object created from the response JSON data
200207 return AccessToken (json_data )
0 commit comments