|
| 1 | +"""Cisco Spark Memberships-API wrapper classes. |
| 2 | +
|
| 3 | +Classes: |
| 4 | + TeamMembership: Models a Spark 'team membership' JSON object as a native |
| 5 | + Python object. |
| 6 | + TeamMembershipsAPI: Wrappers the Cisco Spark Memberships-API and exposes |
| 7 | + the API calls as Python method calls that return native Python objects. |
| 8 | +
|
| 9 | +""" |
| 10 | + |
| 11 | + |
| 12 | +from ciscosparkapi.exceptions import ciscosparkapiException |
| 13 | +from ciscosparkapi.helper import utf8, generator_container |
| 14 | +from ciscosparkapi.restsession import RestSession |
| 15 | +from ciscosparkapi.sparkdata import SparkData |
| 16 | + |
| 17 | + |
| 18 | +class TeamMembership(SparkData): |
| 19 | + """Model a Spark 'team membership' JSON object as a native Python object. |
| 20 | + """ |
| 21 | + |
| 22 | + def __init__(self, json): |
| 23 | + """Init a new TeamMembership object from a JSON dictionary or string. |
| 24 | +
|
| 25 | + Args: |
| 26 | + json(dict, unicode, str): Input JSON object. |
| 27 | +
|
| 28 | + Raises: |
| 29 | + TypeError: If the input object is not a dictionary or string. |
| 30 | +
|
| 31 | + """ |
| 32 | + super(TeamMembership, self).__init__(json) |
| 33 | + |
| 34 | + @property |
| 35 | + def id(self): |
| 36 | + return self._json[u'id'] |
| 37 | + |
| 38 | + @property |
| 39 | + def teamId(self): |
| 40 | + return self._json[u'teamId'] |
| 41 | + |
| 42 | + @property |
| 43 | + def personId(self): |
| 44 | + return self._json[u'personId'] |
| 45 | + |
| 46 | + @property |
| 47 | + def personEmail(self): |
| 48 | + return self._json[u'personEmail'] |
| 49 | + |
| 50 | + @property |
| 51 | + def personDisplayName(self): |
| 52 | + return self._json[u'personDisplayName'] |
| 53 | + |
| 54 | + @property |
| 55 | + def isModerator(self): |
| 56 | + return self._json[u'isModerator'] |
| 57 | + |
| 58 | + @property |
| 59 | + def created(self): |
| 60 | + return self._json[u'created'] |
| 61 | + |
| 62 | + |
| 63 | +class TeamMembershipsAPI(object): |
| 64 | + """Cisco Spark Team-Memberships-API wrapper class. |
| 65 | +
|
| 66 | + Wrappers the Cisco Spark Team-Memberships-API and exposes the API calls as |
| 67 | + Python method calls that return native Python objects. |
| 68 | +
|
| 69 | + Attributes: |
| 70 | + session(RestSession): The RESTful session object to be used for API |
| 71 | + calls to the Cisco Spark service. |
| 72 | +
|
| 73 | + """ |
| 74 | + |
| 75 | + def __init__(self, session): |
| 76 | + """Init a new TeamMembershipsAPI object with the provided RestSession. |
| 77 | +
|
| 78 | + Args: |
| 79 | + session(RestSession): The RESTful session object to be used for |
| 80 | + API calls to the Cisco Spark service. |
| 81 | +
|
| 82 | + Raises: |
| 83 | + AssertionError: If the parameter types are incorrect. |
| 84 | +
|
| 85 | + """ |
| 86 | + assert isinstance(session, RestSession) |
| 87 | + super(TeamMembershipsAPI, self).__init__() |
| 88 | + self.session = session |
| 89 | + |
| 90 | + @generator_container |
| 91 | + def list(self, teamId=None, max=None): |
| 92 | + """Lists all team memberships. |
| 93 | +
|
| 94 | + By default, lists memberships for teams to which the authenticated user |
| 95 | + belongs. |
| 96 | +
|
| 97 | + Use teamId to list memberships for a team, by ID. |
| 98 | +
|
| 99 | + This method supports Cisco Spark's implementation of RFC5988 Web |
| 100 | + Linking to provide pagination support. It returns a generator |
| 101 | + container that incrementally yield all team memberships returned by the |
| 102 | + query. The generator will automatically request additional 'pages' of |
| 103 | + responses from Spark as needed until all responses have been returned. |
| 104 | + The container makes the generator safe for reuse. A new API call will |
| 105 | + be made, using the same parameters that were specified when the |
| 106 | + generator was created, every time a new iterator is requested from the |
| 107 | + container. |
| 108 | +
|
| 109 | + Args: |
| 110 | + teamId(unicode, str): List memberships for the team with teamId. |
| 111 | + max(int): Limits the maximum number of memberships returned from |
| 112 | + the Spark service per request. |
| 113 | +
|
| 114 | +
|
| 115 | + Yields: |
| 116 | + TeamMembership: The the next team membership from the Cisco Spark |
| 117 | + query. |
| 118 | +
|
| 119 | + Raises: |
| 120 | + AssertionError: If the parameter types are incorrect. |
| 121 | + SparkApiError: If the Cisco Spark cloud returns an error. |
| 122 | +
|
| 123 | + """ |
| 124 | + # Process args |
| 125 | + assert teamId is None or isinstance(teamId, basestring) |
| 126 | + assert max is None or isinstance(max, int) |
| 127 | + params = {} |
| 128 | + if teamId: |
| 129 | + params[u'teamId'] = utf8(teamId) |
| 130 | + if max: |
| 131 | + params[u'max'] = max |
| 132 | + # API request - get items |
| 133 | + items = self.session.get_items('team/memberships', params=params) |
| 134 | + # Yield Person objects created from the returned items JSON objects |
| 135 | + for item in items: |
| 136 | + yield TeamMembership(item) |
| 137 | + |
| 138 | + def create(self, teamId, personId=None, personEmail=None, |
| 139 | + isModerator=False): |
| 140 | + """Add someone to a team by Person ID or email address. |
| 141 | +
|
| 142 | + Add someone to a team by Person ID or email address; optionally making |
| 143 | + them a moderator. |
| 144 | +
|
| 145 | + Args: |
| 146 | + teamId(unicode, str): ID of the team to which the person will be |
| 147 | + added. |
| 148 | + personId(unicode, str): ID of the person to be added to the team. |
| 149 | + personEmail(unicode, str): Email address of the person to be added |
| 150 | + to the team. |
| 151 | + isModerator(bool): If True, adds the person as a moderator for the |
| 152 | + team. If False, adds the person as normal member of the team. |
| 153 | +
|
| 154 | + Returns: |
| 155 | + TeamMembership: With the details of the created team membership. |
| 156 | +
|
| 157 | + Raises: |
| 158 | + AssertionError: If the parameter types are incorrect. |
| 159 | + ciscosparkapiException: If neither a personId or personEmail are |
| 160 | + provided. |
| 161 | + SparkApiError: If the Cisco Spark cloud returns an error. |
| 162 | +
|
| 163 | + """ |
| 164 | + # Process args |
| 165 | + assert isinstance(teamId, basestring) |
| 166 | + assert personId is None or isinstance(personId, basestring) |
| 167 | + assert personEmail is None or isinstance(personEmail, basestring) |
| 168 | + assert isModerator is None or isinstance(isModerator, bool) |
| 169 | + post_data = {} |
| 170 | + post_data[u'teamId'] = utf8(teamId) |
| 171 | + if personId: |
| 172 | + post_data[u'personId'] = utf8(personId) |
| 173 | + elif personEmail: |
| 174 | + post_data[u'personEmail'] = utf8(personEmail) |
| 175 | + else: |
| 176 | + error_message = "personId or personEmail must be provided to " \ |
| 177 | + "add a person to a team. Neither were provided." |
| 178 | + raise ciscosparkapiException(error_message) |
| 179 | + post_data[u'isModerator'] = isModerator |
| 180 | + # API request |
| 181 | + json_obj = self.session.post('team/memberships', json=post_data) |
| 182 | + # Return a TeamMembership object created from the response JSON data |
| 183 | + return TeamMembership(json_obj) |
| 184 | + |
| 185 | + def get(self, membershipId): |
| 186 | + """Get details for a team membership by ID. |
| 187 | +
|
| 188 | + Args: |
| 189 | + membershipId(unicode, str): The membershipId of the team |
| 190 | + membership. |
| 191 | +
|
| 192 | + Returns: |
| 193 | + TeamMembership: With the details of the requested team membership. |
| 194 | +
|
| 195 | + Raises: |
| 196 | + AssertionError: If the parameter types are incorrect. |
| 197 | + SparkApiError: If the Cisco Spark cloud returns an error. |
| 198 | +
|
| 199 | + """ |
| 200 | + # Process args |
| 201 | + assert isinstance(membershipId, basestring) |
| 202 | + # API request |
| 203 | + json_obj = self.session.get('team/memberships/'+membershipId) |
| 204 | + # Return a TeamMembership object created from the response JSON data |
| 205 | + return TeamMembership(json_obj) |
| 206 | + |
| 207 | + def update(self, membershipId, **update_attributes): |
| 208 | + """Update details for a team membership. |
| 209 | +
|
| 210 | + Args: |
| 211 | + membershipId(unicode, str): The membershipId of the team membership |
| 212 | + to be updated. |
| 213 | +
|
| 214 | + **update_attributes: |
| 215 | + isModerator(bool): If True, sets the person as a moderator for the |
| 216 | + team. If False, removes the person as a moderator for the team. |
| 217 | +
|
| 218 | + Returns: |
| 219 | + TeamMembership: With the updated Spark team membership details. |
| 220 | +
|
| 221 | + Raises: |
| 222 | + AssertionError: If the parameter types are incorrect. |
| 223 | + ciscosparkapiException: If an update attribute is not provided. |
| 224 | + SparkApiError: If the Cisco Spark cloud returns an error. |
| 225 | +
|
| 226 | + """ |
| 227 | + # Process args |
| 228 | + assert isinstance(membershipId, basestring) |
| 229 | + # Process update_attributes keyword arguments |
| 230 | + if not update_attributes: |
| 231 | + error_message = "At least one **update_attributes keyword " \ |
| 232 | + "argument must be specified." |
| 233 | + raise ciscosparkapiException(error_message) |
| 234 | + put_data = {} |
| 235 | + for param, value in update_attributes.items(): |
| 236 | + if isinstance(value, basestring): |
| 237 | + value = utf8(value) |
| 238 | + put_data[utf8(param)] = value |
| 239 | + # API request |
| 240 | + json_obj = self.session.post('team/memberships/'+membershipId, |
| 241 | + json=put_data) |
| 242 | + # Return a TeamMembership object created from the response JSON data |
| 243 | + return TeamMembership(json_obj) |
| 244 | + |
| 245 | + def delete(self, membershipId): |
| 246 | + """Delete a team membership, by ID. |
| 247 | +
|
| 248 | + Args: |
| 249 | + membershipId(unicode, str): The membershipId of the team membership |
| 250 | + to be deleted. |
| 251 | +
|
| 252 | + Raises: |
| 253 | + AssertionError: If the parameter types are incorrect. |
| 254 | + SparkApiError: If the Cisco Spark cloud returns an error. |
| 255 | +
|
| 256 | + """ |
| 257 | + # Process args |
| 258 | + assert isinstance(membershipId, basestring) |
| 259 | + # API request |
| 260 | + self.session.delete('team/memberships/'+membershipId) |
0 commit comments