Metadata for a payload stored locally or uploaded to a provider.
| 42 | |
| 43 | @dataclass |
| 44 | class AttachmentRef: |
| 45 | """Metadata for a payload stored locally or uploaded to a provider.""" |
| 46 | |
| 47 | attachment_id: str |
| 48 | mime_type: Optional[str] = None |
| 49 | name: Optional[str] = None |
| 50 | size: Optional[int] = None |
| 51 | sha256: Optional[str] = None |
| 52 | local_path: Optional[str] = None |
| 53 | remote_file_id: Optional[str] = None |
| 54 | data_uri: Optional[str] = None |
| 55 | metadata: Dict[str, Any] = field(default_factory=dict) |
| 56 | |
| 57 | def to_dict(self, include_data: bool = True) -> Dict[str, Any]: |
| 58 | payload: Dict[str, Any] = { |
| 59 | "attachment_id": self.attachment_id, |
| 60 | "mime_type": self.mime_type, |
| 61 | "name": self.name, |
| 62 | "size": self.size, |
| 63 | "sha256": self.sha256, |
| 64 | "local_path": self.local_path, |
| 65 | "remote_file_id": self.remote_file_id, |
| 66 | "metadata": dict(self.metadata), |
| 67 | } |
| 68 | if include_data and self.data_uri: |
| 69 | payload["data_uri"] = self.data_uri |
| 70 | elif self.data_uri and not include_data: |
| 71 | payload["data_uri"] = "[omitted]" |
| 72 | # Remove keys that are None to keep payload compact |
| 73 | return {key: value for key, value in payload.items() if value is not None and value != {}} |
| 74 | |
| 75 | @classmethod |
| 76 | def from_dict(cls, data: Dict[str, Any]) -> "AttachmentRef": |
| 77 | return cls( |
| 78 | attachment_id=data.get("attachment_id", ""), |
| 79 | mime_type=data.get("mime_type"), |
| 80 | name=data.get("name"), |
| 81 | size=data.get("size"), |
| 82 | sha256=data.get("sha256"), |
| 83 | local_path=data.get("local_path"), |
| 84 | remote_file_id=data.get("remote_file_id"), |
| 85 | data_uri=data.get("data_uri"), |
| 86 | metadata=data.get("metadata") or {}, |
| 87 | ) |
| 88 | |
| 89 | def copy(self) -> "AttachmentRef": |
| 90 | return AttachmentRef( |
| 91 | attachment_id=self.attachment_id, |
| 92 | mime_type=self.mime_type, |
| 93 | name=self.name, |
| 94 | size=self.size, |
| 95 | sha256=self.sha256, |
| 96 | local_path=self.local_path, |
| 97 | remote_file_id=self.remote_file_id, |
| 98 | data_uri=self.data_uri, |
| 99 | metadata=dict(self.metadata), |
| 100 | ) |
| 101 |
no outgoing calls
no test coverage detected