3
3
4
4
Classes:
5
5
Person: Models a Spark 'person' JSON object as a native Python object.
6
- PeopleAPI: Wrappers the Cisco Spark People-API and exposes the API calls as
7
- Python method calls that return native Python objects.
6
+ PeopleAPI: Wraps the Cisco Spark People-API and exposes the APIs as native
7
+ Python methods that return native Python objects.
8
8
9
9
"""
10
10
19
19
from builtins import *
20
20
from past .builtins import basestring
21
21
22
- from ciscosparkapi .exceptions import ciscosparkapiException
23
- from ciscosparkapi .utils import generator_container
22
+ from ciscosparkapi .utils import (
23
+ check_type ,
24
+ dict_from_items_with_values ,
25
+ generator_container ,
26
+ )
24
27
from ciscosparkapi .restsession import RestSession
25
28
from ciscosparkapi .sparkdata import SparkData
26
29
32
35
33
36
34
37
class Person (SparkData ):
35
- """Model a Spark ' person' JSON object as a native Python object."""
38
+ """Model a Spark person JSON object as a native Python object."""
36
39
37
40
def __init__ (self , json ):
38
41
"""Initialize a Person data object from a dictionary or JSON string.
@@ -58,13 +61,7 @@ def id(self):
58
61
59
62
@property
60
63
def emails (self ):
61
- """Email address(es) of the person.
62
-
63
- CURRENT LIMITATION: Spark (today) only allows you to provide a single
64
- email address for a person. The list data type was selected to enable
65
- future support for providing multiple email address.
66
-
67
- """
64
+ """Email address(es) of the person."""
68
65
return self ._json ['emails' ]
69
66
70
67
@property
@@ -149,20 +146,23 @@ def __init__(self, session):
149
146
API calls to the Cisco Spark service.
150
147
151
148
Raises:
152
- AssertionError : If the parameter types are incorrect.
149
+ TypeError : If the parameter types are incorrect.
153
150
154
151
"""
155
- assert isinstance (session , RestSession )
152
+ check_type (session , RestSession )
153
+
156
154
super (PeopleAPI , self ).__init__ ()
155
+
157
156
self ._session = session
158
157
159
158
@generator_container
160
- def list (self , email = None , displayName = None , orgId = None , id = None , max = None , ** query_params ):
159
+ def list (self , email = None , displayName = None , id = None , orgId = None , max = None ,
160
+ ** query_params ):
161
161
"""List people
162
162
163
163
This method supports Cisco Spark's implementation of RFC5988 Web
164
164
Linking to provide pagination support. It returns a generator
165
- container that incrementally yield all people returned by the
165
+ container that incrementally yields all people returned by the
166
166
query. The generator will automatically request additional 'pages' of
167
167
responses from Spark as needed until all responses have been returned.
168
168
The container makes the generator safe for reuse. A new API call will
@@ -174,190 +174,218 @@ def list(self, email=None, displayName=None, orgId=None, id=None, max=None, **qu
174
174
email(basestring): The e-mail address of the person to be found.
175
175
displayName(basestring): The complete or beginning portion of
176
176
the displayName to be searched.
177
- id(basestring): List people by ID. Accepts up to 85 person IDs separated by commas.
177
+ id(basestring): List people by ID. Accepts up to 85 person IDs
178
+ separated by commas.
178
179
orgId(basestring): The organization id.
179
180
max(int): Limits the maximum number of people returned from the
180
181
Spark service per request.
182
+ **query_params: Additional query parameters (provides support for
183
+ query parameters that may be added in the future).
181
184
182
185
Returns:
183
- GeneratorContainer: When iterated, the GeneratorContainer, yields
184
- the people returned by the Cisco Spark query.
186
+ GeneratorContainer: A GeneratorContainer which, when iterated,
187
+ yields the people returned by the Cisco Spark query.
185
188
186
189
Raises:
187
- AssertionError : If the parameter types are incorrect.
190
+ TypeError : If the parameter types are incorrect.
188
191
SparkApiError: If the Cisco Spark cloud returns an error.
189
192
190
193
"""
191
- # Process args
192
- assert email is None or isinstance (email , basestring )
193
- assert displayName is None or isinstance (displayName , basestring )
194
- assert orgId is None or isinstance (orgId , basestring )
195
- assert id is None or isinstance (id , basestring )
196
- assert max is None or isinstance (max , int )
197
- params = {}
198
- if id :
199
- params ["id" ] = id
200
- elif email :
201
- params ['email' ] = email
202
- elif displayName :
203
- params ['displayName' ] = displayName
204
- if orgId :
205
- params ["orgId" ] = orgId
206
- if max :
207
- params ['max' ] = max
208
- # Process query_param keyword arguments
209
- if query_params :
210
- params .update (query_params )
194
+ check_type (id , basestring )
195
+ check_type (email , basestring )
196
+ check_type (displayName , basestring )
197
+ check_type (orgId , basestring )
198
+ check_type (max , int )
199
+
200
+ params = dict_from_items_with_values (
201
+ query_params ,
202
+ id = id ,
203
+ email = email ,
204
+ displayName = displayName ,
205
+ orgId = orgId ,
206
+ max = max ,
207
+ )
208
+
211
209
# API request - get items
212
210
items = self ._session .get_items ('people' , params = params )
211
+
213
212
# Yield Person objects created from the returned items JSON objects
214
213
for item in items :
215
214
yield Person (item )
216
215
217
- def create (self , emails , ** person_attributes ):
216
+ def create (self , emails , displayName = None , firstName = None , lastName = None ,
217
+ avatar = None , orgId = None , roles = None , licenses = None ,
218
+ ** person_attributes ):
218
219
"""Create a new user account for a given organization
219
220
220
221
Only an admin can create a new user account.
221
222
222
- You must specify displayName and/or firstName and lastName.
223
-
224
223
Args:
225
- emails(list): Email address(es) of the person. (list of strings)
226
- CURRENT LIMITATION: Spark (today) only allows you to provide a
227
- single email address for a person. The list data type was
228
- selected to enable future support for providing multiple email
229
- address.
230
- **person_attributes
231
- displayName(basestring): Full name of the person
232
- firstName(basestring): First name of the person
233
- lastName(basestring): Last name of the person
234
- avatar(basestring): URL to the person's avatar in PNG format
224
+ emails(list): Email address(es) of the person (list of strings).
225
+ displayName(basestring): Full name of the person.
226
+ firstName(basestring): First name of the person.
227
+ lastName(basestring): Last name of the person.
228
+ avatar(basestring): URL to the person's avatar in PNG format.
235
229
orgId(basestring): ID of the organization to which this
236
- person belongs
230
+ person belongs.
237
231
roles(list): Roles of the person (list of strings containing
238
- the role IDs to be assigned to the person)
232
+ the role IDs to be assigned to the person).
239
233
licenses(list): Licenses allocated to the person (list of
240
- strings containing the license IDs to be allocated to the
241
- person)
234
+ strings - containing the license IDs to be allocated to the
235
+ person).
236
+ **person_attributes: Additional person attributes (provides support
237
+ for attributes that may be added in the future).
242
238
243
239
Returns:
244
- Person: With the details of the created person.
240
+ Person: A Person object with the details of the created person.
245
241
246
242
Raises:
247
- AssertionError: If the parameter types are incorrect.
248
- ciscosparkapiException: If required parameters have been omitted.
243
+ TypeError: If the parameter types are incorrect.
249
244
SparkApiError: If the Cisco Spark cloud returns an error.
250
245
251
246
"""
252
- # Process args
253
- assert isinstance (emails , list ) and len (emails ) == 1
254
- post_data = {}
255
- post_data ['emails' ] = emails
256
- post_data .update (person_attributes )
247
+ check_type (emails , list , may_be_none = False )
248
+ check_type (displayName , basestring )
249
+ check_type (firstName , basestring )
250
+ check_type (lastName , basestring )
251
+ check_type (avatar , basestring )
252
+ check_type (orgId , basestring )
253
+ check_type (roles , list )
254
+ check_type (licenses , list )
255
+
256
+ post_data = dict_from_items_with_values (
257
+ person_attributes ,
258
+ emails = emails ,
259
+ displayName = displayName ,
260
+ firstName = firstName ,
261
+ lastName = lastName ,
262
+ avatar = avatar ,
263
+ orgId = orgId ,
264
+ roles = roles ,
265
+ licenses = licenses ,
266
+ )
257
267
258
268
# API request
259
- json_obj = self ._session .post ('people' , json = post_data )
269
+ json_data = self ._session .post ('people' , json = post_data )
260
270
261
271
# Return a Room object created from the returned JSON object
262
- return Person (json_obj )
272
+ return Person (json_data )
263
273
264
- def update (self , personId , ** person_attributes ):
274
+ def update (self , personId , emails = None , displayName = None , firstName = None ,
275
+ lastName = None , avatar = None , orgId = None , roles = None ,
276
+ licenses = None , ** person_attributes ):
265
277
"""Update details for a person, by ID.
266
278
267
- Only an admin can update a person details.
279
+ Only an admin can update a person's details.
280
+
281
+ Email addresses for a person cannot be changed via the Spark API.
282
+
283
+ Include all details for the person. This action expects all user
284
+ details to be present in the request. A common approach is to first GET
285
+ the person's details, make changes, then PUT both the changed and
286
+ unchanged values.
268
287
269
288
Args:
270
- personId(basestring): The ID of the person to be updated.
271
- **person_attributes
272
- emails(list): Email address(es) of the person. (list of
273
- strings) CURRENT LIMITATION: Spark (today) only allows you
274
- to provide a single email address for a person. The list
275
- data type was selected to enable future support for
276
- providing multiple email address.
277
- displayName(basestring): Full name of the person
278
- firstName(basestring): First name of the person
279
- lastName(basestring): Last name of the person
280
- avatar(basestring): URL to the person's avatar in PNG format
289
+ personId(basestring): The 'id' of the person to be updated.
290
+ emails(list): Email address(es) of the person (list of strings).
291
+ displayName(basestring): Full name of the person.
292
+ firstName(basestring): First name of the person.
293
+ lastName(basestring): Last name of the person.
294
+ avatar(basestring): URL to the person's avatar in PNG format.
281
295
orgId(basestring): ID of the organization to which this
282
- person belongs
296
+ person belongs.
283
297
roles(list): Roles of the person (list of strings containing
284
- the role IDs to be assigned to the person)
298
+ the role IDs to be assigned to the person).
285
299
licenses(list): Licenses allocated to the person (list of
286
- strings containing the license IDs to be allocated to the
287
- person)
300
+ strings - containing the license IDs to be allocated to the
301
+ person).
302
+ **person_attributes: Additional person attributes (provides support
303
+ for attributes that may be added in the future).
288
304
289
305
Returns:
290
- Person: With the updated person details.
306
+ Person: A Person object with the updated details.
291
307
292
308
Raises:
293
- AssertionError: If the parameter types are incorrect.
294
- ciscosparkapiException: If an update attribute is not provided.
309
+ TypeError: If the parameter types are incorrect.
295
310
SparkApiError: If the Cisco Spark cloud returns an error.
296
311
297
312
"""
298
- # Process args
299
- assert isinstance (personId , basestring )
300
-
301
- # Process update_attributes keyword arguments
302
- if not person_attributes :
303
- error_message = "At least one **update_attributes keyword " \
304
- "argument must be specified."
305
- raise ciscosparkapiException (error_message )
313
+ check_type (emails , list )
314
+ check_type (displayName , basestring )
315
+ check_type (firstName , basestring )
316
+ check_type (lastName , basestring )
317
+ check_type (avatar , basestring )
318
+ check_type (orgId , basestring )
319
+ check_type (roles , list )
320
+ check_type (licenses , list )
321
+
322
+ put_data = dict_from_items_with_values (
323
+ person_attributes ,
324
+ emails = emails ,
325
+ displayName = displayName ,
326
+ firstName = firstName ,
327
+ lastName = lastName ,
328
+ avatar = avatar ,
329
+ orgId = orgId ,
330
+ roles = roles ,
331
+ licenses = licenses ,
332
+ )
306
333
307
334
# API request
308
- json_obj = self ._session .put ('people/' + personId ,
309
- json = person_attributes )
335
+ json_data = self ._session .put ('people/' + personId , json = put_data )
310
336
311
337
# Return a Person object created from the returned JSON object
312
- return Person (json_obj )
338
+ return Person (json_data )
313
339
314
340
def get (self , personId ):
315
341
"""Get person details, by personId.
316
342
317
343
Args:
318
- personId(basestring): The personID of the person.
344
+ personId(basestring): The 'id' of the person to be retrieved .
319
345
320
346
Returns:
321
- Person: With the details of the requested person.
347
+ Person: A Person object with the details of the requested person.
322
348
323
349
Raises:
324
- AssertionError : If the parameter types are incorrect.
350
+ TypeError : If the parameter types are incorrect.
325
351
SparkApiError: If the Cisco Spark cloud returns an error.
326
352
327
353
"""
328
- # Process args
329
- assert isinstance ( personId , basestring )
354
+ check_type ( personId , basestring , may_be_none = False )
355
+
330
356
# API request
331
- json_obj = self ._session .get ('people/' + personId )
357
+ json_data = self ._session .get ('people/' + personId )
358
+
332
359
# Return a Person object created from the response JSON data
333
- return Person (json_obj )
360
+ return Person (json_data )
334
361
335
362
def delete (self , personId ):
336
363
"""Remove a person from the system.
337
364
338
365
Only an admin can remove a person.
339
366
340
367
Args:
341
- personId(basestring): The personID of the person.
368
+ personId(basestring): The 'id' of the person to be deleted .
342
369
343
370
Raises:
344
371
AssertionError: If the parameter types are incorrect.
345
372
SparkApiError: If the Cisco Spark cloud returns an error.
346
373
347
374
"""
348
- # Process args
349
- assert isinstance ( personId , basestring )
375
+ check_type ( personId , basestring , may_be_none = False )
376
+
350
377
# API request
351
378
self ._session .delete ('people/' + personId )
352
379
353
380
def me (self ):
354
- """Get the person details of the account accessing the API 'me' .
381
+ """Get the details of the person accessing the API.
355
382
356
383
Raises:
357
384
SparkApiError: If the Cisco Spark cloud returns an error.
358
385
359
386
"""
360
387
# API request
361
- json_obj = self ._session .get ('people/me' )
388
+ json_data = self ._session .get ('people/me' )
389
+
362
390
# Return a Person object created from the response JSON data
363
- return Person (json_obj )
391
+ return Person (json_data )
0 commit comments