|
| 1 | +from ns1.rest.datasets import Datasets |
| 2 | + |
| 3 | + |
| 4 | +class DatasetException(Exception): |
| 5 | + pass |
| 6 | + |
| 7 | + |
| 8 | +class Dataset(object): |
| 9 | + """ |
| 10 | + High level object representing a dataset. |
| 11 | + """ |
| 12 | + |
| 13 | + def __init__(self, config): |
| 14 | + """ |
| 15 | + Create a new high level Dataset object |
| 16 | + :param ns1.config.Config config: config object |
| 17 | + """ |
| 18 | + self._rest = Datasets(config) |
| 19 | + self.config = config |
| 20 | + self.data = None |
| 21 | + |
| 22 | + def __repr__(self): |
| 23 | + return "<Dataset [%s]=%s,%s,%s,%s,%s,%s>" % ( |
| 24 | + self.__getitem__("id"), |
| 25 | + self.__getitem__("name"), |
| 26 | + self.__getitem__("datatype"), |
| 27 | + self.__getitem__("repeat"), |
| 28 | + self.__getitem__("timeframe"), |
| 29 | + self.__getitem__("export_type"), |
| 30 | + self.__getitem__("recipient_emails"), |
| 31 | + ) |
| 32 | + |
| 33 | + def __getitem__(self, item: str): |
| 34 | + if not self.data: |
| 35 | + raise DatasetException("dataset not loaded") |
| 36 | + return self.data.get(item, None) |
| 37 | + |
| 38 | + def reload(self, callback=None, errback=None): |
| 39 | + """ |
| 40 | + Reload dataset data from the API. |
| 41 | + :param callback: function call back once the call has completed |
| 42 | + :param errback: function call back if the call fails |
| 43 | + """ |
| 44 | + return self.load(reload=True, callback=callback, errback=errback) |
| 45 | + |
| 46 | + def load(self, id: str = None, callback=None, errback=None, reload=False): |
| 47 | + """ |
| 48 | + Load dataset data from the API. |
| 49 | + :param str id: dataset id to load |
| 50 | + :param callback: function call back once the call has completed |
| 51 | + :param bool reload: whether to reuse the instance data instead of fetching it from the server |
| 52 | + """ |
| 53 | + if not reload and self.data: |
| 54 | + return self.data |
| 55 | + if id is None and self.data: |
| 56 | + id = self.__getitem__("id") |
| 57 | + if id is None: |
| 58 | + raise DatasetException("no dataset id: did you mean to create?") |
| 59 | + |
| 60 | + def success(result: dict, *args): |
| 61 | + self.data = result |
| 62 | + if callback: |
| 63 | + return callback(self) |
| 64 | + else: |
| 65 | + return self |
| 66 | + |
| 67 | + return self._rest.retrieve(id, callback=success, errback=errback) |
| 68 | + |
| 69 | + def loadFromDict(self, dt: dict): |
| 70 | + """ |
| 71 | + Load dataset data from a dictionary. |
| 72 | + :param dict dt: dictionary containing *at least* either an id or domain/path/target |
| 73 | + """ |
| 74 | + if "id" in dt or ( |
| 75 | + "name" in dt |
| 76 | + and "datatype" in dt |
| 77 | + and "repeat" in dt |
| 78 | + and "timeframe" in dt |
| 79 | + and "export_type" in dt |
| 80 | + and "recipient_emails" in dt |
| 81 | + ): |
| 82 | + self.data = dt |
| 83 | + return self |
| 84 | + else: |
| 85 | + raise DatasetException("insufficient parameters") |
| 86 | + |
| 87 | + def delete(self, callback=None, errback=None): |
| 88 | + """ |
| 89 | + Delete the dataset. |
| 90 | + :param callback: function call back once the call has completed |
| 91 | + :param errback: function call back if the call fails |
| 92 | + """ |
| 93 | + id = self.__getitem__("id") |
| 94 | + return self._rest.delete(id, callback=callback, errback=errback) |
| 95 | + |
| 96 | + def create( |
| 97 | + self, |
| 98 | + name: str, |
| 99 | + datatype: dict, |
| 100 | + repeat: dict, |
| 101 | + timeframe: dict, |
| 102 | + export_type: str, |
| 103 | + recipient_emails: list, |
| 104 | + callback=None, |
| 105 | + errback=None, |
| 106 | + **kwargs |
| 107 | + ): |
| 108 | + """ |
| 109 | + Create a new dataset. Pass a list of keywords and their values to |
| 110 | + configure. For the list of keywords available for dataset configuration, |
| 111 | + see :attr:`ns1.rest.datasets.Datasets.PASSTHRU_FIELDS` |
| 112 | + :param str name: the name of the dataset |
| 113 | + :param str datatype: datatype settings to define the type of data to be pulled |
| 114 | + :param str repeat: repeat settings to define recurrent reports |
| 115 | + :param str timeframe: timeframe settings for the data to be pulled |
| 116 | + :param str export_type: output format of the report |
| 117 | + :param str recipient_emails: list of user emails that will receive a copy of the report |
| 118 | + :param callback: function call back once the call has completed |
| 119 | + :param errback: function call back if the call fails |
| 120 | + """ |
| 121 | + if self.data: |
| 122 | + raise DatasetException("dataset already loaded") |
| 123 | + |
| 124 | + return self._rest.create( |
| 125 | + name, |
| 126 | + datatype, |
| 127 | + repeat, |
| 128 | + timeframe, |
| 129 | + export_type, |
| 130 | + recipient_emails, |
| 131 | + callback=callback, |
| 132 | + errback=errback, |
| 133 | + **kwargs |
| 134 | + ) |
| 135 | + |
| 136 | + def listDatasets(self, callback=None, errback=None): |
| 137 | + """ |
| 138 | + Lists all datasets currently configured. |
| 139 | + :param callback: function call back once the call has completed |
| 140 | + :param errback: function call back if the call fails |
| 141 | + :return: a list of Dataset objects |
| 142 | + """ |
| 143 | + |
| 144 | + def success(result, *args): |
| 145 | + ret = [] |
| 146 | + for dt in result: |
| 147 | + ret.append(Dataset(self.config).loadFromDict(dt)) |
| 148 | + if callback: |
| 149 | + return callback(ret) |
| 150 | + else: |
| 151 | + return ret |
| 152 | + |
| 153 | + return Datasets(self.config).list(callback=success, errback=errback) |
| 154 | + |
| 155 | + def retrieveReport( |
| 156 | + self, rp_id: str, dt_id: str = None, callback=None, errback=None |
| 157 | + ): |
| 158 | + """ |
| 159 | + Retrieves a generated report given a dataset id and a report id |
| 160 | + :param str rp_id: the id of the generated report to download |
| 161 | + :param str dt_id: the id of the dataset that the above report belongs to |
| 162 | + :param callback: function call back once the call has completed |
| 163 | + :param errback: function call back if the call fails |
| 164 | + :return: generated report |
| 165 | + """ |
| 166 | + |
| 167 | + if dt_id is None and self.data: |
| 168 | + dt_id = self.__getitem__("id") |
| 169 | + if dt_id is None: |
| 170 | + raise DatasetException("no dataset id: did you mean to create?") |
| 171 | + |
| 172 | + return Datasets(self.config).retrieveReport( |
| 173 | + dt_id, rp_id, callback=callback, errback=errback |
| 174 | + ) |
0 commit comments