Skip to content

Commit a124370

Browse files
committed
Initial TeamsAPI wrapper
Add wrapper classes for the Cisco Spark Teams-API.
1 parent 5b017ff commit a124370

File tree

2 files changed

+206
-0
lines changed

2 files changed

+206
-0
lines changed

ciscosparkapi/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from api.rooms import Room, RoomsAPI
55
from api.memberships import Membership, MembershipsAPI
66
from api.messages import Message, MessagesAPI
7+
from api.teams import Team, TeamsAPI
78

89
class CiscoSparkAPI(object):
910
"""Cisco Spark API wrapper class."""
@@ -27,6 +28,7 @@ def __init__(self, access_token, base_url=None, timeout=None):
2728
self.rooms = RoomsAPI(self.session)
2829
self.memberships = MembershipsAPI(self.session)
2930
self.messages = MessagesAPI(self.session)
31+
self.teams = TeamsAPI(self.session)
3032

3133
@property
3234
def access_token(self):

ciscosparkapi/api/teams.py

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
"""Cisco Spark Teams-API wrapper classes.
2+
3+
Classes:
4+
Team: Models a Spark 'team' JSON object as a native Python object.
5+
TeamsAPI: Wrappers the Cisco Spark Teams-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 Team(SparkData):
18+
"""Model a Spark 'team' JSON object as a native Python object."""
19+
20+
def __init__(self, json):
21+
"""Init a new Team 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(Team, self).__init__(json)
31+
32+
@property
33+
def id(self):
34+
return self._json[u'id']
35+
36+
@property
37+
def name(self):
38+
return self._json[u'name']
39+
40+
@property
41+
def created(self):
42+
return self._json[u'created']
43+
44+
45+
class TeamsAPI(object):
46+
"""Cisco Spark Teams-API wrapper class.
47+
48+
Wrappers the Cisco Spark Teams-API and exposes the API calls as Python
49+
method calls that return native Python objects.
50+
51+
Attributes:
52+
session(RestSession): The RESTful session object to be used for API
53+
calls to the Cisco Spark service.
54+
55+
"""
56+
57+
def __init__(self, session):
58+
"""Init a new TeamsAPI object with the provided RestSession.
59+
60+
Args:
61+
session(RestSession): The RESTful session object to be used for
62+
API calls to the Cisco Spark service.
63+
64+
Raises:
65+
AssertionError: If the parameter types are incorrect.
66+
67+
"""
68+
assert isinstance(session, RestSession)
69+
super(TeamsAPI, self).__init__()
70+
self.session = session
71+
72+
@generator_container
73+
def list(self, max=None):
74+
"""List teams to which the authenticated user belongs.
75+
76+
This method supports Cisco Spark's implementation of RFC5988 Web
77+
Linking to provide pagination support. It returns a generator
78+
container that incrementally yield all teams returned by the
79+
query. The generator will automatically request additional 'pages' of
80+
responses from Spark as needed until all responses have been returned.
81+
The container makes the generator safe for reuse. A new API call will
82+
be made, using the same parameters that were specified when the
83+
generator was created, every time a new iterator is requested from the
84+
container.
85+
86+
Args:
87+
max(int): Limits the maximum number of teams returned from the
88+
Spark service per request.
89+
90+
Yields:
91+
Team: The the next team from the Cisco Spark query.
92+
93+
Raises:
94+
AssertionError: If the parameter types are incorrect.
95+
SparkApiError: If the Cisco Spark cloud returns an error.
96+
97+
"""
98+
# Process args
99+
assert max is None or isinstance(max, int)
100+
params = {}
101+
if max:
102+
params[u'max'] = max
103+
# API request - get items
104+
items = self.session.get_items('teams', params=params)
105+
# Yield Team objects created from the returned items JSON objects
106+
for item in items:
107+
yield Team(item)
108+
109+
def create(self, name):
110+
"""Create a team.
111+
112+
The authenticated user is automatically added as a member of the team.
113+
114+
Args:
115+
name(unicode, str): A user-friendly name for the team.
116+
117+
Returns:
118+
Team: With the details of the created team.
119+
120+
Raises:
121+
AssertionError: If the parameter types are incorrect.
122+
SparkApiError: If the Cisco Spark cloud returns an error.
123+
124+
"""
125+
# Process args
126+
assert isinstance(name, basestring)
127+
post_data = {}
128+
post_data[u'name'] = utf8(name)
129+
# API request
130+
json_obj = self.session.post('teams', json=post_data)
131+
# Return a Team object created from the response JSON data
132+
return Team(json_obj)
133+
134+
def get(self, teamId):
135+
"""Get the details of a team, by ID.
136+
137+
Args:
138+
teamId(unicode, str): The teamId of the team.
139+
140+
Returns:
141+
Team: With the details of the requested team.
142+
143+
Raises:
144+
AssertionError: If the parameter types are incorrect.
145+
SparkApiError: If the Cisco Spark cloud returns an error.
146+
147+
"""
148+
# Process args
149+
assert isinstance(teamId, basestring)
150+
# API request
151+
json_obj = self.session.get('teams/'+teamId)
152+
# Return a Team object created from the response JSON data
153+
return Team(json_obj)
154+
155+
def update(self, teamId, **update_attributes):
156+
"""Update details for a team.
157+
158+
Args:
159+
teamId(unicode, str): The teamId of the team to be updated.
160+
161+
**update_attributes:
162+
name(unicode, str): A user-friendly name for the team.
163+
164+
Returns:
165+
Team: With the updated Spark team details.
166+
167+
Raises:
168+
AssertionError: If the parameter types are incorrect.
169+
ciscosparkapiException: If an update attribute is not provided.
170+
SparkApiError: If the Cisco Spark cloud returns an error.
171+
172+
"""
173+
# Process args
174+
assert isinstance(teamId, basestring)
175+
# Process update_attributes keyword arguments
176+
if not update_attributes:
177+
error_message = "At least one **update_attributes keyword " \
178+
"argument must be specified."
179+
raise ciscosparkapiException(error_message)
180+
put_data = {}
181+
for param, value in update_attributes.items():
182+
if isinstance(value, basestring):
183+
value = utf8(value)
184+
put_data[utf8(param)] = value
185+
# API request
186+
json_obj = self.session.post('teams/'+teamId, json=put_data)
187+
# Return a Team object created from the response JSON data
188+
return Team(json_obj)
189+
190+
def delete(self, teamId):
191+
"""Delete a team.
192+
193+
Args:
194+
teamId(unicode, str): The teamId of the team to be deleted.
195+
196+
Raises:
197+
AssertionError: If the parameter types are incorrect.
198+
SparkApiError: If the Cisco Spark cloud returns an error.
199+
200+
"""
201+
# Process args
202+
assert isinstance(teamId, basestring)
203+
# API request
204+
self.session.delete('teams/'+teamId)

0 commit comments

Comments
 (0)