Skip to content

Commit f89b6f4

Browse files
committed
Refactor Memberships
Apply structural changes to the RoomsAPI and ensure all parameters are up-to-date.
1 parent a3e28e3 commit f89b6f4

File tree

1 file changed

+126
-118
lines changed

1 file changed

+126
-118
lines changed

ciscosparkapi/api/memberships.py

Lines changed: 126 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
Classes:
55
Membership: Models a Spark 'membership' JSON object as a native Python
66
object.
7-
MembershipsAPI: Wrappers the Cisco Spark Memberships-API and exposes the
8-
API calls as Python method calls that return native Python objects.
7+
MembershipsAPI: Wraps the Cisco Spark Memberships-API and exposes the
8+
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"
@@ -36,10 +39,10 @@ class Membership(SparkData):
3639
"""Model a Spark 'membership' JSON object as a native Python object."""
3740

3841
def __init__(self, json):
39-
"""Init a new Membership data object from a JSON dictionary or string.
42+
"""Initialize a Membership object from a dictionary or JSON string.
4043
4144
Args:
42-
json(dict, basestring): Input JSON object.
45+
json(dict, basestring): Input dictionary or JSON string.
4346
4447
Raises:
4548
TypeError: If the input object is not a dictionary or string.
@@ -49,42 +52,55 @@ def __init__(self, json):
4952

5053
@property
5154
def id(self):
52-
return self._json['id']
55+
"""The membership's unique ID."""
56+
return self._json.get('id')
5357

5458
@property
5559
def roomId(self):
56-
return self._json['roomId']
60+
"""The ID of the room."""
61+
return self._json.get('roomId')
5762

5863
@property
5964
def personId(self):
60-
return self._json['personId']
65+
"""The ID of the person."""
66+
return self._json.get('personId')
6167

6268
@property
6369
def personEmail(self):
64-
return self._json['personEmail']
70+
"""The email address of the person."""
71+
return self._json.get('personEmail')
6572

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

7083
@property
7184
def isModerator(self):
72-
return self._json['isModerator']
85+
"""Person is a moderator for the room."""
86+
return self._json.get('isModerator')
7387

7488
@property
7589
def isMonitor(self):
76-
return self._json['isMonitor']
90+
"""Person is a monitor for the room."""
91+
return self._json.get('isMonitor')
7792

7893
@property
7994
def created(self):
80-
return self._json['created']
95+
"""The date and time the membership was created."""
96+
return self._json.get('created')
8197

8298

8399
class MembershipsAPI(object):
84100
"""Cisco Spark Memberships-API wrapper class.
85101
86-
Wrappers the Cisco Spark Memberships-API and exposes the API calls as
87-
Python method calls that return native Python objects.
102+
Wraps the Cisco Spark Memberships-API and exposes the APIs as native Python
103+
methods that return native Python objects.
88104
89105
"""
90106

@@ -96,25 +112,28 @@ def __init__(self, session):
96112
API calls to the Cisco Spark service.
97113
98114
Raises:
99-
AssertionError: If the parameter types are incorrect.
115+
TypeError: If the parameter types are incorrect.
100116
101117
"""
102-
assert isinstance(session, RestSession)
118+
check_type(session, RestSession)
119+
103120
super(MembershipsAPI, self).__init__()
121+
104122
self._session = session
105123

106124
@generator_container
107-
def list(self, roomId=None, personId=None, personEmail=None, max=None):
125+
def list(self, roomId=None, personId=None, personEmail=None, max=None,
126+
**request_parameters):
108127
"""List room memberships.
109128
110-
By default, lists memberships for rooms to which the authenticated
111-
user belongs.
129+
By default, lists memberships for rooms to which the authenticated user
130+
belongs.
112131
113132
Use query parameters to filter the response.
114133
115-
Use roomId to list memberships for a room, by ID.
134+
Use `roomId` to list memberships for a room, by ID.
116135
117-
Use either personId or personEmail to filter the results.
136+
Use either `personId` or `personEmail` to filter the results.
118137
119138
This method supports Cisco Spark's implementation of RFC5988 Web
120139
Linking to provide pagination support. It returns a generator
@@ -127,164 +146,153 @@ def list(self, roomId=None, personId=None, personEmail=None, max=None):
127146
container.
128147
129148
Args:
130-
roomId(basestring): List memberships for the room with roomId.
131-
personId(basestring): Filter results to include only those with
132-
personId.
133-
personEmail(basestring): Filter results to include only those
134-
with personEmail.
135-
max(int): Limits the maximum number of memberships returned from
136-
the Spark service per request.
137-
149+
roomId(basestring): Limit results to a specific room, by ID.
150+
personId(basestring): Limit results to a specific person, by ID.
151+
personEmail(basestring): Limit results to a specific person, by
152+
email address.
153+
max(int): Limit the maximum number of items returned from the Spark
154+
service per request.
155+
**request_parameters: Additional request parameters (provides
156+
support for parameters that may be added in the future).
138157
139158
Returns:
140-
GeneratorContainer: When iterated, the GeneratorContainer, yields
141-
the memberships returned from the Cisco Spark query.
159+
GeneratorContainer: A GeneratorContainer which, when iterated,
160+
yields the memberships returned by the Cisco Spark query.
142161
143162
Raises:
144-
AssertionError: If the parameter types are incorrect.
145-
ciscosparkapiException: If a personId or personEmail argument is
146-
specified without providing a roomId argument.
163+
TypeError: If the parameter types are incorrect.
147164
SparkApiError: If the Cisco Spark cloud returns an error.
148165
149166
"""
150-
# Process args
151-
assert roomId is None or isinstance(roomId, basestring)
152-
assert personId is None or isinstance(personId, basestring)
153-
assert personEmail is None or isinstance(personEmail, basestring)
154-
assert max is None or isinstance(max, int)
155-
params = {}
156-
if roomId:
157-
params['roomId'] = roomId
158-
if personId:
159-
params['personId'] = personId
160-
elif personEmail:
161-
params['personEmail'] = personEmail
162-
elif personId or personEmail:
163-
error_message = "A roomId must be specified. A personId or " \
164-
"personEmail filter may only be specified when " \
165-
"requesting the memberships for a room with the " \
166-
"roomId argument."
167-
raise ciscosparkapiException(error_message)
168-
if max:
169-
params['max'] = max
167+
check_type(roomId, basestring)
168+
check_type(personId, basestring)
169+
check_type(personEmail, basestring)
170+
check_type(max, int)
171+
172+
params = dict_from_items_with_values(
173+
request_parameters,
174+
roomId=roomId,
175+
personId=personId,
176+
personEmail=personEmail,
177+
max=max,
178+
)
179+
170180
# API request - get items
171181
items = self._session.get_items('memberships', params=params)
182+
172183
# Yield Person objects created from the returned items JSON objects
173184
for item in items:
174185
yield Membership(item)
175186

176187
def create(self, roomId, personId=None, personEmail=None,
177-
isModerator=False):
188+
isModerator=False, **request_parameters):
178189
"""Add someone to a room by Person ID or email address.
179190
180191
Add someone to a room by Person ID or email address; optionally
181192
making them a moderator.
182193
183194
Args:
184-
roomId(basestring): ID of the room to which the person will be
185-
added.
186-
personId(basestring): ID of the person to be added to the room.
187-
personEmail(basestring): Email address of the person to be added
188-
to the room.
189-
isModerator(bool): If True, adds the person as a moderator for the
190-
room. If False, adds the person as normal member of the room.
195+
roomId(basestring): The room ID.
196+
personId(basestring): The ID of the person.
197+
personEmail(basestring): The email address of the person.
198+
isModerator(bool): Set to True to make the person a room moderator.
199+
**request_parameters: Additional request parameters (provides
200+
support for parameters that may be added in the future).
191201
192202
Returns:
193-
Membership: With the details of the created membership.
203+
Membership: A Membership object with the details of the created
204+
membership.
194205
195206
Raises:
196-
AssertionError: If the parameter types are incorrect.
197-
ciscosparkapiException: If neither a personId or personEmail are
198-
provided.
207+
TypeError: If the parameter types are incorrect.
199208
SparkApiError: If the Cisco Spark cloud returns an error.
200209
201210
"""
202-
# Process args
203-
assert isinstance(roomId, basestring)
204-
assert personId is None or isinstance(personId, basestring)
205-
assert personEmail is None or isinstance(personEmail, basestring)
206-
assert isModerator is None or isinstance(isModerator, bool)
207-
post_data = {}
208-
post_data['roomId'] = roomId
209-
if personId:
210-
post_data['personId'] = personId
211-
elif personEmail:
212-
post_data['personEmail'] = personEmail
213-
else:
214-
error_message = "personId or personEmail must be provided to " \
215-
"add a person to a room. Neither were provided."
216-
raise ciscosparkapiException(error_message)
217-
post_data['isModerator'] = isModerator
211+
check_type(roomId, basestring, may_be_none=False)
212+
check_type(personId, basestring)
213+
check_type(personEmail, basestring)
214+
check_type(isModerator, bool)
215+
216+
post_data = dict_from_items_with_values(
217+
request_parameters,
218+
roomId=roomId,
219+
personId=personId,
220+
personEmail=personEmail,
221+
isModerator=isModerator,
222+
)
223+
218224
# API request
219-
json_obj = self._session.post('memberships', json=post_data)
225+
json_data = self._session.post('memberships', json=post_data)
226+
220227
# Return a Membership object created from the response JSON data
221-
return Membership(json_obj)
228+
return Membership(json_data)
222229

223230
def get(self, membershipId):
224-
"""Get details for a membership by ID.
231+
"""Get details for a membership, by ID.
225232
226233
Args:
227-
membershipId(basestring): The membershipId of the membership.
234+
membershipId(basestring): The membership ID.
228235
229236
Returns:
230-
Membership: With the details of the requested membership.
237+
Membership: A Membership object with the details of the requested
238+
membership.
231239
232240
Raises:
233-
AssertionError: If the parameter types are incorrect.
241+
TypeError: If the parameter types are incorrect.
234242
SparkApiError: If the Cisco Spark cloud returns an error.
235243
236244
"""
237-
# Process args
238-
assert isinstance(membershipId, basestring)
245+
check_type(membershipId, basestring, may_be_none=False)
246+
239247
# API request
240-
json_obj = self._session.get('memberships/' + membershipId)
248+
json_data = self._session.get('memberships/' + membershipId)
249+
241250
# Return a Membership object created from the response JSON data
242-
return Membership(json_obj)
251+
return Membership(json_data)
243252

244-
def update(self, membershipId, **update_attributes):
245-
"""Update details for a membership.
253+
def update(self, membershipId, isModerator=None, **request_parameters):
254+
"""Updates properties for a membership, by ID.
246255
247256
Args:
248-
membershipId(basestring): The membershipId of the membership to
249-
be updated.
250-
isModerator(bool): If True, sets the person as a moderator for the
251-
room. If False, removes the person as a moderator for the room.
257+
membershipId(basestring): The membership ID.
258+
isModerator(bool): Set to True to make the person a room moderator.
252259
253260
Returns:
254-
Membership: With the updated Spark membership details.
261+
Membership: A Membership object with the updated Spark membership
262+
details.
255263
256264
Raises:
257-
AssertionError: If the parameter types are incorrect.
258-
ciscosparkapiException: If an update attribute is not provided.
265+
TypeError: If the parameter types are incorrect.
259266
SparkApiError: If the Cisco Spark cloud returns an error.
260267
261268
"""
262-
# Process args
263-
assert isinstance(membershipId, basestring)
264-
# Process update_attributes keyword arguments
265-
if not update_attributes:
266-
error_message = "At least one **update_attributes keyword " \
267-
"argument must be specified."
268-
raise ciscosparkapiException(error_message)
269+
check_type(membershipId, basestring, may_be_none=False)
270+
check_type(isModerator, bool)
271+
272+
put_data = dict_from_items_with_values(
273+
request_parameters,
274+
isModerator=isModerator,
275+
)
276+
269277
# API request
270-
json_obj = self._session.put('memberships/' + membershipId,
271-
json=update_attributes)
278+
json_data = self._session.put('memberships/' + membershipId,
279+
json=put_data)
280+
272281
# Return a Membership object created from the response JSON data
273-
return Membership(json_obj)
282+
return Membership(json_data)
274283

275284
def delete(self, membershipId):
276285
"""Delete a membership, by ID.
277286
278287
Args:
279-
membershipId(basestring): The membershipId of the membership to
280-
be deleted.
288+
membershipId(basestring): The membership ID.
281289
282290
Raises:
283-
AssertionError: If the parameter types are incorrect.
291+
TypeError: If the parameter types are incorrect.
284292
SparkApiError: If the Cisco Spark cloud returns an error.
285293
286294
"""
287-
# Process args
288-
assert isinstance(membershipId, basestring)
295+
check_type(membershipId, basestring)
296+
289297
# API request
290298
self._session.delete('memberships/' + membershipId)

0 commit comments

Comments
 (0)