Skip to content

Commit ab85067

Browse files
committed
Refactor TeamMemberships
Apply structural changes to the TeamMembershipsAPI and ensure all parameters are up-to-date.
1 parent b841f30 commit ab85067

File tree

1 file changed

+121
-102
lines changed

1 file changed

+121
-102
lines changed

ciscosparkapi/api/teammemberships.py

Lines changed: 121 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
Classes:
55
TeamMembership: Models a Spark 'team membership' JSON object as a native
66
Python object.
7-
TeamMembershipsAPI: Wrappers the Cisco Spark Memberships-API and exposes
8-
the API calls as Python method calls that return native Python objects.
7+
TeamMembershipsAPI: Wraps the Cisco Spark Memberships-API and exposes
8+
the APIs as native Python methods that return native Python objects.
99
1010
"""
1111

@@ -20,10 +20,13 @@
2020
from builtins import *
2121
from past.builtins import basestring
2222

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

2831

2932
__author__ = "Chris Lunsford"
@@ -37,10 +40,10 @@ class TeamMembership(SparkData):
3740
"""
3841

3942
def __init__(self, json):
40-
"""Init a new TeamMembership object from a JSON dictionary or string.
43+
"""Initialize a TeamMembership object from a dictionary or JSON string.
4144
4245
Args:
43-
json(dict, basestring): Input JSON object.
46+
json(dict, basestring): Input dictionary or JSON object.
4447
4548
Raises:
4649
TypeError: If the input object is not a dictionary or string.
@@ -50,38 +53,50 @@ def __init__(self, json):
5053

5154
@property
5255
def id(self):
53-
return self._json['id']
56+
"""The team membership's unique ID."""
57+
return self._json.get('id')
5458

5559
@property
5660
def teamId(self):
57-
return self._json['teamId']
61+
"""The ID of the team."""
62+
return self._json.get('teamId')
5863

5964
@property
6065
def personId(self):
61-
return self._json['personId']
66+
"""The ID of the person."""
67+
return self._json.get('personId')
6268

6369
@property
6470
def personEmail(self):
65-
return self._json['personEmail']
71+
"""The email address of the person."""
72+
return self._json.get('personEmail')
6673

6774
@property
6875
def personDisplayName(self):
69-
return self._json['personDisplayName']
76+
"""The display name of the person."""
77+
return self._json.get('personDisplayName')
78+
79+
@property
80+
def personOrgId(self):
81+
"""The ID of the organization that the person is associated with."""
82+
return self._json.get('personOrgId')
7083

7184
@property
7285
def isModerator(self):
73-
return self._json['isModerator']
86+
"""Person is a moderator for the team."""
87+
return self._json.get('isModerator')
7488

7589
@property
7690
def created(self):
77-
return self._json['created']
91+
"""The date and time the team membership was created."""
92+
return self._json.get('created')
7893

7994

8095
class TeamMembershipsAPI(object):
8196
"""Cisco Spark Team-Memberships-API wrapper class.
8297
83-
Wrappers the Cisco Spark Team-Memberships-API and exposes the API calls as
84-
Python method calls that return native Python objects.
98+
Wraps the Cisco Spark Memberships-API and exposes the APIs as native Python
99+
methods that return native Python objects.
85100
86101
"""
87102

@@ -93,168 +108,172 @@ def __init__(self, session):
93108
API calls to the Cisco Spark service.
94109
95110
Raises:
96-
AssertionError: If the parameter types are incorrect.
111+
TypeError: If the parameter types are incorrect.
97112
98113
"""
99-
assert isinstance(session, RestSession)
114+
check_type(session, RestSession)
115+
100116
super(TeamMembershipsAPI, self).__init__()
117+
101118
self._session = session
102119

103120
@generator_container
104-
def list(self, teamId, max=None):
121+
def list(self, teamId, max=None, **request_parameters):
105122
"""List team memberships for a team, by ID.
106123
107124
This method supports Cisco Spark's implementation of RFC5988 Web
108125
Linking to provide pagination support. It returns a generator
109-
container that incrementally yield all team memberships returned by the
110-
query. The generator will automatically request additional 'pages' of
111-
responses from Spark as needed until all responses have been returned.
112-
The container makes the generator safe for reuse. A new API call will
113-
be made, using the same parameters that were specified when the
114-
generator was created, every time a new iterator is requested from the
115-
container.
126+
container that incrementally yields all team memberships returned by
127+
the query. The generator will automatically request additional 'pages'
128+
of responses from Spark as needed until all responses have been
129+
returned. The container makes the generator safe for reuse. A new API
130+
call will be made, using the same parameters that were specified when
131+
the generator was created, every time a new iterator is requested from
132+
the container.
116133
117134
Args:
118-
teamId(basestring): List memberships for the team with teamId.
119-
max(int): Limits the maximum number of memberships returned from
120-
the Spark service per request.
121-
135+
teamId(basestring): List team memberships for a team, by ID.
136+
max(int): Limit the maximum number of items returned from the Spark
137+
service per request.
138+
**request_parameters: Additional request parameters (provides
139+
support for parameters that may be added in the future).
122140
123141
Returns:
124-
GeneratorContainer: When iterated, the GeneratorContainer, yields
125-
the team memberships returned by the Cisco Spark query.
142+
GeneratorContainer: A GeneratorContainer which, when iterated,
143+
yields the team memberships returned by the Cisco Spark query.
126144
127145
Raises:
128-
AssertionError: If the parameter types are incorrect.
146+
TypeError: If the parameter types are incorrect.
129147
SparkApiError: If the Cisco Spark cloud returns an error.
130148
131149
"""
132-
# Process args
133-
assert isinstance(teamId, basestring)
134-
assert max is None or isinstance(max, int)
135-
params = {}
136-
params['teamId'] = teamId
137-
if max:
138-
params['max'] = max
150+
check_type(teamId, basestring, may_be_none=False)
151+
check_type(max, int)
152+
153+
params = dict_from_items_with_values(
154+
request_parameters,
155+
teamId=teamId,
156+
max=max,
157+
)
158+
139159
# API request - get items
140160
items = self._session.get_items('team/memberships', params=params)
161+
141162
# Yield Person objects created from the returned items JSON objects
142163
for item in items:
143164
yield TeamMembership(item)
144165

145166
def create(self, teamId, personId=None, personEmail=None,
146-
isModerator=False):
167+
isModerator=False, **request_parameters):
147168
"""Add someone to a team by Person ID or email address.
148169
149170
Add someone to a team by Person ID or email address; optionally making
150171
them a moderator.
151172
152173
Args:
153-
teamId(basestring): ID of the team to which the person will be
154-
added.
155-
personId(basestring): ID of the person to be added to the team.
156-
personEmail(basestring): Email address of the person to be added
157-
to the team.
158-
isModerator(bool): If True, adds the person as a moderator for the
159-
team. If False, adds the person as normal member of the team.
174+
teamId(basestring): The team ID.
175+
personId(basestring): The person ID.
176+
personEmail(basestring): The email address of the person.
177+
isModerator(bool): Set to True to make the person a team moderator.
178+
**request_parameters: Additional request parameters (provides
179+
support for parameters that may be added in the future).
160180
161181
Returns:
162-
TeamMembership: With the details of the created team membership.
182+
TeamMembership: A TeamMembership object with the details of the
183+
created team membership.
163184
164185
Raises:
165-
AssertionError: If the parameter types are incorrect.
166-
ciscosparkapiException: If neither a personId or personEmail are
167-
provided.
186+
TypeError: If the parameter types are incorrect.
168187
SparkApiError: If the Cisco Spark cloud returns an error.
169188
170189
"""
171-
# Process args
172-
assert isinstance(teamId, basestring)
173-
assert personId is None or isinstance(personId, basestring)
174-
assert personEmail is None or isinstance(personEmail, basestring)
175-
assert isModerator is None or isinstance(isModerator, bool)
176-
post_data = {}
177-
post_data['teamId'] = teamId
178-
if personId:
179-
post_data['personId'] = personId
180-
elif personEmail:
181-
post_data['personEmail'] = personEmail
182-
else:
183-
error_message = "personId or personEmail must be provided to " \
184-
"add a person to a team. Neither were provided."
185-
raise ciscosparkapiException(error_message)
186-
post_data['isModerator'] = isModerator
190+
check_type(teamId, basestring, may_be_none=False)
191+
check_type(personId, basestring)
192+
check_type(personEmail, basestring)
193+
check_type(isModerator, bool)
194+
195+
post_data = dict_from_items_with_values(
196+
request_parameters,
197+
teamId=teamId,
198+
personId=personId,
199+
personEmail=personEmail,
200+
isModerator=isModerator,
201+
)
202+
187203
# API request
188-
json_obj = self._session.post('team/memberships', json=post_data)
204+
json_data = self._session.post('team/memberships', json=post_data)
205+
189206
# Return a TeamMembership object created from the response JSON data
190-
return TeamMembership(json_obj)
207+
return TeamMembership(json_data)
191208

192209
def get(self, membershipId):
193-
"""Get details for a team membership by ID.
210+
"""Get details for a team membership, by ID.
194211
195212
Args:
196-
membershipId(basestring): The membershipId of the team
197-
membership.
213+
membershipId(basestring): The team membership ID.
198214
199215
Returns:
200-
TeamMembership: With the details of the requested team membership.
216+
TeamMembership: A TeamMembership object with the details of the
217+
requested team membership.
201218
202219
Raises:
203-
AssertionError: If the parameter types are incorrect.
220+
TypeError: If the parameter types are incorrect.
204221
SparkApiError: If the Cisco Spark cloud returns an error.
205222
206223
"""
207-
# Process args
208-
assert isinstance(membershipId, basestring)
224+
check_type(membershipId, basestring, may_be_none=False)
225+
209226
# API request
210-
json_obj = self._session.get('team/memberships/' + membershipId)
227+
json_data = self._session.get('team/memberships/' + membershipId)
228+
211229
# Return a TeamMembership object created from the response JSON data
212-
return TeamMembership(json_obj)
230+
return TeamMembership(json_data)
213231

214-
def update(self, membershipId, **update_attributes):
215-
"""Update details for a team membership.
232+
def update(self, membershipId, isModerator=None, **request_parameters):
233+
"""Update a team membership, by ID.
216234
217235
Args:
218-
membershipId(basestring): The membershipId of the team membership
219-
to be updated.
220-
isModerator(bool): If True, sets the person as a moderator for the
221-
team. If False, removes the person as a moderator for the team.
236+
membershipId(basestring): The team membership ID.
237+
isModerator(bool): Set to True to make the person a team moderator.
238+
**request_parameters: Additional request parameters (provides
239+
support for parameters that may be added in the future).
222240
223241
Returns:
224-
TeamMembership: With the updated Spark team membership details.
242+
TeamMembership: A TeamMembership object with the updated Spark team
243+
membership details.
225244
226245
Raises:
227-
AssertionError: If the parameter types are incorrect.
228-
ciscosparkapiException: If an update attribute is not provided.
246+
TypeError: If the parameter types are incorrect.
229247
SparkApiError: If the Cisco Spark cloud returns an error.
230248
231249
"""
232-
# Process args
233-
assert isinstance(membershipId, basestring)
234-
# Process update_attributes keyword arguments
235-
if not update_attributes:
236-
error_message = "At least one **update_attributes keyword " \
237-
"argument must be specified."
238-
raise ciscosparkapiException(error_message)
250+
check_type(membershipId, basestring, may_be_none=False)
251+
check_type(isModerator, bool)
252+
253+
put_data = dict_from_items_with_values(
254+
request_parameters,
255+
isModerator=isModerator,
256+
)
257+
239258
# API request
240-
json_obj = self._session.put('team/memberships/' + membershipId,
241-
json=update_attributes)
259+
json_data = self._session.put('team/memberships/' + membershipId,
260+
json=put_data)
261+
242262
# Return a TeamMembership object created from the response JSON data
243-
return TeamMembership(json_obj)
263+
return TeamMembership(json_data)
244264

245265
def delete(self, membershipId):
246266
"""Delete a team membership, by ID.
247267
248268
Args:
249-
membershipId(basestring): The membershipId of the team membership
250-
to be deleted.
269+
membershipId(basestring): The team membership ID.
251270
252271
Raises:
253-
AssertionError: If the parameter types are incorrect.
272+
TypeError: If the parameter types are incorrect.
254273
SparkApiError: If the Cisco Spark cloud returns an error.
255274
256275
"""
257-
# Process args
258-
assert isinstance(membershipId, basestring)
276+
check_type(membershipId, basestring, may_be_none=False)
277+
259278
# API request
260279
self._session.delete('team/memberships/' + membershipId)

0 commit comments

Comments
 (0)