Skip to content

Commit f209423

Browse files
committed
#20 Add Roles Admin API Wrapper
Add a wrapper for the new Roles API endpoints.
1 parent 8389ba9 commit f209423

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

ciscosparkapi/api/roles.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# -*- coding: utf-8 -*-
2+
"""Cisco Spark Roles API wrapper.
3+
4+
Classes:
5+
Role: Models a Spark Role JSON object as a native Python object.
6+
RolesAPI: Wraps the Cisco Spark Roles API and exposes the
7+
API calls as Python method calls that return native Python objects.
8+
9+
"""
10+
11+
12+
from builtins import object
13+
from six import string_types
14+
15+
from ciscosparkapi.utils import generator_container
16+
from ciscosparkapi.restsession import RestSession
17+
from ciscosparkapi.sparkdata import SparkData
18+
19+
20+
__author__ = "Chris Lunsford"
21+
__author_email__ = "[email protected]"
22+
__copyright__ = "Copyright (c) 2016 Cisco Systems, Inc."
23+
__role__ = "MIT"
24+
25+
26+
class Role(SparkData):
27+
"""Model a Spark Role JSON object as a native Python object."""
28+
29+
def __init__(self, json):
30+
"""Init a new Role data object from a dict or JSON string.
31+
32+
Args:
33+
json(dict, string_types): Input JSON object.
34+
35+
Raises:
36+
TypeError: If the input object is not a dictionary or string.
37+
38+
"""
39+
super(Role, self).__init__(json)
40+
41+
@property
42+
def id(self):
43+
"""The unique id for the Role."""
44+
return self._json.get('id')
45+
46+
@property
47+
def name(self):
48+
"""The name of the Role."""
49+
return self._json.get('name')
50+
51+
52+
class RolesAPI(object):
53+
"""Cisco Spark Roles API wrapper.
54+
55+
Wraps the Cisco Spark Roles API and exposes the API calls as Python
56+
method calls that return native Python objects.
57+
58+
"""
59+
60+
def __init__(self, session):
61+
"""Init a new RolesAPI object with the provided RestSession.
62+
63+
Args:
64+
session(RestSession): The RESTful session object to be used for
65+
API calls to the Cisco Spark service.
66+
67+
Raises:
68+
AssertionError: If the parameter types are incorrect.
69+
70+
"""
71+
assert isinstance(session, RestSession)
72+
super(RolesAPI, self).__init__()
73+
self._session = session
74+
75+
@generator_container
76+
def list(self, max=None):
77+
"""List Roles.
78+
79+
This method supports Cisco Spark's implementation of RFC5988 Web
80+
Linking to provide pagination support. It returns a generator
81+
container that incrementally yields all objects returned by the
82+
query. The generator will automatically request additional 'pages' of
83+
responses from Spark as needed until all responses have been returned.
84+
The container makes the generator safe for reuse. A new API call will
85+
be made, using the same parameters that were specified when the
86+
generator was created, every time a new iterator is requested from the
87+
container.
88+
89+
Args:
90+
max(int): Limits the maximum number of entries returned from the
91+
Spark service per request (page size; requesting additional
92+
pages is handled automatically).
93+
94+
Returns:
95+
GeneratorContainer: When iterated, the GeneratorContainer, yields
96+
the objects returned from the Cisco Spark query.
97+
98+
Raises:
99+
AssertionError: If the parameter types are incorrect.
100+
SparkApiError: If the Cisco Spark cloud returns an error.
101+
102+
"""
103+
# Process args
104+
assert max is None or isinstance(max, int)
105+
params = {}
106+
if max:
107+
params['max'] = max
108+
# API request - get items
109+
items = self._session.get_items('roles', params=params)
110+
# Yield Role objects created from the returned JSON objects
111+
for item in items:
112+
yield Role(item)
113+
114+
def get(self, roleId):
115+
"""Get the details of a Role, by id.
116+
117+
Args:
118+
roleId(string_types): The id of the Role.
119+
120+
Returns:
121+
Role: With the details of the requested Role.
122+
123+
Raises:
124+
AssertionError: If the parameter types are incorrect.
125+
SparkApiError: If the Cisco Spark cloud returns an error.
126+
127+
"""
128+
# Process args
129+
assert isinstance(roleId, string_types)
130+
# API request
131+
json_obj = self._session.get('roles/' + roleId)
132+
# Return a Role object created from the returned JSON object
133+
return Role(json_obj)

0 commit comments

Comments
 (0)