-
Notifications
You must be signed in to change notification settings - Fork 892
Expand file tree
/
Copy pathapi.py
More file actions
60 lines (44 loc) · 1.48 KB
/
api.py
File metadata and controls
60 lines (44 loc) · 1.48 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
import httpx
import json
from e2b.exceptions import (
SandboxException,
NotFoundException,
AuthenticationException,
InvalidArgumentException,
NotEnoughSpaceException,
RateLimitException,
format_sandbox_timeout_exception,
)
ENVD_API_FILES_ROUTE = "/files"
ENVD_API_HEALTH_ROUTE = "/health"
def get_message(e: httpx.Response) -> str:
try:
message = e.json().get("message", e.text)
except json.JSONDecodeError:
message = e.text
return message
def handle_envd_api_exception(res: httpx.Response):
if res.is_success:
return
res.read()
return format_envd_api_exception(res.status_code, get_message(res))
async def ahandle_envd_api_exception(res: httpx.Response):
if res.is_success:
return
await res.aread()
return format_envd_api_exception(res.status_code, get_message(res))
def format_envd_api_exception(status_code: int, message: str):
if status_code == 400:
return InvalidArgumentException(message)
elif status_code == 401:
return AuthenticationException(message)
elif status_code == 404:
return NotFoundException(message)
elif status_code == 429:
return RateLimitException(f"{message}: The requests are being rate limited.")
elif status_code == 502:
return format_sandbox_timeout_exception(message)
elif status_code == 507:
return NotEnoughSpaceException(message)
else:
return SandboxException(f"{status_code}: {message}")