-
Notifications
You must be signed in to change notification settings - Fork 980
Expand file tree
/
Copy pathconnection_config.py
More file actions
151 lines (120 loc) · 4.93 KB
/
Copy pathconnection_config.py
File metadata and controls
151 lines (120 loc) · 4.93 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
import logging
import os
from typing import Dict, Optional, TypedDict
from typing_extensions import Unpack
from e2b.api.metadata import package_version
from e2b.connection_config import ProxyTypes
REQUEST_TIMEOUT: float = 60.0 # 60 seconds
# Timeout for volume file transfers, which stream large bodies and so must not
# inherit the short REQUEST_TIMEOUT. (Sandbox filesystem streaming instead
# bounds each chunk by the request timeout and leaves the total to the server.)
FILE_TIMEOUT: float = 3600.0 # 1 hour
# Idle bound for every read on the volume content transports: the transfer is
# aborted when no bytes at all arrive for this long. It resets on each chunk,
# so it never limits total transfer time — only a fully stalled connection.
# Matches the JS SDK's default stream idle timeout (REQUEST_TIMEOUT_MS).
READ_TIMEOUT: float = 60.0 # 60 seconds
class VolumeApiParams(TypedDict, total=False):
"""
Parameters for requests made to the volume content API.
"""
domain: Optional[str]
"""Domain to use for the volume API, defaults to `E2B_DOMAIN` or `e2b.app`."""
debug: Optional[bool]
"""Whether to use debug mode, defaults to `E2B_DEBUG` environment variable."""
request_timeout: Optional[float]
"""Timeout for the request in **seconds**, defaults to 60 seconds."""
headers: Optional[Dict[str, str]]
"""Additional headers to send with the request."""
token: Optional[str]
"""Volume auth token used for `Authorization: Bearer <token>`."""
api_url: Optional[str]
"""URL to use for the volume API, defaults to `E2B_VOLUME_API_URL` or `https://api.<domain>`."""
proxy: Optional[ProxyTypes]
"""Proxy to use for the request."""
logger: Optional[logging.Logger]
"""Logger used for request and response logging. Accepts a standard library `logging.Logger`."""
class VolumeConnectionConfig:
"""
Configuration for the volume content API.
Uses bearer token authentication and defaults to the volume content host.
"""
@staticmethod
def _domain():
return os.getenv("E2B_DOMAIN") or "e2b.app"
@staticmethod
def _debug():
return os.getenv("E2B_DEBUG", "false").lower() == "true"
@staticmethod
def _volume_api_url():
return os.getenv("E2B_VOLUME_API_URL")
@staticmethod
def _get_request_timeout(
default_timeout: Optional[float],
request_timeout: Optional[float],
):
if request_timeout == 0:
return None
elif request_timeout is not None:
return request_timeout
else:
return default_timeout
def __init__(
self,
domain: Optional[str] = None,
debug: Optional[bool] = None,
token: Optional[str] = None,
api_url: Optional[str] = None,
request_timeout: Optional[float] = None,
headers: Optional[Dict[str, str]] = None,
proxy: Optional[ProxyTypes] = None,
logger: Optional[logging.Logger] = None,
):
self.logger = logger
self.domain = domain or self._domain()
self.debug = debug if debug is not None else self._debug()
self.api_url = (
api_url
or self._volume_api_url()
or ("http://localhost:8080" if self.debug else f"https://api.{self.domain}")
)
self.access_token = token
self.token = self.access_token
self.proxy = proxy
self.headers = dict(headers) if headers else {}
self.headers["User-Agent"] = f"e2b-python-sdk/{package_version}"
self.request_timeout = self._get_request_timeout(
REQUEST_TIMEOUT, request_timeout
)
def get_request_timeout(self, request_timeout: Optional[float] = None):
return self._get_request_timeout(self.request_timeout, request_timeout)
def get_api_params(
self,
**opts: Unpack[VolumeApiParams],
) -> dict:
"""
Get request parameters for the volume content API.
"""
domain = opts.get("domain")
debug = opts.get("debug")
headers = opts.get("headers")
request_timeout = opts.get("request_timeout")
token = opts.get("token")
api_url = opts.get("api_url")
proxy = opts.get("proxy")
logger = opts.get("logger")
req_headers = self.headers.copy()
if headers is not None:
req_headers.update(headers)
return dict(
VolumeApiParams(
domain=domain if domain is not None else self.domain,
debug=debug if debug is not None else self.debug,
token=token if token is not None else self.token,
api_url=api_url if api_url is not None else self.api_url,
request_timeout=self.get_request_timeout(request_timeout),
headers=req_headers,
proxy=proxy if proxy is not None else self.proxy,
logger=logger if logger is not None else self.logger,
)
)