|
| 1 | +import datetime as dt |
| 2 | +from typing import Iterable, Mapping, Optional, Union |
| 3 | + |
| 4 | +from .utils import ApiComponent |
| 5 | + |
| 6 | + |
| 7 | +class Subscriptions(ApiComponent): |
| 8 | + """Subscription operations for Microsoft Graph webhooks.""" |
| 9 | + |
| 10 | + _endpoints = { |
| 11 | + "subscriptions": "/subscriptions", |
| 12 | + } |
| 13 | + |
| 14 | + def __init__(self, *, parent=None, con=None, **kwargs): |
| 15 | + if parent and con: |
| 16 | + raise ValueError("Need a parent or a connection but not both") |
| 17 | + self.con = parent.con if parent else con |
| 18 | + |
| 19 | + main_resource = kwargs.pop("main_resource", None) or ( |
| 20 | + getattr(parent, "main_resource", None) if parent else None |
| 21 | + ) |
| 22 | + |
| 23 | + super().__init__( |
| 24 | + protocol=parent.protocol if parent else kwargs.get("protocol"), |
| 25 | + main_resource=main_resource, |
| 26 | + ) |
| 27 | + |
| 28 | + def _build_subscription_url(self, subscription_id: Optional[str] = None) -> str: |
| 29 | + """Build the Microsoft Graph subscriptions endpoint.""" |
| 30 | + endpoint = self._endpoints.get("subscriptions") |
| 31 | + if endpoint is None: |
| 32 | + raise ValueError("Subscriptions endpoint is not configured.") |
| 33 | + base_url = self.protocol.service_url.rstrip("/") |
| 34 | + if subscription_id: |
| 35 | + return f"{base_url}{endpoint}/{subscription_id}" |
| 36 | + return f"{base_url}{endpoint}" |
| 37 | + |
| 38 | + @staticmethod |
| 39 | + def _format_subscription_expiration( |
| 40 | + expiration_datetime: Optional[dt.datetime] = None, |
| 41 | + expiration_minutes: Optional[int] = None, |
| 42 | + ) -> str: |
| 43 | + """Return an ISO 8601 UTC expiration string as required by Graph webhooks.""" |
| 44 | + if expiration_datetime and expiration_minutes is not None: |
| 45 | + raise ValueError( |
| 46 | + "Provide either expiration_datetime or expiration_minutes, not both." |
| 47 | + ) |
| 48 | + if expiration_datetime is None: |
| 49 | + minutes = expiration_minutes if expiration_minutes is not None else 60 |
| 50 | + if minutes <= 0: |
| 51 | + raise ValueError("expiration_minutes must be a positive integer.") |
| 52 | + expiration_datetime = dt.datetime.now(dt.timezone.utc) + dt.timedelta( |
| 53 | + minutes=minutes |
| 54 | + ) |
| 55 | + else: |
| 56 | + if expiration_datetime.tzinfo is None: |
| 57 | + expiration_datetime = expiration_datetime.replace(tzinfo=dt.timezone.utc) |
| 58 | + else: |
| 59 | + expiration_datetime = expiration_datetime.astimezone(dt.timezone.utc) |
| 60 | + return expiration_datetime.isoformat(timespec="microseconds").replace("+00:00", "Z") |
| 61 | + |
| 62 | + @staticmethod |
| 63 | + def _stringify_change_type(change_type: Union[str, Iterable[str]]) -> str: |
| 64 | + """Normalize changeType into the comma-separated string Graph expects.""" |
| 65 | + if isinstance(change_type, str): |
| 66 | + value = change_type.strip() |
| 67 | + else: |
| 68 | + try: |
| 69 | + parts = [str(part).strip() for part in change_type] |
| 70 | + except TypeError as exc: |
| 71 | + raise ValueError( |
| 72 | + "change_type must be a string or an iterable of strings." |
| 73 | + ) from exc |
| 74 | + value = ",".join(part for part in parts if part) |
| 75 | + if not value: |
| 76 | + raise ValueError("change_type must contain at least one value.") |
| 77 | + return value |
| 78 | + |
| 79 | + def create_subscription( |
| 80 | + self, |
| 81 | + notification_url: str, |
| 82 | + resource: Optional[str] = None, |
| 83 | + change_type: Union[str, Iterable[str]] = "created", |
| 84 | + *, |
| 85 | + expiration_datetime: Optional[dt.datetime] = None, |
| 86 | + expiration_minutes: Optional[int] = None, |
| 87 | + client_state: Optional[str] = None, |
| 88 | + include_resource_data: Optional[bool] = None, |
| 89 | + encryption_certificate: Optional[str] = None, |
| 90 | + encryption_certificate_id: Optional[str] = None, |
| 91 | + lifecycle_notification_url: Optional[str] = None, |
| 92 | + latest_supported_tls_version: Optional[str] = None, |
| 93 | + additional_data: Optional[Mapping[str, object]] = None, |
| 94 | + **request_kwargs, |
| 95 | + ) -> Optional[dict]: |
| 96 | + """Create a Microsoft Graph webhook subscription. |
| 97 | +
|
| 98 | + See Documentation.md for webhook setup requirements. |
| 99 | + """ |
| 100 | + if not notification_url: |
| 101 | + raise ValueError("notification_url must be provided.") |
| 102 | + |
| 103 | + resource = resource or self.main_resource |
| 104 | + if not resource: |
| 105 | + raise ValueError("resource must be provided.") |
| 106 | + if not resource.startswith("/"): |
| 107 | + resource = f"/{resource}" |
| 108 | + |
| 109 | + expiration_value = self._format_subscription_expiration( |
| 110 | + expiration_datetime=expiration_datetime, |
| 111 | + expiration_minutes=expiration_minutes, |
| 112 | + ) |
| 113 | + change_type_value = self._stringify_change_type(change_type) |
| 114 | + |
| 115 | + payload = { |
| 116 | + self._cc("change_type"): change_type_value, |
| 117 | + self._cc("notification_url"): notification_url, |
| 118 | + self._cc("resource"): resource, |
| 119 | + self._cc("expiration_date_time"): expiration_value, |
| 120 | + } |
| 121 | + |
| 122 | + if client_state is not None: |
| 123 | + payload[self._cc("client_state")] = client_state |
| 124 | + if include_resource_data is not None: |
| 125 | + payload[self._cc("include_resource_data")] = include_resource_data |
| 126 | + if encryption_certificate is not None: |
| 127 | + payload[self._cc("encryption_certificate")] = encryption_certificate |
| 128 | + if encryption_certificate_id is not None: |
| 129 | + payload[self._cc("encryption_certificate_id")] = encryption_certificate_id |
| 130 | + if lifecycle_notification_url is not None: |
| 131 | + payload[self._cc("lifecycle_notification_url")] = lifecycle_notification_url |
| 132 | + if latest_supported_tls_version is not None: |
| 133 | + payload[ |
| 134 | + self._cc("latest_supported_tls_version") |
| 135 | + ] = latest_supported_tls_version |
| 136 | + if additional_data: |
| 137 | + if not isinstance(additional_data, Mapping): |
| 138 | + raise ValueError("additional_data must be a mapping if provided.") |
| 139 | + payload.update({str(key): value for key, value in additional_data.items()}) |
| 140 | + |
| 141 | + url = self._build_subscription_url() |
| 142 | + response = self.con.post(url, data=payload, **request_kwargs) |
| 143 | + |
| 144 | + if not response: |
| 145 | + return None |
| 146 | + |
| 147 | + return response.json() |
| 148 | + |
| 149 | + def renew_subscription( |
| 150 | + self, |
| 151 | + subscription_id: str, |
| 152 | + *, |
| 153 | + expiration_datetime: Optional[dt.datetime] = None, |
| 154 | + expiration_minutes: Optional[int] = None, |
| 155 | + **request_kwargs, |
| 156 | + ) -> Optional[dict]: |
| 157 | + """Renew an existing webhook subscription.""" |
| 158 | + if not subscription_id: |
| 159 | + raise ValueError("subscription_id must be provided.") |
| 160 | + |
| 161 | + expiration_value = self._format_subscription_expiration( |
| 162 | + expiration_datetime=expiration_datetime, |
| 163 | + expiration_minutes=expiration_minutes, |
| 164 | + ) |
| 165 | + |
| 166 | + payload = { |
| 167 | + self._cc("expiration_date_time"): expiration_value, |
| 168 | + } |
| 169 | + |
| 170 | + url = self._build_subscription_url(subscription_id) |
| 171 | + response = self.con.patch(url, data=payload, **request_kwargs) |
| 172 | + |
| 173 | + if not response: |
| 174 | + return None |
| 175 | + |
| 176 | + return response.json() |
| 177 | + |
| 178 | + def delete_subscription( |
| 179 | + self, |
| 180 | + subscription_id: str, |
| 181 | + **request_kwargs, |
| 182 | + ) -> bool: |
| 183 | + """Delete an existing webhook subscription.""" |
| 184 | + if not subscription_id: |
| 185 | + raise ValueError("subscription_id must be provided.") |
| 186 | + |
| 187 | + url = self._build_subscription_url(subscription_id) |
| 188 | + response = self.con.delete(url, **request_kwargs) |
| 189 | + |
| 190 | + return bool(response) |
0 commit comments