|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import List, overload |
| 4 | + |
| 5 | +from .. import urls |
| 6 | + |
| 7 | +from ..cursors import CursorPaginator |
| 8 | +from ..resources import Resource, Resources |
| 9 | + |
| 10 | + |
| 11 | +class ShinyUsageEvent(Resource): |
| 12 | + @property |
| 13 | + def content_guid(self) -> str: |
| 14 | + """The associated unique content identifier. |
| 15 | +
|
| 16 | + Returns |
| 17 | + ------- |
| 18 | + str |
| 19 | + """ |
| 20 | + return self["content_guid"] |
| 21 | + |
| 22 | + @property |
| 23 | + def user_guid(self) -> str: |
| 24 | + """The associated unique user identifier. |
| 25 | +
|
| 26 | + Returns |
| 27 | + ------- |
| 28 | + str |
| 29 | + """ |
| 30 | + return self["user_guid"] |
| 31 | + |
| 32 | + @property |
| 33 | + def started(self) -> str: |
| 34 | + """The started timestamp. |
| 35 | +
|
| 36 | + Returns |
| 37 | + ------- |
| 38 | + str |
| 39 | + """ |
| 40 | + return self["started"] |
| 41 | + |
| 42 | + @property |
| 43 | + def ended(self) -> str: |
| 44 | + """The ended timestamp. |
| 45 | +
|
| 46 | + Returns |
| 47 | + ------- |
| 48 | + str |
| 49 | + """ |
| 50 | + return self["ended"] |
| 51 | + |
| 52 | + @property |
| 53 | + def data_version(self) -> int: |
| 54 | + """The data version. |
| 55 | +
|
| 56 | + Returns |
| 57 | + ------- |
| 58 | + int |
| 59 | + """ |
| 60 | + return self["data_version"] |
| 61 | + |
| 62 | + |
| 63 | +class ShinyUsage(Resources): |
| 64 | + @overload |
| 65 | + def find( |
| 66 | + self, |
| 67 | + content_guid: str = ..., |
| 68 | + min_data_version: int = ..., |
| 69 | + start: str = ..., |
| 70 | + end: str = ..., |
| 71 | + ) -> List[ShinyUsageEvent]: |
| 72 | + """Find usage. |
| 73 | +
|
| 74 | + Parameters |
| 75 | + ---------- |
| 76 | + content_guid : str, optional |
| 77 | + Filter by an associated unique content identifer, by default ... |
| 78 | + min_data_version : int, optional |
| 79 | + Filter by a minimum data version, by default ... |
| 80 | + start : str, optional |
| 81 | + Filter by the start time, by default ... |
| 82 | + end : str, optional |
| 83 | + Filter by the end time, by default ... |
| 84 | +
|
| 85 | + Returns |
| 86 | + ------- |
| 87 | + List[ShinyUsageEvent] |
| 88 | + """ |
| 89 | + ... |
| 90 | + |
| 91 | + @overload |
| 92 | + def find(self, *args, **kwargs) -> List[ShinyUsageEvent]: |
| 93 | + """Find usage. |
| 94 | +
|
| 95 | + Returns |
| 96 | + ------- |
| 97 | + List[ShinyUsageEvent] |
| 98 | + """ |
| 99 | + ... |
| 100 | + |
| 101 | + def find(self, *args, **kwargs) -> List[ShinyUsageEvent]: |
| 102 | + """Find usage. |
| 103 | +
|
| 104 | + Returns |
| 105 | + ------- |
| 106 | + List[ShinyUsageEvent] |
| 107 | + """ |
| 108 | + params = dict(*args, **kwargs) |
| 109 | + params = rename_params(params) |
| 110 | + |
| 111 | + path = "/v1/instrumentation/shiny/usage" |
| 112 | + url = urls.append(self.config.url, path) |
| 113 | + paginator = CursorPaginator(self.session, url, params=params) |
| 114 | + results = paginator.fetch_results() |
| 115 | + return [ |
| 116 | + ShinyUsageEvent( |
| 117 | + config=self.config, |
| 118 | + session=self.session, |
| 119 | + **result, |
| 120 | + ) |
| 121 | + for result in results |
| 122 | + ] |
| 123 | + |
| 124 | + @overload |
| 125 | + def find_one( |
| 126 | + self, |
| 127 | + content_guid: str = ..., |
| 128 | + min_data_version: int = ..., |
| 129 | + start: str = ..., |
| 130 | + end: str = ..., |
| 131 | + ) -> ShinyUsageEvent | None: |
| 132 | + """Find a usage event. |
| 133 | +
|
| 134 | + Parameters |
| 135 | + ---------- |
| 136 | + content_guid : str, optional |
| 137 | + Filter by an associated unique content identifer, by default ... |
| 138 | + min_data_version : int, optional |
| 139 | + Filter by a minimum data version, by default ... |
| 140 | + start : str, optional |
| 141 | + Filter by the start time, by default ... |
| 142 | + end : str, optional |
| 143 | + Filter by the end time, by default ... |
| 144 | +
|
| 145 | + Returns |
| 146 | + ------- |
| 147 | + ShinyUsageEvent | None |
| 148 | + """ |
| 149 | + ... |
| 150 | + |
| 151 | + @overload |
| 152 | + def find_one(self, *args, **kwargs) -> ShinyUsageEvent | None: |
| 153 | + """Find a usage event. |
| 154 | +
|
| 155 | + Returns |
| 156 | + ------- |
| 157 | + ShinyUsageEvent | None |
| 158 | + """ |
| 159 | + ... |
| 160 | + |
| 161 | + def find_one(self, *args, **kwargs) -> ShinyUsageEvent | None: |
| 162 | + """Find a usage event. |
| 163 | +
|
| 164 | + Returns |
| 165 | + ------- |
| 166 | + ShinyUsageEvent | None |
| 167 | + """ |
| 168 | + params = dict(*args, **kwargs) |
| 169 | + params = rename_params(params) |
| 170 | + path = "/v1/instrumentation/shiny/usage" |
| 171 | + url = urls.append(self.config.url, path) |
| 172 | + paginator = CursorPaginator(self.session, url, params=params) |
| 173 | + pages = paginator.fetch_pages() |
| 174 | + results = (result for page in pages for result in page.results) |
| 175 | + visits = ( |
| 176 | + ShinyUsageEvent( |
| 177 | + config=self.config, |
| 178 | + session=self.session, |
| 179 | + **result, |
| 180 | + ) |
| 181 | + for result in results |
| 182 | + ) |
| 183 | + return next(visits, None) |
| 184 | + |
| 185 | + |
| 186 | +def rename_params(params: dict) -> dict: |
| 187 | + """Rename params from the internal to the external signature. |
| 188 | +
|
| 189 | + The API accepts `from` as a querystring parameter. Since `from` is a reserved word in Python, the SDK uses the name `start` instead. The querystring parameter `to` takes the same form for consistency. |
| 190 | +
|
| 191 | + Parameters |
| 192 | + ---------- |
| 193 | + params : dict |
| 194 | +
|
| 195 | + Returns |
| 196 | + ------- |
| 197 | + dict |
| 198 | + """ |
| 199 | + if "start" in params: |
| 200 | + params["from"] = params["start"] |
| 201 | + del params["start"] |
| 202 | + |
| 203 | + if "end" in params: |
| 204 | + params["to"] = params["end"] |
| 205 | + del params["end"] |
| 206 | + |
| 207 | + return params |
0 commit comments