|
| 1 | +"""Nextcloud Webhooks API.""" |
| 2 | + |
| 3 | +import dataclasses |
| 4 | + |
| 5 | +from ._misc import clear_from_params_empty # , require_capabilities |
| 6 | +from ._session import AsyncNcSessionBasic, NcSessionBasic |
| 7 | + |
| 8 | + |
| 9 | +@dataclasses.dataclass |
| 10 | +class WebhookInfo: |
| 11 | + """Information about the Webhook.""" |
| 12 | + |
| 13 | + def __init__(self, raw_data: dict): |
| 14 | + self._raw_data = raw_data |
| 15 | + |
| 16 | + @property |
| 17 | + def webhook_id(self) -> int: |
| 18 | + """`ID` of the webhook.""" |
| 19 | + return self._raw_data["id"] |
| 20 | + |
| 21 | + @property |
| 22 | + def app_id(self) -> str: |
| 23 | + """`ID` of the ExApp that registered webhook.""" |
| 24 | + return self._raw_data["appId"] if self._raw_data["appId"] else "" |
| 25 | + |
| 26 | + @property |
| 27 | + def user_id(self) -> str: |
| 28 | + """`UserID` if webhook was registered in user context.""" |
| 29 | + return self._raw_data["userId"] if self._raw_data["userId"] else "" |
| 30 | + |
| 31 | + @property |
| 32 | + def http_method(self) -> str: |
| 33 | + """HTTP method used to call webhook.""" |
| 34 | + return self._raw_data["httpMethod"] |
| 35 | + |
| 36 | + @property |
| 37 | + def uri(self) -> str: |
| 38 | + """URL address that will be called for this webhook.""" |
| 39 | + return self._raw_data["uri"] |
| 40 | + |
| 41 | + @property |
| 42 | + def event(self) -> str: |
| 43 | + """Nextcloud PHP event that triggers this webhook.""" |
| 44 | + return self._raw_data["event"] |
| 45 | + |
| 46 | + @property |
| 47 | + def event_filter(self): |
| 48 | + """Mongo filter to apply to the serialized data to decide if firing.""" |
| 49 | + return self._raw_data["eventFilter"] |
| 50 | + |
| 51 | + @property |
| 52 | + def user_id_filter(self) -> str: |
| 53 | + """Currently unknown.""" |
| 54 | + return self._raw_data["userIdFilter"] |
| 55 | + |
| 56 | + @property |
| 57 | + def headers(self) -> dict: |
| 58 | + """Headers that should be added to request when calling webhook.""" |
| 59 | + return self._raw_data["headers"] if self._raw_data["headers"] else {} |
| 60 | + |
| 61 | + @property |
| 62 | + def auth_method(self) -> str: |
| 63 | + """Currently unknown.""" |
| 64 | + return self._raw_data["authMethod"] |
| 65 | + |
| 66 | + @property |
| 67 | + def auth_data(self) -> dict: |
| 68 | + """Currently unknown.""" |
| 69 | + return self._raw_data["authData"] if self._raw_data["authData"] else {} |
| 70 | + |
| 71 | + def __repr__(self): |
| 72 | + return f"<{self.__class__.__name__} id={self.webhook_id}, event={self.event}>" |
| 73 | + |
| 74 | + |
| 75 | +class _WebhooksAPI: |
| 76 | + """The class provides the application management API on the Nextcloud server.""" |
| 77 | + |
| 78 | + _ep_base: str = "/ocs/v1.php/apps/webhook_listeners/api/v1/webhooks" |
| 79 | + |
| 80 | + def __init__(self, session: NcSessionBasic): |
| 81 | + self._session = session |
| 82 | + |
| 83 | + def get_list(self, uri_filter: str = "") -> list[WebhookInfo]: |
| 84 | + params = {"uri": uri_filter} if uri_filter else {} |
| 85 | + return [WebhookInfo(i) for i in self._session.ocs("GET", f"{self._ep_base}", params=params)] |
| 86 | + |
| 87 | + def get_entry(self, webhook_id: int) -> WebhookInfo: |
| 88 | + return WebhookInfo(self._session.ocs("GET", f"{self._ep_base}/{webhook_id}")) |
| 89 | + |
| 90 | + def register( |
| 91 | + self, |
| 92 | + http_method: str, |
| 93 | + uri: str, |
| 94 | + event: str, |
| 95 | + event_filter: dict | None = None, |
| 96 | + user_id_filter: str = "", |
| 97 | + headers: dict | None = None, |
| 98 | + auth_method: str = "none", |
| 99 | + auth_data: dict | None = None, |
| 100 | + ): |
| 101 | + params = { |
| 102 | + "httpMethod": http_method, |
| 103 | + "uri": uri, |
| 104 | + "event": event, |
| 105 | + "eventFilter": event_filter, |
| 106 | + "userIdFilter": user_id_filter, |
| 107 | + "headers": headers, |
| 108 | + "authMethod": auth_method, |
| 109 | + "authData": auth_data, |
| 110 | + } |
| 111 | + clear_from_params_empty(["eventFilter", "userIdFilter", "headers", "authMethod", "authData"], params) |
| 112 | + return WebhookInfo(self._session.ocs("POST", f"{self._ep_base}", json=params)) |
| 113 | + |
| 114 | + def update( |
| 115 | + self, |
| 116 | + webhook_id: int, |
| 117 | + http_method: str, |
| 118 | + uri: str, |
| 119 | + event: str, |
| 120 | + event_filter: dict | None = None, |
| 121 | + user_id_filter: str = "", |
| 122 | + headers: dict | None = None, |
| 123 | + auth_method: str = "none", |
| 124 | + auth_data: dict | None = None, |
| 125 | + ): |
| 126 | + params = { |
| 127 | + "id": webhook_id, |
| 128 | + "httpMethod": http_method, |
| 129 | + "uri": uri, |
| 130 | + "event": event, |
| 131 | + "eventFilter": event_filter, |
| 132 | + "userIdFilter": user_id_filter, |
| 133 | + "headers": headers, |
| 134 | + "authMethod": auth_method, |
| 135 | + "authData": auth_data, |
| 136 | + } |
| 137 | + clear_from_params_empty(["eventFilter", "userIdFilter", "headers", "authMethod", "authData"], params) |
| 138 | + return WebhookInfo(self._session.ocs("POST", f"{self._ep_base}/{webhook_id}", json=params)) |
| 139 | + |
| 140 | + def unregister(self, webhook_id: int) -> bool: |
| 141 | + return self._session.ocs("DELETE", f"{self._ep_base}/{webhook_id}") |
| 142 | + |
| 143 | + |
| 144 | +class _AsyncWebhooksAPI: |
| 145 | + """The class provides the async application management API on the Nextcloud server.""" |
| 146 | + |
| 147 | + _ep_base: str = "/ocs/v1.php/webhooks" |
| 148 | + |
| 149 | + def __init__(self, session: AsyncNcSessionBasic): |
| 150 | + self._session = session |
| 151 | + |
| 152 | + async def get_list(self, uri_filter: str = "") -> list[WebhookInfo]: |
| 153 | + params = {"uri": uri_filter} if uri_filter else {} |
| 154 | + return [WebhookInfo(i) for i in await self._session.ocs("GET", f"{self._ep_base}", params=params)] |
| 155 | + |
| 156 | + async def get_entry(self, webhook_id: int) -> WebhookInfo: |
| 157 | + return WebhookInfo(await self._session.ocs("GET", f"{self._ep_base}/{webhook_id}")) |
| 158 | + |
| 159 | + async def register( |
| 160 | + self, |
| 161 | + http_method: str, |
| 162 | + uri: str, |
| 163 | + event: str, |
| 164 | + event_filter: dict | None = None, |
| 165 | + user_id_filter: str = "", |
| 166 | + headers: dict | None = None, |
| 167 | + auth_method: str = "none", |
| 168 | + auth_data: dict | None = None, |
| 169 | + ): |
| 170 | + params = { |
| 171 | + "httpMethod": http_method, |
| 172 | + "uri": uri, |
| 173 | + "event": event, |
| 174 | + "eventFilter": event_filter, |
| 175 | + "userIdFilter": user_id_filter, |
| 176 | + "headers": headers, |
| 177 | + "authMethod": auth_method, |
| 178 | + "authData": auth_data, |
| 179 | + } |
| 180 | + clear_from_params_empty(["eventFilter", "userIdFilter", "headers", "authMethod", "authData"], params) |
| 181 | + return WebhookInfo(await self._session.ocs("POST", f"{self._ep_base}", json=params)) |
| 182 | + |
| 183 | + async def update( |
| 184 | + self, |
| 185 | + webhook_id: int, |
| 186 | + http_method: str, |
| 187 | + uri: str, |
| 188 | + event: str, |
| 189 | + event_filter: dict | None = None, |
| 190 | + user_id_filter: str = "", |
| 191 | + headers: dict | None = None, |
| 192 | + auth_method: str = "none", |
| 193 | + auth_data: dict | None = None, |
| 194 | + ): |
| 195 | + params = { |
| 196 | + "id": webhook_id, |
| 197 | + "httpMethod": http_method, |
| 198 | + "uri": uri, |
| 199 | + "event": event, |
| 200 | + "eventFilter": event_filter, |
| 201 | + "userIdFilter": user_id_filter, |
| 202 | + "headers": headers, |
| 203 | + "authMethod": auth_method, |
| 204 | + "authData": auth_data, |
| 205 | + } |
| 206 | + clear_from_params_empty(["eventFilter", "userIdFilter", "headers", "authMethod", "authData"], params) |
| 207 | + return WebhookInfo(await self._session.ocs("POST", f"{self._ep_base}/{webhook_id}", json=params)) |
| 208 | + |
| 209 | + async def unregister(self, webhook_id: int) -> bool: |
| 210 | + return await self._session.ocs("DELETE", f"{self._ep_base}/{webhook_id}") |
0 commit comments