Skip to content

Commit 5316af3

Browse files
committed
Refactor Python v2 & v3 Compatibility Mechanism
Remove Six dependency by leveraging additional capabilities of the Future package and refactoring unicode type checking and use.
1 parent 0a7f2db commit 5316af3

33 files changed

+431
-211
lines changed

ciscosparkapi/__init__.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22
"""Python API wrapper for the Cisco Spark APIs."""
33

44

5-
from __future__ import absolute_import
6-
from builtins import object
7-
from six import string_types
5+
# Use future for Python v2 and v3 compatibility
6+
from __future__ import (
7+
absolute_import,
8+
division,
9+
print_function,
10+
unicode_literals,
11+
)
12+
from builtins import *
13+
14+
from past.builtins import basestring
815

916
import os
1017

@@ -91,10 +98,10 @@ def __init__(self, access_token=None, base_url=DEFAULT_BASE_URL,
9198
via one of these two methods.
9299
93100
Args:
94-
access_token(string_types): The access token to be used for API
101+
access_token(str): The access token to be used for API
95102
calls to the Cisco Spark service. Defaults to checking for a
96103
SPARK_ACCESS_TOKEN environment variable.
97-
base_url(string_types): The base URL to be prefixed to the
104+
base_url(str): The base URL to be prefixed to the
98105
individual API endpoint suffixes.
99106
Defaults to ciscosparkapi.DEFAULT_BASE_URL.
100107
timeout(int): Timeout (in seconds) for RESTful HTTP requests.
@@ -111,8 +118,8 @@ def __init__(self, access_token=None, base_url=DEFAULT_BASE_URL,
111118
112119
"""
113120
# Process args
114-
assert access_token is None or isinstance(access_token, string_types)
115-
assert isinstance(base_url, string_types)
121+
assert access_token is None or isinstance(access_token, basestring)
122+
assert isinstance(base_url, str)
116123
assert isinstance(timeout, int)
117124
spark_access_token = os.environ.get(ACCESS_TOKEN_ENVIRONMENT_VARIABLE)
118125
access_token = access_token if access_token else spark_access_token

ciscosparkapi/api/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding: utf-8 -*-

ciscosparkapi/api/accesstokens.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@
1010
"""
1111

1212

13+
# Use future for Python v2 and v3 compatibility
14+
from __future__ import (
15+
absolute_import,
16+
division,
17+
print_function,
18+
unicode_literals,
19+
)
20+
from builtins import *
1321
from future import standard_library
1422
standard_library.install_aliases()
15-
from builtins import object
16-
from six import string_types
1723

1824
import urllib.parse
1925

@@ -44,7 +50,7 @@ def __init__(self, json):
4450
"""Init a new AccessToken data object from a JSON dictionary or string.
4551
4652
Args:
47-
json(dict, string_types): Input JSON object.
53+
json(dict, str): Input JSON object.
4854
4955
Raises:
5056
TypeError: If the input object is not a dictionary or string.
@@ -85,14 +91,14 @@ def __init__(self, base_url, timeout=None):
8591
"""Init a new AccessTokensAPI object with the provided RestSession.
8692
8793
Args:
88-
base_url(string_types): The base URL the API endpoints.
94+
base_url(str): The base URL the API endpoints.
8995
timeout(int): Timeout in seconds for the API requests.
9096
9197
Raises:
9298
AssertionError: If the parameter types are incorrect.
9399
94100
"""
95-
assert isinstance(base_url, string_types)
101+
assert isinstance(base_url, str)
96102
assert timeout is None or isinstance(timeout, int)
97103
super(AccessTokensAPI, self).__init__()
98104
self._base_url = str(validate_base_url(base_url))
@@ -116,13 +122,13 @@ def get(self, client_id, client_secret, code, redirect_uri):
116122
invoke the APIs.
117123
118124
Args:
119-
client_id(string_types): Provided when you created your
125+
client_id(str): Provided when you created your
120126
integration.
121-
client_secret(string_types): Provided when you created your
127+
client_secret(str): Provided when you created your
122128
integration.
123-
code(string_types): The Authorization Code provided by the user
129+
code(str): The Authorization Code provided by the user
124130
OAuth process.
125-
redirect_uri(string_types): The redirect URI used in the user OAuth
131+
redirect_uri(str): The redirect URI used in the user OAuth
126132
process.
127133
128134
Returns:
@@ -135,10 +141,10 @@ def get(self, client_id, client_secret, code, redirect_uri):
135141
136142
"""
137143
# Process args
138-
assert isinstance(client_id, string_types)
139-
assert isinstance(client_secret, string_types)
140-
assert isinstance(code, string_types)
141-
assert isinstance(redirect_uri, string_types)
144+
assert isinstance(client_id, str)
145+
assert isinstance(client_secret, str)
146+
assert isinstance(code, str)
147+
assert isinstance(redirect_uri, str)
142148
# Build request parameters
143149
data = {}
144150
data["grant_type"] = "authorization_code"
@@ -158,11 +164,11 @@ def refresh(self, client_id, client_secret, refresh_token):
158164
"""Return a refreshed Access Token via the provided refresh_token.
159165
160166
Args:
161-
client_id(string_types): Provided when you created your
167+
client_id(str): Provided when you created your
162168
integration.
163-
client_secret(string_types): Provided when you created your
169+
client_secret(str): Provided when you created your
164170
integration.
165-
refresh_token(string_types): Provided when you requested the Access
171+
refresh_token(str): Provided when you requested the Access
166172
Token.
167173
168174
Returns:
@@ -175,9 +181,9 @@ def refresh(self, client_id, client_secret, refresh_token):
175181
176182
"""
177183
# Process args
178-
assert isinstance(client_id, string_types)
179-
assert isinstance(client_secret, string_types)
180-
assert isinstance(refresh_token, string_types)
184+
assert isinstance(client_id, str)
185+
assert isinstance(client_secret, str)
186+
assert isinstance(refresh_token, str)
181187
# Build request parameters
182188
data = {}
183189
data["grant_type"] = "refresh_token"

ciscosparkapi/api/licenses.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@
99
"""
1010

1111

12-
from builtins import object
13-
from six import string_types
12+
# Use future for Python v2 and v3 compatibility
13+
from __future__ import (
14+
absolute_import,
15+
division,
16+
print_function,
17+
unicode_literals,
18+
)
19+
from builtins import *
1420

1521
from ciscosparkapi.utils import generator_container
1622
from ciscosparkapi.restsession import RestSession
@@ -30,7 +36,7 @@ def __init__(self, json):
3036
"""Init a new License data object from a dict or JSON string.
3137
3238
Args:
33-
json(dict, string_types): Input JSON object.
39+
json(dict, str): Input JSON object.
3440
3541
Raises:
3642
TypeError: If the input object is not a dictionary or string.
@@ -99,7 +105,7 @@ def list(self, orgId=None, max=None):
99105
container.
100106
101107
Args:
102-
orgId(string_types): Filters the returned licenses to only include
108+
orgId(str): Filters the returned licenses to only include
103109
those liceses associated with the specified Organization
104110
(orgId).
105111
max(int): Limits the maximum number of entries returned from the
@@ -116,7 +122,7 @@ def list(self, orgId=None, max=None):
116122
117123
"""
118124
# Process args
119-
assert orgId is None or isinstance(orgId, string_types)
125+
assert orgId is None or isinstance(orgId, str)
120126
assert max is None or isinstance(max, int)
121127
params = {}
122128
if orgId:
@@ -133,7 +139,7 @@ def get(self, licenseId):
133139
"""Get the details of a License, by id.
134140
135141
Args:
136-
licenseId(string_types): The id of the License.
142+
licenseId(str): The id of the License.
137143
138144
Returns:
139145
License: With the details of the requested License.
@@ -144,7 +150,7 @@ def get(self, licenseId):
144150
145151
"""
146152
# Process args
147-
assert isinstance(licenseId, string_types)
153+
assert isinstance(licenseId, str)
148154
# API request
149155
json_obj = self._session.get('licenses/' + licenseId)
150156
# Return a License object created from the returned JSON object

ciscosparkapi/api/memberships.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@
1010
"""
1111

1212

13-
from builtins import object
14-
from six import string_types
13+
# Use future for Python v2 and v3 compatibility
14+
from __future__ import (
15+
absolute_import,
16+
division,
17+
print_function,
18+
unicode_literals,
19+
)
20+
from builtins import *
1521

1622
from ciscosparkapi.exceptions import ciscosparkapiException
1723
from ciscosparkapi.utils import generator_container
@@ -32,7 +38,7 @@ def __init__(self, json):
3238
"""Init a new Membership data object from a JSON dictionary or string.
3339
3440
Args:
35-
json(dict, string_types): Input JSON object.
41+
json(dict, str): Input JSON object.
3642
3743
Raises:
3844
TypeError: If the input object is not a dictionary or string.
@@ -120,10 +126,10 @@ def list(self, roomId=None, personId=None, personEmail=None, max=None):
120126
container.
121127
122128
Args:
123-
roomId(string_types): List memberships for the room with roomId.
124-
personId(string_types): Filter results to include only those with
129+
roomId(str): List memberships for the room with roomId.
130+
personId(str): Filter results to include only those with
125131
personId.
126-
personEmail(string_types): Filter results to include only those
132+
personEmail(str): Filter results to include only those
127133
with personEmail.
128134
max(int): Limits the maximum number of memberships returned from
129135
the Spark service per request.
@@ -141,9 +147,9 @@ def list(self, roomId=None, personId=None, personEmail=None, max=None):
141147
142148
"""
143149
# Process args
144-
assert roomId is None or isinstance(roomId, string_types)
145-
assert personId is None or isinstance(personId, string_types)
146-
assert personEmail is None or isinstance(personEmail, string_types)
150+
assert roomId is None or isinstance(roomId, str)
151+
assert personId is None or isinstance(personId, str)
152+
assert personEmail is None or isinstance(personEmail, str)
147153
assert max is None or isinstance(max, int)
148154
params = {}
149155
if roomId:
@@ -174,10 +180,10 @@ def create(self, roomId, personId=None, personEmail=None,
174180
making them a moderator.
175181
176182
Args:
177-
roomId(string_types): ID of the room to which the person will be
183+
roomId(str): ID of the room to which the person will be
178184
added.
179-
personId(string_types): ID of the person to be added to the room.
180-
personEmail(string_types): Email address of the person to be added
185+
personId(str): ID of the person to be added to the room.
186+
personEmail(str): Email address of the person to be added
181187
to the room.
182188
isModerator(bool): If True, adds the person as a moderator for the
183189
room. If False, adds the person as normal member of the room.
@@ -193,9 +199,9 @@ def create(self, roomId, personId=None, personEmail=None,
193199
194200
"""
195201
# Process args
196-
assert isinstance(roomId, string_types)
197-
assert personId is None or isinstance(personId, string_types)
198-
assert personEmail is None or isinstance(personEmail, string_types)
202+
assert isinstance(roomId, str)
203+
assert personId is None or isinstance(personId, str)
204+
assert personEmail is None or isinstance(personEmail, str)
199205
assert isModerator is None or isinstance(isModerator, bool)
200206
post_data = {}
201207
post_data['roomId'] = roomId
@@ -217,7 +223,7 @@ def get(self, membershipId):
217223
"""Get details for a membership by ID.
218224
219225
Args:
220-
membershipId(string_types): The membershipId of the membership.
226+
membershipId(str): The membershipId of the membership.
221227
222228
Returns:
223229
Membership: With the details of the requested membership.
@@ -228,7 +234,7 @@ def get(self, membershipId):
228234
229235
"""
230236
# Process args
231-
assert isinstance(membershipId, string_types)
237+
assert isinstance(membershipId, str)
232238
# API request
233239
json_obj = self._session.get('memberships/' + membershipId)
234240
# Return a Membership object created from the response JSON data
@@ -238,7 +244,7 @@ def update(self, membershipId, **update_attributes):
238244
"""Update details for a membership.
239245
240246
Args:
241-
membershipId(string_types): The membershipId of the membership to
247+
membershipId(str): The membershipId of the membership to
242248
be updated.
243249
isModerator(bool): If True, sets the person as a moderator for the
244250
room. If False, removes the person as a moderator for the room.
@@ -253,7 +259,7 @@ def update(self, membershipId, **update_attributes):
253259
254260
"""
255261
# Process args
256-
assert isinstance(membershipId, string_types)
262+
assert isinstance(membershipId, str)
257263
# Process update_attributes keyword arguments
258264
if not update_attributes:
259265
error_message = "At least one **update_attributes keyword " \
@@ -269,7 +275,7 @@ def delete(self, membershipId):
269275
"""Delete a membership, by ID.
270276
271277
Args:
272-
membershipId(string_types): The membershipId of the membership to
278+
membershipId(str): The membershipId of the membership to
273279
be deleted.
274280
275281
Raises:
@@ -278,6 +284,6 @@ def delete(self, membershipId):
278284
279285
"""
280286
# Process args
281-
assert isinstance(membershipId, string_types)
287+
assert isinstance(membershipId, str)
282288
# API request
283289
self._session.delete('memberships/' + membershipId)

0 commit comments

Comments
 (0)