-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvanish.py
More file actions
322 lines (265 loc) · 9.1 KB
/
Copy pathvanish.py
File metadata and controls
322 lines (265 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
"""
Vanish Email API Client - Python Library
A simple, lightweight client for the Vanish temporary email service.
"""
import json
import urllib.request
import urllib.parse
import urllib.error
from dataclasses import dataclass
from typing import Optional, List
from datetime import datetime
@dataclass
class AttachmentMeta:
"""Metadata for an email attachment."""
id: str
name: str
type: str
size: int
@dataclass
class EmailSummary:
"""Summary of an email in the mailbox list."""
id: str
sender: str
subject: str
text_preview: str
received_at: datetime
has_attachments: bool
@dataclass
class EmailDetail:
"""Full email details with attachments."""
id: str
sender: str
to: List[str]
subject: str
html: str
text: str
received_at: datetime
has_attachments: bool
attachments: List[AttachmentMeta]
@dataclass
class PaginatedEmailList:
"""Paginated list of emails."""
data: List[EmailSummary]
next_cursor: Optional[str]
total: int
class VanishError(Exception):
"""Base exception for Vanish API errors."""
def __init__(self, message: str, status_code: Optional[int] = None):
super().__init__(message)
self.status_code = status_code
class VanishClient:
"""
Client for interacting with the Vanish Email API.
Example:
client = VanishClient("https://api.vanish.host", api_key="your-key")
# Generate a temporary email
email = client.generate_email()
# List emails in the mailbox
emails = client.list_emails(email)
# Get a specific email
detail = client.get_email(emails.data[0].id)
"""
def __init__(self, base_url: str, api_key: Optional[str] = None, timeout: int = 30):
"""
Initialize the Vanish client.
Args:
base_url: Base URL of the Vanish API (e.g., "https://api.vanish.host")
api_key: Optional API key for authentication
timeout: Request timeout in seconds
"""
self.base_url = base_url.rstrip("/")
self.api_key = api_key
self.timeout = timeout
def _request(
self,
method: str,
path: str,
params: Optional[dict] = None,
body: Optional[dict] = None,
raw_response: bool = False
):
"""Make an HTTP request to the API."""
url = f"{self.base_url}{path}"
if params:
query = urllib.parse.urlencode({k: v for k, v in params.items() if v is not None})
if query:
url = f"{url}?{query}"
headers = {"Content-Type": "application/json"}
if self.api_key:
headers["Authorization"] = f"Bearer {self.api_key}"
data = json.dumps(body).encode("utf-8") if body else None
req = urllib.request.Request(url, data=data, headers=headers, method=method)
try:
with urllib.request.urlopen(req, timeout=self.timeout) as resp:
if raw_response:
return resp.read(), resp.headers
return json.loads(resp.read().decode("utf-8"))
except urllib.error.HTTPError as e:
try:
error_body = json.loads(e.read().decode("utf-8"))
message = error_body.get("error", str(e))
except (json.JSONDecodeError, UnicodeDecodeError):
message = str(e)
raise VanishError(message, e.code) from e
except urllib.error.URLError as e:
raise VanishError(f"Connection error: {e.reason}") from e
def get_domains(self) -> List[str]:
"""
Get list of available email domains.
Returns:
List of domain strings
"""
resp = self._request("GET", "/domains")
return resp["domains"]
def generate_email(
self,
domain: Optional[str] = None,
prefix: Optional[str] = None
) -> str:
"""
Generate a unique temporary email address.
Args:
domain: Optional specific domain to use
prefix: Optional prefix for the email address
Returns:
Generated email address string
"""
body = {}
if domain:
body["domain"] = domain
if prefix:
body["prefix"] = prefix
resp = self._request("POST", "/mailbox", body=body if body else None)
return resp["email"]
def list_emails(
self,
address: str,
limit: int = 20,
cursor: Optional[str] = None
) -> PaginatedEmailList:
"""
List emails for a mailbox address.
Args:
address: Email address to list emails for
limit: Maximum number of emails to return (1-100)
cursor: Pagination cursor for next page
Returns:
Paginated list of email summaries
"""
encoded_address = urllib.parse.quote(address, safe="")
resp = self._request(
"GET",
f"/mailbox/{encoded_address}",
params={"limit": limit, "cursor": cursor}
)
emails = [
EmailSummary(
id=e["id"],
sender=e["from"],
subject=e["subject"],
text_preview=e["textPreview"],
received_at=datetime.fromisoformat(e["receivedAt"].replace("Z", "+00:00")),
has_attachments=e["hasAttachments"]
)
for e in resp["data"]
]
return PaginatedEmailList(
data=emails,
next_cursor=resp.get("nextCursor"),
total=resp["total"]
)
def get_email(self, email_id: str) -> EmailDetail:
"""
Get full details of a specific email.
Args:
email_id: UUID of the email
Returns:
Email details with attachments
"""
resp = self._request("GET", f"/email/{email_id}")
attachments = [
AttachmentMeta(
id=a["id"],
name=a["name"],
type=a["type"],
size=a["size"]
)
for a in resp.get("attachments", [])
]
return EmailDetail(
id=resp["id"],
sender=resp["from"],
to=resp["to"],
subject=resp["subject"],
html=resp["html"],
text=resp["text"],
received_at=datetime.fromisoformat(resp["receivedAt"].replace("Z", "+00:00")),
has_attachments=resp["hasAttachments"],
attachments=attachments
)
def get_attachment(self, email_id: str, attachment_id: str) -> tuple[bytes, dict]:
"""
Download an attachment.
Args:
email_id: UUID of the email
attachment_id: UUID of the attachment
Returns:
Tuple of (content bytes, headers dict)
"""
content, headers = self._request(
"GET",
f"/email/{email_id}/attachments/{attachment_id}",
raw_response=True
)
return content, dict(headers)
def delete_email(self, email_id: str) -> bool:
"""
Delete a specific email.
Args:
email_id: UUID of the email to delete
Returns:
True if successful
"""
resp = self._request("DELETE", f"/email/{email_id}")
return resp.get("success", False)
def delete_mailbox(self, address: str) -> int:
"""
Delete all emails in a mailbox.
Args:
address: Email address to delete all emails for
Returns:
Number of emails deleted
"""
encoded_address = urllib.parse.quote(address, safe="")
resp = self._request("DELETE", f"/mailbox/{encoded_address}")
return resp.get("deleted", 0)
def poll_for_emails(
self,
address: str,
timeout: int = 60,
interval: int = 5,
initial_count: int = 0
) -> Optional[EmailSummary]:
"""
Poll for new emails until one arrives or timeout.
Args:
address: Email address to poll
timeout: Maximum time to wait in seconds
interval: Check interval in seconds
initial_count: Initial email count to compare against
Returns:
First new email if found, None if timeout
"""
import time
start = time.time()
while time.time() - start < timeout:
result = self.list_emails(address, limit=1)
if result.total > initial_count and result.data:
return result.data[0]
time.sleep(interval)
return None
# Convenience function for quick usage
def create_client(base_url: str, api_key: Optional[str] = None) -> VanishClient:
"""Create a Vanish client with the given configuration."""
return VanishClient(base_url, api_key)