|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Cisco Spark Events API.""" |
| 3 | + |
| 4 | + |
| 5 | +from __future__ import ( |
| 6 | + absolute_import, |
| 7 | + division, |
| 8 | + print_function, |
| 9 | + unicode_literals, |
| 10 | +) |
| 11 | + |
| 12 | +from builtins import * |
| 13 | + |
| 14 | +from past.builtins import basestring |
| 15 | + |
| 16 | +from ..generator_containers import generator_container |
| 17 | +from ..restsession import RestSession |
| 18 | +from ..utils import ( |
| 19 | + check_type, |
| 20 | + dict_from_items_with_values, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +__author__ = "Chris Lunsford" |
| 25 | +__author_email__ = "chrlunsf@cisco.com" |
| 26 | +__copyright__ = "Copyright (c) 2016-2018 Cisco and/or its affiliates." |
| 27 | +__license__ = "MIT" |
| 28 | + |
| 29 | + |
| 30 | +API_ENDPOINT = 'events' |
| 31 | +OBJECT_TYPE = 'event' |
| 32 | + |
| 33 | + |
| 34 | +class EventsAPI(object): |
| 35 | + """Cisco Spark Events API. |
| 36 | +
|
| 37 | + Wraps the Cisco Spark Events API and exposes the API as native Python |
| 38 | + methods that return native Python objects. |
| 39 | +
|
| 40 | + """ |
| 41 | + |
| 42 | + def __init__(self, session, object_factory): |
| 43 | + """Initialize a new EventsAPI object with the provided RestSession. |
| 44 | +
|
| 45 | + Args: |
| 46 | + session(RestSession): The RESTful session object to be used for |
| 47 | + API calls to the Cisco Spark service. |
| 48 | +
|
| 49 | + Raises: |
| 50 | + TypeError: If the parameter types are incorrect. |
| 51 | +
|
| 52 | + """ |
| 53 | + check_type(session, RestSession, may_be_none=False) |
| 54 | + |
| 55 | + super(EventsAPI, self).__init__() |
| 56 | + |
| 57 | + self._session = session |
| 58 | + self._object_factory = object_factory |
| 59 | + |
| 60 | + @generator_container |
| 61 | + def list(self, resource=None, type=None, actorId=None, _from=None, to=None, |
| 62 | + max=None, **request_parameters): |
| 63 | + """List events. |
| 64 | +
|
| 65 | + List events in your organization. Several query parameters are |
| 66 | + available to filter the response. |
| 67 | +
|
| 68 | + Note: `from` is a keyword in Python and may not be used as a variable |
| 69 | + name, so we had to use `_from` instead. |
| 70 | +
|
| 71 | + This method supports Cisco Spark's implementation of RFC5988 Web |
| 72 | + Linking to provide pagination support. It returns a generator |
| 73 | + container that incrementally yields all events returned by the |
| 74 | + query. The generator will automatically request additional 'pages' of |
| 75 | + responses from Spark as needed until all responses have been returned. |
| 76 | + The container makes the generator safe for reuse. A new API call will |
| 77 | + be made, using the same parameters that were specified when the |
| 78 | + generator was created, every time a new iterator is requested from the |
| 79 | + container. |
| 80 | +
|
| 81 | + Args: |
| 82 | + resource(basestring): Limit results to a specific resource type. |
| 83 | + Possible values: "messages", "memberships". |
| 84 | + type(basestring): Limit results to a specific event type. Possible |
| 85 | + values: "created", "updated", "deleted". |
| 86 | + actorId(basestring): Limit results to events performed by this |
| 87 | + person, by ID. |
| 88 | + _from(basestring): Limit results to events which occurred after a |
| 89 | + date and time, in ISO8601 format (yyyy-MM-dd'T'HH:mm:ss.SSSZ). |
| 90 | + to(basestring): Limit results to events which occurred before a |
| 91 | + date and time, in ISO8601 format (yyyy-MM-dd'T'HH:mm:ss.SSSZ). |
| 92 | + max(int): Limit the maximum number of items returned from the Spark |
| 93 | + service per request. |
| 94 | + **request_parameters: Additional request parameters (provides |
| 95 | + support for parameters that may be added in the future). |
| 96 | +
|
| 97 | + Returns: |
| 98 | + GeneratorContainer: A GeneratorContainer which, when iterated, |
| 99 | + yields the events returned by the Cisco Spark query. |
| 100 | +
|
| 101 | + Raises: |
| 102 | + TypeError: If the parameter types are incorrect. |
| 103 | + SparkApiError: If the Cisco Spark cloud returns an error. |
| 104 | +
|
| 105 | + """ |
| 106 | + check_type(resource, basestring) |
| 107 | + check_type(type, basestring) |
| 108 | + check_type(actorId, basestring) |
| 109 | + check_type(_from, basestring) |
| 110 | + check_type(to, basestring) |
| 111 | + check_type(max, int) |
| 112 | + |
| 113 | + params = dict_from_items_with_values( |
| 114 | + request_parameters, |
| 115 | + resource=resource, |
| 116 | + type=type, |
| 117 | + actorId=actorId, |
| 118 | + _from=_from, |
| 119 | + to=to, |
| 120 | + max=max, |
| 121 | + ) |
| 122 | + |
| 123 | + if _from: |
| 124 | + params["from"] = params.pop("_from") |
| 125 | + |
| 126 | + # API request - get items |
| 127 | + items = self._session.get_items(API_ENDPOINT, params=params) |
| 128 | + |
| 129 | + # Yield event objects created from the returned items JSON objects |
| 130 | + for item in items: |
| 131 | + yield self._object_factory(OBJECT_TYPE, item) |
| 132 | + |
| 133 | + def get(self, eventId): |
| 134 | + """Get the details for an event, by event ID. |
| 135 | +
|
| 136 | + Args: |
| 137 | + eventId(basestring): The ID of the event to be retrieved. |
| 138 | +
|
| 139 | + Returns: |
| 140 | + Event: A event object with the details of the requested room. |
| 141 | +
|
| 142 | + Raises: |
| 143 | + TypeError: If the parameter types are incorrect. |
| 144 | + SparkApiError: If the Cisco Spark cloud returns an error. |
| 145 | +
|
| 146 | + """ |
| 147 | + check_type(eventId, basestring, may_be_none=False) |
| 148 | + |
| 149 | + # API request |
| 150 | + json_data = self._session.get(API_ENDPOINT + '/' + eventId) |
| 151 | + |
| 152 | + # Return a room object created from the response JSON data |
| 153 | + return self._object_factory(OBJECT_TYPE, json_data) |
0 commit comments