|
5 | 5 | from datetime import UTC, datetime |
6 | 6 | from typing import Any, Dict, List, Optional, Union |
7 | 7 | import urllib.parse |
| 8 | +import dataclasses |
8 | 9 |
|
9 | 10 | import httpx |
10 | 11 | from cryptography.hazmat.primitives.asymmetric import padding |
@@ -227,7 +228,7 @@ async def _deliver_activity( |
227 | 228 |
|
228 | 229 | if response.status_code >= 400: |
229 | 230 | logger.error(f"Failed to deliver activity to {inbox_url}: {response.status_code} {response.text}") |
230 | | - raise errors.ExternalServiceError( |
| 231 | + raise errors.ProgrammingError( |
231 | 232 | message=f"Failed to deliver activity to {inbox_url}: {response.status_code}" |
232 | 233 | ) |
233 | 234 |
|
@@ -288,23 +289,22 @@ async def _build_signature_headers( |
288 | 289 | "Accept": "application/activity+json", |
289 | 290 | } |
290 | 291 |
|
291 | | - def _to_dict(self, obj: Any) -> Dict[str, Any]: |
| 292 | + def _to_dict(self, obj: Any) -> Any: |
292 | 293 | """Convert an object to a dictionary.""" |
293 | 294 | if isinstance(obj, dict): |
294 | 295 | return {k: self._to_dict(v) for k, v in obj.items()} |
295 | 296 | elif isinstance(obj, list): |
296 | 297 | return [self._to_dict(item) for item in obj] |
297 | | - elif hasattr(obj, "__dataclass_fields__"): |
298 | | - # It's a dataclass |
| 298 | + elif dataclasses.is_dataclass(obj) and not isinstance(obj, type): |
| 299 | + # Convert dataclass instance to dict |
299 | 300 | result = {} |
300 | | - for field_name in obj.__dataclass_fields__: |
301 | | - value = getattr(obj, field_name) |
302 | | - if value is not None: # Skip None values |
| 301 | + for field_name, field_value in dataclasses.asdict(obj).items(): |
| 302 | + if field_value is not None: # Skip None values |
303 | 303 | if field_name == "context": |
304 | 304 | # Special case for @context |
305 | | - result["@context"] = self._to_dict(value) |
| 305 | + result["@context"] = field_value |
306 | 306 | else: |
307 | | - result[field_name] = self._to_dict(value) |
| 307 | + result[field_name] = field_value |
308 | 308 | return result |
309 | 309 | elif isinstance(obj, datetime): |
310 | 310 | return obj.isoformat() |
|
0 commit comments