Skip to content

Commit fd70b4a

Browse files
committed
Refactor Teams
Apply structural changes to the TeamsAPI and ensure all parameters are up-to-date.
1 parent d294c0d commit fd70b4a

File tree

1 file changed

+89
-62
lines changed

1 file changed

+89
-62
lines changed

ciscosparkapi/api/teams.py

Lines changed: 89 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
44
Classes:
55
Team: Models a Spark 'team' JSON object as a native Python object.
6-
TeamsAPI: Wrappers the Cisco Spark Teams-API and exposes the API calls as
7-
Python method calls that return native Python objects.
6+
TeamsAPI: Wraps the Cisco Spark Teams-API and exposes the APIs as native
7+
Python methods that return native Python objects.
88
99
"""
1010

@@ -19,10 +19,13 @@
1919
from builtins import *
2020
from past.builtins import basestring
2121

22-
from ciscosparkapi.exceptions import ciscosparkapiException
23-
from ciscosparkapi.utils import generator_container
2422
from ciscosparkapi.restsession import RestSession
2523
from ciscosparkapi.sparkdata import SparkData
24+
from ciscosparkapi.utils import (
25+
check_type,
26+
dict_from_items_with_values,
27+
generator_container,
28+
)
2629

2730

2831
__author__ = "Chris Lunsford"
@@ -35,10 +38,10 @@ class Team(SparkData):
3538
"""Model a Spark 'team' JSON object as a native Python object."""
3639

3740
def __init__(self, json):
38-
"""Init a new Team data object from a JSON dictionary or string.
41+
"""Initialize a new Team data object from a dictionary or JSON string.
3942
4043
Args:
41-
json(dict, basestring): Input JSON object.
44+
json(dict, basestring): Input dictionary or JSON object.
4245
4346
Raises:
4447
TypeError: If the input object is not a dictionary or string.
@@ -48,47 +51,57 @@ def __init__(self, json):
4851

4952
@property
5053
def id(self):
51-
return self._json['id']
54+
"""The team's unique ID."""
55+
return self._json.get('id')
5256

5357
@property
5458
def name(self):
55-
return self._json['name']
59+
"""A user-friendly name for the team."""
60+
return self._json.get('name')
5661

5762
@property
5863
def created(self):
59-
return self._json['created']
64+
"""The date and time the team was created."""
65+
return self._json.get('created')
66+
67+
@property
68+
def creatorId(self):
69+
"""The ID of the person who created the team."""
70+
return self._json.get('creatorId')
6071

6172

6273
class TeamsAPI(object):
6374
"""Cisco Spark Teams-API wrapper class.
6475
65-
Wrappers the Cisco Spark Teams-API and exposes the API calls as Python
66-
method calls that return native Python objects.
76+
Wraps the Cisco Spark Teams-API and exposes the APIs as native Python
77+
methods that return native Python objects.
6778
6879
"""
6980

7081
def __init__(self, session):
71-
"""Init a new TeamsAPI object with the provided RestSession.
82+
"""Initialize a new TeamsAPI object with the provided RestSession.
7283
7384
Args:
7485
session(RestSession): The RESTful session object to be used for
7586
API calls to the Cisco Spark service.
7687
7788
Raises:
78-
AssertionError: If the parameter types are incorrect.
89+
TypeError: If the parameter types are incorrect.
7990
8091
"""
81-
assert isinstance(session, RestSession)
92+
check_type(session, RestSession, may_be_none=False)
93+
8294
super(TeamsAPI, self).__init__()
95+
8396
self._session = session
8497

8598
@generator_container
86-
def list(self, max=None):
99+
def list(self, max=None, **request_parameters):
87100
"""List teams to which the authenticated user belongs.
88101
89102
This method supports Cisco Spark's implementation of RFC5988 Web
90103
Linking to provide pagination support. It returns a generator
91-
container that incrementally yield all teams returned by the
104+
container that incrementally yields all teams returned by the
92105
query. The generator will automatically request additional 'pages' of
93106
responses from Spark as needed until all responses have been returned.
94107
The container makes the generator safe for reuse. A new API call will
@@ -97,116 +110,130 @@ def list(self, max=None):
97110
container.
98111
99112
Args:
100-
max(int): Limits the maximum number of teams returned from the
101-
Spark service per request.
113+
max(int): Limit the maximum number of items returned from the Spark
114+
service per request.
115+
**request_parameters: Additional request parameters (provides
116+
support for parameters that may be added in the future).
102117
103118
Returns:
104-
GeneratorContainer: When iterated, the GeneratorContainer, yields
105-
the teams returned by the Cisco Spark query.
119+
GeneratorContainer: A GeneratorContainer which, when iterated,
120+
yields the teams returned by the Cisco Spark query.
106121
107122
Raises:
108-
AssertionError: If the parameter types are incorrect.
123+
TypeError: If the parameter types are incorrect.
109124
SparkApiError: If the Cisco Spark cloud returns an error.
110125
111126
"""
112-
# Process args
113-
assert max is None or isinstance(max, int)
114-
params = {}
115-
if max:
116-
params['max'] = max
127+
check_type(max, int)
128+
129+
params = dict_from_items_with_values(
130+
request_parameters,
131+
max=max,
132+
)
133+
117134
# API request - get items
118135
items = self._session.get_items('teams', params=params)
136+
119137
# Yield Team objects created from the returned items JSON objects
120138
for item in items:
121139
yield Team(item)
122140

123-
def create(self, name):
141+
def create(self, name, **request_parameters):
124142
"""Create a team.
125143
126144
The authenticated user is automatically added as a member of the team.
127145
128146
Args:
129147
name(basestring): A user-friendly name for the team.
148+
**request_parameters: Additional request parameters (provides
149+
support for parameters that may be added in the future).
130150
131151
Returns:
132-
Team: With the details of the created team.
152+
Team: A Team object with the details of the created team.
133153
134154
Raises:
135-
AssertionError: If the parameter types are incorrect.
155+
TypeError: If the parameter types are incorrect.
136156
SparkApiError: If the Cisco Spark cloud returns an error.
137157
138158
"""
139-
# Process args
140-
assert isinstance(name, basestring)
141-
post_data = {}
142-
post_data['name'] = name
159+
check_type(name, basestring, may_be_none=False)
160+
161+
post_data = dict_from_items_with_values(
162+
request_parameters,
163+
name=name,
164+
)
165+
143166
# API request
144-
json_obj = self._session.post('teams', json=post_data)
167+
json_data = self._session.post('teams', json=post_data)
168+
145169
# Return a Team object created from the response JSON data
146-
return Team(json_obj)
170+
return Team(json_data)
147171

148172
def get(self, teamId):
149173
"""Get the details of a team, by ID.
150174
151175
Args:
152-
teamId(basestring): The teamId of the team.
176+
teamId(basestring): The ID of the team to be retrieved.
153177
154178
Returns:
155-
Team: With the details of the requested team.
179+
Team: A Team object with the details of the requested team.
156180
157181
Raises:
158-
AssertionError: If the parameter types are incorrect.
182+
TypeError: If the parameter types are incorrect.
159183
SparkApiError: If the Cisco Spark cloud returns an error.
160184
161185
"""
162-
# Process args
163-
assert isinstance(teamId, basestring)
186+
check_type(teamId, basestring, may_be_none=False)
187+
164188
# API request
165-
json_obj = self._session.get('teams/' + teamId)
189+
json_data = self._session.get('teams/' + teamId)
190+
166191
# Return a Team object created from the response JSON data
167-
return Team(json_obj)
192+
return Team(json_data)
168193

169-
def update(self, teamId, **update_attributes):
170-
"""Update details for a team.
194+
def update(self, teamId, name=None, **request_parameters):
195+
"""Update details for a team, by ID.
171196
172197
Args:
173-
teamId(basestring): The teamId of the team to be updated.
198+
teamId(basestring): The team ID.
174199
name(basestring): A user-friendly name for the team.
200+
**request_parameters: Additional request parameters (provides
201+
support for parameters that may be added in the future).
175202
176203
Returns:
177-
Team: With the updated Spark team details.
204+
Team: A Team object with the updated Spark team details.
178205
179206
Raises:
180-
AssertionError: If the parameter types are incorrect.
181-
ciscosparkapiException: If an update attribute is not provided.
207+
TypeError: If the parameter types are incorrect.
182208
SparkApiError: If the Cisco Spark cloud returns an error.
183209
184210
"""
185-
# Process args
186-
assert isinstance(teamId, basestring)
187-
# Process update_attributes keyword arguments
188-
if not update_attributes:
189-
error_message = "At least one **update_attributes keyword " \
190-
"argument must be specified."
191-
raise ciscosparkapiException(error_message)
211+
check_type(teamId, basestring, may_be_none=False)
212+
check_type(name, basestring)
213+
214+
put_data = dict_from_items_with_values(
215+
request_parameters,
216+
name=name,
217+
)
218+
192219
# API request
193-
json_obj = self._session.put('teams/' + teamId,
194-
json=update_attributes)
220+
json_data = self._session.put('teams/' + teamId, json=put_data)
221+
195222
# Return a Team object created from the response JSON data
196-
return Team(json_obj)
223+
return Team(json_data)
197224

198225
def delete(self, teamId):
199226
"""Delete a team.
200227
201228
Args:
202-
teamId(basestring): The teamId of the team to be deleted.
229+
teamId(basestring): The ID of the team to be deleted.
203230
204231
Raises:
205-
AssertionError: If the parameter types are incorrect.
232+
TypeError: If the parameter types are incorrect.
206233
SparkApiError: If the Cisco Spark cloud returns an error.
207234
208235
"""
209-
# Process args
210-
assert isinstance(teamId, basestring)
236+
check_type(teamId, basestring, may_be_none=False)
237+
211238
# API request
212239
self._session.delete('teams/' + teamId)

0 commit comments

Comments
 (0)