4
4
Classes:
5
5
Membership: Models a Spark 'membership' JSON object as a native Python
6
6
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.
9
9
10
10
"""
11
11
20
20
from builtins import *
21
21
from past .builtins import basestring
22
22
23
- from ciscosparkapi .exceptions import ciscosparkapiException
24
- from ciscosparkapi .utils import generator_container
25
23
from ciscosparkapi .restsession import RestSession
26
24
from ciscosparkapi .sparkdata import SparkData
25
+ from ciscosparkapi .utils import (
26
+ check_type ,
27
+ dict_from_items_with_values ,
28
+ generator_container ,
29
+ )
27
30
28
31
29
32
__author__ = "Chris Lunsford"
@@ -36,10 +39,10 @@ class Membership(SparkData):
36
39
"""Model a Spark 'membership' JSON object as a native Python object."""
37
40
38
41
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.
40
43
41
44
Args:
42
- json(dict, basestring): Input JSON object .
45
+ json(dict, basestring): Input dictionary or JSON string .
43
46
44
47
Raises:
45
48
TypeError: If the input object is not a dictionary or string.
@@ -49,42 +52,55 @@ def __init__(self, json):
49
52
50
53
@property
51
54
def id (self ):
52
- return self ._json ['id' ]
55
+ """The membership's unique ID."""
56
+ return self ._json .get ('id' )
53
57
54
58
@property
55
59
def roomId (self ):
56
- return self ._json ['roomId' ]
60
+ """The ID of the room."""
61
+ return self ._json .get ('roomId' )
57
62
58
63
@property
59
64
def personId (self ):
60
- return self ._json ['personId' ]
65
+ """The ID of the person."""
66
+ return self ._json .get ('personId' )
61
67
62
68
@property
63
69
def personEmail (self ):
64
- return self ._json ['personEmail' ]
70
+ """The email address of the person."""
71
+ return self ._json .get ('personEmail' )
65
72
66
73
@property
67
74
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' )
69
82
70
83
@property
71
84
def isModerator (self ):
72
- return self ._json ['isModerator' ]
85
+ """Person is a moderator for the room."""
86
+ return self ._json .get ('isModerator' )
73
87
74
88
@property
75
89
def isMonitor (self ):
76
- return self ._json ['isMonitor' ]
90
+ """Person is a monitor for the room."""
91
+ return self ._json .get ('isMonitor' )
77
92
78
93
@property
79
94
def created (self ):
80
- return self ._json ['created' ]
95
+ """The date and time the membership was created."""
96
+ return self ._json .get ('created' )
81
97
82
98
83
99
class MembershipsAPI (object ):
84
100
"""Cisco Spark Memberships-API wrapper class.
85
101
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.
88
104
89
105
"""
90
106
@@ -96,25 +112,28 @@ def __init__(self, session):
96
112
API calls to the Cisco Spark service.
97
113
98
114
Raises:
99
- AssertionError : If the parameter types are incorrect.
115
+ TypeError : If the parameter types are incorrect.
100
116
101
117
"""
102
- assert isinstance (session , RestSession )
118
+ check_type (session , RestSession )
119
+
103
120
super (MembershipsAPI , self ).__init__ ()
121
+
104
122
self ._session = session
105
123
106
124
@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 ):
108
127
"""List room memberships.
109
128
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.
112
131
113
132
Use query parameters to filter the response.
114
133
115
- Use roomId to list memberships for a room, by ID.
134
+ Use ` roomId` to list memberships for a room, by ID.
116
135
117
- Use either personId or personEmail to filter the results.
136
+ Use either ` personId` or ` personEmail` to filter the results.
118
137
119
138
This method supports Cisco Spark's implementation of RFC5988 Web
120
139
Linking to provide pagination support. It returns a generator
@@ -127,164 +146,153 @@ def list(self, roomId=None, personId=None, personEmail=None, max=None):
127
146
container.
128
147
129
148
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).
138
157
139
158
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.
142
161
143
162
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.
147
164
SparkApiError: If the Cisco Spark cloud returns an error.
148
165
149
166
"""
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
+
170
180
# API request - get items
171
181
items = self ._session .get_items ('memberships' , params = params )
182
+
172
183
# Yield Person objects created from the returned items JSON objects
173
184
for item in items :
174
185
yield Membership (item )
175
186
176
187
def create (self , roomId , personId = None , personEmail = None ,
177
- isModerator = False ):
188
+ isModerator = False , ** request_parameters ):
178
189
"""Add someone to a room by Person ID or email address.
179
190
180
191
Add someone to a room by Person ID or email address; optionally
181
192
making them a moderator.
182
193
183
194
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).
191
201
192
202
Returns:
193
- Membership: With the details of the created membership.
203
+ Membership: A Membership object with the details of the created
204
+ membership.
194
205
195
206
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.
199
208
SparkApiError: If the Cisco Spark cloud returns an error.
200
209
201
210
"""
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
+
218
224
# API request
219
- json_obj = self ._session .post ('memberships' , json = post_data )
225
+ json_data = self ._session .post ('memberships' , json = post_data )
226
+
220
227
# Return a Membership object created from the response JSON data
221
- return Membership (json_obj )
228
+ return Membership (json_data )
222
229
223
230
def get (self , membershipId ):
224
- """Get details for a membership by ID.
231
+ """Get details for a membership, by ID.
225
232
226
233
Args:
227
- membershipId(basestring): The membershipId of the membership.
234
+ membershipId(basestring): The membership ID .
228
235
229
236
Returns:
230
- Membership: With the details of the requested membership.
237
+ Membership: A Membership object with the details of the requested
238
+ membership.
231
239
232
240
Raises:
233
- AssertionError : If the parameter types are incorrect.
241
+ TypeError : If the parameter types are incorrect.
234
242
SparkApiError: If the Cisco Spark cloud returns an error.
235
243
236
244
"""
237
- # Process args
238
- assert isinstance ( membershipId , basestring )
245
+ check_type ( membershipId , basestring , may_be_none = False )
246
+
239
247
# API request
240
- json_obj = self ._session .get ('memberships/' + membershipId )
248
+ json_data = self ._session .get ('memberships/' + membershipId )
249
+
241
250
# Return a Membership object created from the response JSON data
242
- return Membership (json_obj )
251
+ return Membership (json_data )
243
252
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 .
246
255
247
256
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.
252
259
253
260
Returns:
254
- Membership: With the updated Spark membership details.
261
+ Membership: A Membership object with the updated Spark membership
262
+ details.
255
263
256
264
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.
259
266
SparkApiError: If the Cisco Spark cloud returns an error.
260
267
261
268
"""
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
+
269
277
# 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
+
272
281
# Return a Membership object created from the response JSON data
273
- return Membership (json_obj )
282
+ return Membership (json_data )
274
283
275
284
def delete (self , membershipId ):
276
285
"""Delete a membership, by ID.
277
286
278
287
Args:
279
- membershipId(basestring): The membershipId of the membership to
280
- be deleted.
288
+ membershipId(basestring): The membership ID.
281
289
282
290
Raises:
283
- AssertionError : If the parameter types are incorrect.
291
+ TypeError : If the parameter types are incorrect.
284
292
SparkApiError: If the Cisco Spark cloud returns an error.
285
293
286
294
"""
287
- # Process args
288
- assert isinstance ( membershipId , basestring )
295
+ check_type ( membershipId , basestring )
296
+
289
297
# API request
290
298
self ._session .delete ('memberships/' + membershipId )
0 commit comments