Skip to content

Commit efe3eff

Browse files
committed
#20 Add Organizations Admin API Wrapper
Add a wrapper for the new Organizations API endpoints.
1 parent 2d3286e commit efe3eff

1 file changed

Lines changed: 139 additions & 0 deletions

File tree

ciscosparkapi/api/organizations.py

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

0 commit comments

Comments
 (0)