|
| 1 | +"""Spark - Rooms API - wrapper classes.""" |
| 2 | + |
| 3 | + |
| 4 | +from ciscosparkapi.exceptions import ciscosparkapiException |
| 5 | +from ciscosparkapi.helperfunc import utf8 |
| 6 | +from ciscosparkapi.restsession import RestSession |
| 7 | + |
| 8 | + |
| 9 | +class Room(object): |
| 10 | + """Spark Room object wrapper class.""" |
| 11 | + |
| 12 | + def __init__(self, json): |
| 13 | + assert isinstance(json, dict) |
| 14 | + super(Room, self).__init__() |
| 15 | + self.json = json |
| 16 | + |
| 17 | + |
| 18 | +class RoomsAPI(object): |
| 19 | + """Spark Rooms API request wrapper.""" |
| 20 | + |
| 21 | + def __init__(self, session): |
| 22 | + assert isinstance(session, RestSession) |
| 23 | + super(RoomsAPI, self).__init__() |
| 24 | + self.session = session |
| 25 | + |
| 26 | + def list(self, max=None, **query_params): |
| 27 | + """List rooms. |
| 28 | +
|
| 29 | + By default, lists rooms to which the authenticated user belongs. |
| 30 | +
|
| 31 | + This method supports Cisco Spark's implmentation of RFC5988 Web Linking |
| 32 | + to provide pagination support. It returns an iterator that |
| 33 | + incrementally yields all rooms returned by the query. It will |
| 34 | + automatically and efficiently request the additional 'pages' of |
| 35 | + responses from Spark as needed until all responses have been exhausted. |
| 36 | +
|
| 37 | + Args: |
| 38 | + max(int): Limits the maximum number of rooms returned from the |
| 39 | + Spark service per request. |
| 40 | +
|
| 41 | + **query_params: |
| 42 | + teamId(string): Limit the rooms to those associated with a team. |
| 43 | + type(string): |
| 44 | + 'direct': returns all 1-to-1 rooms. |
| 45 | + 'group': returns all group rooms. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + A Room iterator. |
| 49 | +
|
| 50 | + Raises: |
| 51 | + SparkApiError: If the list request fails. |
| 52 | + """ |
| 53 | + # Process args |
| 54 | + assert max is None or isinstance(max, int) |
| 55 | + params = {} |
| 56 | + if max: params[u'max'] = max |
| 57 | + # Process query_param keyword arguments |
| 58 | + if query_params: |
| 59 | + for param, value in query_params.items(): |
| 60 | + if isinstance(value, basestring): value = utf8(value) |
| 61 | + params[utf8(param)] = value |
| 62 | + # API request - get items |
| 63 | + items = self.session.get_items('rooms', params=params) |
| 64 | + # Yield Room objects created from the returned items JSON objects |
| 65 | + for item in items: |
| 66 | + yield Room(item) |
| 67 | + |
| 68 | + def create(self, title, teamId=None): |
| 69 | + """Creates a room. |
| 70 | +
|
| 71 | + The authenticated user is automatically added as a member of the room. |
| 72 | +
|
| 73 | + Args: |
| 74 | + title(string): A user-friendly name for the room. |
| 75 | + teamId(string): The team ID with which this room is associated. |
| 76 | +
|
| 77 | + Raises: |
| 78 | + SparkApiError: If the create operation fails. |
| 79 | + """ |
| 80 | + # Process args |
| 81 | + assert isinstance(title, basestring) |
| 82 | + assert teamId is None or isinstance(teamId, basestring) |
| 83 | + post_data = {} |
| 84 | + post_data[u'title'] = utf8(title) |
| 85 | + if teamId: post_data[u'teamId'] = utf8(teamId) |
| 86 | + # API request |
| 87 | + json_room_obj = self.session.post('rooms', json=post_data) |
| 88 | + # Return a Room object created from the response JSON data |
| 89 | + return Room(json_room_obj) |
| 90 | + |
| 91 | + def get(self, roomId): |
| 92 | + """Gets the details of a room. |
| 93 | +
|
| 94 | + Args: |
| 95 | + roomId(string): The roomId of the room. |
| 96 | +
|
| 97 | + Raises: |
| 98 | + SparkApiError: If the get operation fails. |
| 99 | + """ |
| 100 | + # Process args |
| 101 | + assert isinstance(roomId, basestring) |
| 102 | + # API request |
| 103 | + json_room_obj = self.session.get('rooms/'+roomId) |
| 104 | + # Return a Room object created from the response JSON data |
| 105 | + return Room(json_room_obj) |
| 106 | + |
| 107 | + def update(self, roomId, **update_attributes): |
| 108 | + """Updates details for a room. |
| 109 | +
|
| 110 | + Args: |
| 111 | + roomId(string): The roomId of the room to be updated. |
| 112 | +
|
| 113 | + **update_attributes: |
| 114 | + title(string): A user-friendly name for the room. |
| 115 | +
|
| 116 | + Returns: |
| 117 | + A Room object with the updated Spark room details. |
| 118 | +
|
| 119 | + Raises: |
| 120 | + ciscosparkapiException: If an update attribute is not provided. |
| 121 | + SparkApiError: If the update operation fails. |
| 122 | + """ |
| 123 | + # Process args |
| 124 | + assert isinstance(roomId, basestring) |
| 125 | + # Process update_attributes keyword arguments |
| 126 | + if not update_attributes: |
| 127 | + error_message = "You must provide at least one " \ |
| 128 | + "**update_attributes keyword argument; 0 provided." |
| 129 | + raise ciscosparkapiException(error_message) |
| 130 | + put_data = {} |
| 131 | + for param, value in update_attributes.items(): |
| 132 | + if isinstance(value, basestring): value = utf8(value) |
| 133 | + put_data[utf8(param)] = value |
| 134 | + # API request |
| 135 | + json_room_obj = self.session.post('rooms', json=put_data) |
| 136 | + # Return a Room object created from the response JSON data |
| 137 | + return Room(json_room_obj) |
| 138 | + |
| 139 | + def delete(self, roomId): |
| 140 | + """Delete a room. |
| 141 | +
|
| 142 | + Args: |
| 143 | + roomId(string): The roomId of the room to be deleted. |
| 144 | +
|
| 145 | + Raises: |
| 146 | + SparkApiError: If the delete operation fails. |
| 147 | + """ |
| 148 | + # Process args |
| 149 | + assert isinstance(roomId, basestring) |
| 150 | + # API request |
| 151 | + self.session.delete('rooms/'+roomId) |
0 commit comments