Skip to content

Commit 6850a43

Browse files
committed
Initial PeopleAPI wrapper
Add wrapper classes for the Cisco Spark People-API.
1 parent 393ff34 commit 6850a43

File tree

2 files changed

+179
-4
lines changed

2 files changed

+179
-4
lines changed

ciscosparkapi/__init__.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from exceptions import ciscosparkapiException, SparkApiError
22
from restsession import RestSession
33
from api.rooms import Room, RoomsAPI
4+
from api.people import Person, PeopleAPI
45

56

67
class CiscoSparkAPI(object):
@@ -13,12 +14,16 @@ def __init__(self, access_token, base_url=None, timeout=None):
1314
session_args = {}
1415
if base_url: session_args['base_url'] = base_url
1516
if timeout: session_args['timeout'] = timeout
16-
# Create API session - All of the API calls associated with a
17-
# CiscoSparkAPI object will leverage a single RESTful 'session'
18-
# connecting to the Cisco Spark cloud.
17+
18+
# Create API session
19+
# All of the API calls associated with a CiscoSparkAPI object will
20+
# leverage a single RESTful 'session' connecting to the Cisco Spark
21+
# cloud.
1922
self.session = RestSession(access_token, **session_args)
20-
# Setup Spark API wrappers
23+
24+
# Spark API wrappers
2125
self.rooms = RoomsAPI(self.session)
26+
self.people = PeopleAPI(self.session)
2227

2328
@property
2429
def access_token(self):

ciscosparkapi/api/people.py

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
"""Cisco Spark People-API wrapper classes.
2+
3+
Classes:
4+
Person: Models a Spark 'person' JSON object as a native Python object.
5+
PeopleAPI: Wrappers the Cisco Spark People-API and exposes the API calls as
6+
Python method calls that return native Python objects.
7+
8+
"""
9+
10+
11+
from ciscosparkapi.exceptions import ciscosparkapiException
12+
from ciscosparkapi.helper import utf8, generator_container
13+
from ciscosparkapi.restsession import RestSession
14+
from ciscosparkapi.sparkdata import SparkData
15+
16+
17+
class Person(SparkData):
18+
"""Model a Spark 'person' JSON object as a native Python object."""
19+
20+
def __init__(self, json):
21+
"""Init a new Person data object from a JSON dictionary or string.
22+
23+
Args:
24+
json(dict, unicode, str): Input JSON object.
25+
26+
Raises:
27+
TypeError: If the input object is not a dictionary or string.
28+
29+
"""
30+
super(Person, self).__init__(json)
31+
32+
@property
33+
def id(self):
34+
return self._json[u'id']
35+
36+
@property
37+
def emails(self):
38+
return self._json[u'emails']
39+
40+
@property
41+
def displayName(self):
42+
return self._json[u'displayName']
43+
44+
@property
45+
def avatar(self):
46+
return self._json[u'avatar']
47+
48+
@property
49+
def created(self):
50+
return self._json[u'created']
51+
52+
@property
53+
def lastActivity(self):
54+
return self._json[u'lastActivity']
55+
56+
@property
57+
def status(self):
58+
return self._json[u'status']
59+
60+
61+
class PeopleAPI(object):
62+
"""Cisco Spark People-API wrapper class.
63+
64+
Wrappers the Cisco Spark People-API and exposes the API calls as Python
65+
method calls that return native Python objects.
66+
67+
Attributes:
68+
session(RestSession): The RESTful session object to be used for API
69+
calls to the Cisco Spark service.
70+
71+
"""
72+
73+
def __init__(self, session):
74+
"""Init a new PeopleAPI object with the provided RestSession.
75+
76+
Args:
77+
session(RestSession): The RESTful session object to be used for
78+
API calls to the Cisco Spark service.
79+
80+
Raises:
81+
AssertionError: If the parameter types are incorrect.
82+
83+
"""
84+
assert isinstance(session, RestSession)
85+
super(PeopleAPI, self).__init__()
86+
self.session = session
87+
88+
@generator_container
89+
def list(self, email=None, displayName=None, max=None):
90+
"""List people by email or displayName.
91+
92+
An email address or displayName must be provided.
93+
94+
This method supports Cisco Spark's implmentation of RFC5988 Web Linking
95+
to provide pagination support. It returns a generator container that
96+
incrementally yield all people returned by the query. The generator
97+
will automatically request additional 'pages' of responses from Spark
98+
as needed until all responses have been returned. The container makes
99+
the generator safe for reuse. A new API call will be made, using the
100+
same parameters that were specified when the generator was created,
101+
every time a new iterator is requested from the container.
102+
103+
Args:
104+
email(unicode, str): The e-mail address of the person to be found.
105+
displayName(unicode, str): The complete or beginning portion of
106+
the displayName to be searched.
107+
max(int): Limits the maximum number of people returned from the
108+
Spark service per request.
109+
110+
Yields:
111+
Person: The the next person from the Cisco Spark query.
112+
113+
Raises:
114+
AssertionError: If the parameter types are incorrect.
115+
ciscosparkapiException: If neither an email or displayName argument
116+
is specified.
117+
SparkApiError: If the Cisco Spark cloud returns an error.
118+
119+
"""
120+
# Process args
121+
assert email is None or isinstance(email, basestring)
122+
assert displayName is None or isinstance(displayName, basestring)
123+
assert max is None or isinstance(max, int)
124+
params = {}
125+
if email:
126+
params[u'email'] = email
127+
elif displayName:
128+
params[u'displayName'] = displayName
129+
else:
130+
error_message = "An email or displayName argument must be " \
131+
"specified."
132+
raise ciscosparkapiException(error_message)
133+
if max:
134+
params[u'max'] = max
135+
# API request - get items
136+
items = self.session.get_items('people', params=params)
137+
# Yield Person objects created from the returned items JSON objects
138+
for item in items:
139+
yield Person(item)
140+
141+
def get(self, personId):
142+
"""Get person details, by personId.
143+
144+
Args:
145+
personId(unicode, str): The personID of the person.
146+
147+
Raises:
148+
AssertionError: If the parameter types are incorrect.
149+
SparkApiError: If the Cisco Spark cloud returns an error.
150+
151+
"""
152+
# Process args
153+
assert isinstance(personId, basestring)
154+
# API request
155+
json_obj = self.session.get('people/'+personId)
156+
# Return a Person object created from the response JSON data
157+
return Person(json_obj)
158+
159+
160+
def me(self):
161+
"""Get the person details of the account accessing the API 'me'.
162+
163+
Raises:
164+
SparkApiError: If the Cisco Spark cloud returns an error.
165+
166+
"""
167+
# API request
168+
json_obj = self.session.get('people/me')
169+
# Return a Person object created from the response JSON data
170+
return Person(json_obj)

0 commit comments

Comments
 (0)