botversion-sdk 2.2.2__py3-none-any.whl → 2.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- botversion_sdk/__init__.py +16 -287
- botversion_sdk/scanner_sdk/__init__.py +288 -0
- botversion_sdk/scanner_sdk/bin/__init__.py +1 -0
- botversion_sdk/scanner_sdk/bin/scan_endpoints.py +114 -0
- botversion_sdk/{client.py → scanner_sdk/client.py} +1 -1
- botversion_sdk/{interceptor.py → scanner_sdk/interceptor.py} +52 -17
- botversion_sdk/scanner_sdk/scan/__init__.py +2 -0
- botversion_sdk/scanner_sdk/scan/aiohttp.py +101 -0
- botversion_sdk/scanner_sdk/scan/bottle.py +136 -0
- botversion_sdk/scanner_sdk/scan/cherrypy.py +108 -0
- botversion_sdk/scanner_sdk/scan/common.py +144 -0
- botversion_sdk/scanner_sdk/scan/django.py +438 -0
- botversion_sdk/scanner_sdk/scan/django_static_bodies.py +1163 -0
- botversion_sdk/scanner_sdk/scan/django_static_urls.py +1341 -0
- botversion_sdk/scanner_sdk/scan/django_static_views.py +1073 -0
- botversion_sdk/scanner_sdk/scan/falcon.py +121 -0
- botversion_sdk/scanner_sdk/scan/fastapi.py +217 -0
- botversion_sdk/scanner_sdk/scan/fastapi_static.py +1292 -0
- botversion_sdk/scanner_sdk/scan/flask.py +1373 -0
- botversion_sdk/scanner_sdk/scan/multipart.py +229 -0
- botversion_sdk/scanner_sdk/scan/pyramid.py +120 -0
- botversion_sdk/scanner_sdk/scan/response_shapes.py +273 -0
- botversion_sdk/scanner_sdk/scan/sanic.py +63 -0
- botversion_sdk/scanner_sdk/scan/sanic_static.py +657 -0
- botversion_sdk/scanner_sdk/scan/starlette.py +50 -0
- botversion_sdk/scanner_sdk/scan/starlette_static.py +1286 -0
- botversion_sdk/scanner_sdk/scan/static_common.py +475 -0
- botversion_sdk/scanner_sdk/scan/tornado.py +133 -0
- botversion_sdk/scanner_sdk/scanner.py +116 -0
- botversion_sdk/undo/__init__.py +103 -0
- botversion_sdk/undo/__main__.py +4 -0
- botversion_sdk/undo/bin/__init__.py +1 -0
- botversion_sdk/undo/bin/scan.py +16 -0
- botversion_sdk/undo/lib/__init__.py +1 -0
- botversion_sdk/undo/lib/action_id.py +37 -0
- botversion_sdk/undo/lib/capture_guard.py +18 -0
- botversion_sdk/undo/lib/capture_mongo.py +205 -0
- botversion_sdk/undo/lib/capture_mssql.py +504 -0
- botversion_sdk/undo/lib/capture_mysql.py +485 -0
- botversion_sdk/undo/lib/capture_sql.py +546 -0
- botversion_sdk/undo/lib/capture_sqlite.py +527 -0
- botversion_sdk/undo/lib/gate.py +65 -0
- botversion_sdk/undo/lib/hooks/asyncpg.py +177 -0
- botversion_sdk/undo/lib/hooks/mongodb.py +62 -0
- botversion_sdk/undo/lib/hooks/mssql.py +139 -0
- botversion_sdk/undo/lib/hooks/mysql.py +112 -0
- botversion_sdk/undo/lib/hooks/psycopg.py +97 -0
- botversion_sdk/undo/lib/hooks/psycopg2.py +141 -0
- botversion_sdk/undo/lib/hooks/psycopg_async.py +96 -0
- botversion_sdk/undo/lib/hooks/sqlite.py +96 -0
- botversion_sdk/undo/lib/loader_watch.py +67 -0
- botversion_sdk/undo/lib/log.py +14 -0
- botversion_sdk/undo/lib/mssql_sql.py +156 -0
- botversion_sdk/undo/lib/mysql_sql.py +96 -0
- botversion_sdk/undo/lib/refuse.py +6 -0
- botversion_sdk/undo/lib/report.py +35 -0
- botversion_sdk/undo/lib/requester.py +33 -0
- botversion_sdk/undo/lib/scanner.py +92 -0
- botversion_sdk/undo/lib/setup_check.py +132 -0
- botversion_sdk/undo/lib/snapshot_crypto.py +112 -0
- botversion_sdk/undo/lib/snapshot_sql.py +23 -0
- botversion_sdk/undo/lib/snapshot_store.py +141 -0
- botversion_sdk/undo/lib/snapshot_store_mongo.py +95 -0
- botversion_sdk/undo/lib/sql_classify.py +175 -0
- botversion_sdk/undo/lib/sqlite_sql.py +262 -0
- botversion_sdk/undo/lib/steps.py +22 -0
- botversion_sdk/undo/lib/tagger.py +422 -0
- botversion_sdk/undo/lib/undo_mongo.py +134 -0
- botversion_sdk/undo/lib/undo_mssql.py +248 -0
- botversion_sdk/undo/lib/undo_mysql.py +216 -0
- botversion_sdk/undo/lib/undo_pg.py +244 -0
- botversion_sdk/undo/lib/undo_route.py +132 -0
- botversion_sdk/undo/lib/undo_sqlite.py +227 -0
- botversion_sdk/undo/register.py +5 -0
- botversion_sdk-2.3.0.dist-info/METADATA +84 -0
- botversion_sdk-2.3.0.dist-info/RECORD +80 -0
- {botversion_sdk-2.2.2.dist-info → botversion_sdk-2.3.0.dist-info}/WHEEL +1 -1
- botversion_sdk-2.3.0.dist-info/entry_points.txt +3 -0
- botversion_sdk/bin/scan_endpoints.py +0 -59
- botversion_sdk/scanner.py +0 -2317
- botversion_sdk-2.2.2.dist-info/METADATA +0 -33
- botversion_sdk-2.2.2.dist-info/RECORD +0 -11
- botversion_sdk-2.2.2.dist-info/entry_points.txt +0 -2
- /botversion_sdk/{bin → undo/lib/hooks}/__init__.py +0 -0
- {botversion_sdk-2.2.2.dist-info → botversion_sdk-2.3.0.dist-info}/top_level.txt +0 -0
botversion_sdk/__init__.py
CHANGED
|
@@ -1,290 +1,19 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import
|
|
5
|
-
import os
|
|
6
|
-
|
|
7
|
-
from .client import BotVersionClient
|
|
8
|
-
from .scanner import scan_routes
|
|
9
|
-
from .interceptor import (
|
|
10
|
-
attach_fastapi_interceptor,
|
|
11
|
-
attach_flask_interceptor,
|
|
12
|
-
attach_django_interceptor,
|
|
13
|
-
attach_starlette_interceptor,
|
|
14
|
-
attach_sanic_interceptor,
|
|
15
|
-
attach_falcon_interceptor,
|
|
16
|
-
attach_bottle_interceptor,
|
|
17
|
-
attach_aiohttp_interceptor,
|
|
18
|
-
attach_tornado_interceptor,
|
|
19
|
-
attach_pyramid_interceptor,
|
|
20
|
-
attach_cherrypy_interceptor,
|
|
21
|
-
)
|
|
22
|
-
|
|
23
|
-
def _classify_installation(framework):
|
|
24
|
-
"""
|
|
25
|
-
Python/PHP SDKs only ever run on a backend server — there is no such
|
|
26
|
-
thing as a Python or PHP frontend project. So unlike the JS SDK, this
|
|
27
|
-
is a simple two-way classification: either a supported backend
|
|
28
|
-
framework was detected, or it wasn't.
|
|
29
|
-
"""
|
|
30
|
-
if framework:
|
|
31
|
-
return "backend-only"
|
|
32
|
-
return "unknown"
|
|
33
|
-
|
|
34
|
-
_initialized = False
|
|
35
|
-
_client = None
|
|
36
|
-
_options = {}
|
|
37
|
-
_app = None
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
def _run_full_scan(app, framework, cwd, client, debug=False):
|
|
41
|
-
"""
|
|
42
|
-
Runs a full scan (backend endpoints + frontend routes) and reports
|
|
43
|
-
results to the platform. This used to run automatically on server
|
|
44
|
-
start / first request. Now it only runs when the BotVersion dashboard's
|
|
45
|
-
"Scan" button sends a request to this SDK's scan-trigger endpoint
|
|
46
|
-
(see interceptor.py). This fixes scans not happening on redeploys
|
|
47
|
-
(e.g. Vercel/serverless) since there's no "restart" event to hook into.
|
|
48
|
-
"""
|
|
49
|
-
result = {
|
|
50
|
-
"endpoint_count": 0,
|
|
51
|
-
"route_count": 0,
|
|
52
|
-
"project_type": None,
|
|
53
|
-
"detected_backend": framework,
|
|
54
|
-
"detected_frontend": None,
|
|
55
|
-
"sdk_language": "python",
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
try:
|
|
59
|
-
endpoints = []
|
|
60
|
-
if app is not None:
|
|
61
|
-
endpoints = scan_routes(app, framework)
|
|
62
|
-
elif framework == "django":
|
|
63
|
-
endpoints = scan_routes(None, "django")
|
|
64
|
-
|
|
65
|
-
result["endpoint_count"] = len(endpoints)
|
|
66
|
-
|
|
67
|
-
if endpoints:
|
|
68
|
-
client.register_endpoints_now(endpoints)
|
|
69
|
-
|
|
70
|
-
except Exception:
|
|
71
|
-
if debug:
|
|
72
|
-
import traceback
|
|
73
|
-
traceback.print_exc()
|
|
74
|
-
|
|
75
|
-
result["detected_frontend"] = None
|
|
76
|
-
result["project_type"] = _classify_installation(framework)
|
|
77
|
-
|
|
78
|
-
return result
|
|
1
|
+
# botversion_sdk/__init__.py
|
|
2
|
+
# Root entry point: the host app imports botversion_sdk and calls init().
|
|
3
|
+
# It starts the main SDK, and also the Undo SDK when undo=True is passed.
|
|
4
|
+
from .scanner_sdk import init as _scanner_init, get_endpoints, register_endpoint
|
|
79
5
|
|
|
80
6
|
|
|
81
7
|
def init(app=None, api_key=None, **options):
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
# Django — no app object needed:
|
|
95
|
-
botversion_sdk.init(api_key="YOUR_KEY")
|
|
96
|
-
"""
|
|
97
|
-
global _initialized, _client, _options, _app
|
|
98
|
-
|
|
99
|
-
# Restore from builtins if module was re-imported after hot reload
|
|
100
|
-
if getattr(builtins, "_botversion_client", None):
|
|
101
|
-
_client = builtins._botversion_client
|
|
102
|
-
_options = builtins._botversion_options
|
|
103
|
-
_initialized = True
|
|
104
|
-
# Re-attach interceptor after hot reload
|
|
105
|
-
framework = _detect_framework(app)
|
|
106
|
-
if framework and _client:
|
|
107
|
-
_cwd = _options.get("cwd", os.getcwd())
|
|
108
|
-
_debug = _options.get("debug", False)
|
|
109
|
-
interceptor_options = {
|
|
110
|
-
"exclude": _options.get("exclude", []),
|
|
111
|
-
"api_prefix": _options.get("api_prefix", None),
|
|
112
|
-
"debug": _debug,
|
|
113
|
-
"scan_secret": _options.get("api_key"),
|
|
114
|
-
"on_scan_requested": lambda: _run_full_scan(
|
|
115
|
-
app, framework, _cwd, _client, _debug
|
|
116
|
-
),
|
|
117
|
-
}
|
|
118
|
-
if framework == "fastapi":
|
|
119
|
-
attach_fastapi_interceptor(app, _client, interceptor_options)
|
|
120
|
-
elif framework == "flask":
|
|
121
|
-
attach_flask_interceptor(app, _client, interceptor_options)
|
|
122
|
-
elif framework == "django":
|
|
123
|
-
attach_django_interceptor(_client, interceptor_options)
|
|
124
|
-
# ── NEW ───────────────────────────────────────────────────────
|
|
125
|
-
elif framework == "starlette":
|
|
126
|
-
attach_starlette_interceptor(app, _client, interceptor_options)
|
|
127
|
-
elif framework == "sanic":
|
|
128
|
-
attach_sanic_interceptor(app, _client, interceptor_options)
|
|
129
|
-
elif framework == "falcon":
|
|
130
|
-
attach_falcon_interceptor(app, _client, interceptor_options)
|
|
131
|
-
elif framework == "bottle":
|
|
132
|
-
attach_bottle_interceptor(app, _client, interceptor_options)
|
|
133
|
-
elif framework == "aiohttp":
|
|
134
|
-
attach_aiohttp_interceptor(app, _client, interceptor_options)
|
|
135
|
-
elif framework == "tornado":
|
|
136
|
-
attach_tornado_interceptor(app, _client, interceptor_options)
|
|
137
|
-
elif framework == "pyramid":
|
|
138
|
-
attach_pyramid_interceptor(app, _client, interceptor_options)
|
|
139
|
-
elif framework == "cherrypy":
|
|
140
|
-
attach_cherrypy_interceptor(app, _client, interceptor_options)
|
|
141
|
-
return
|
|
142
|
-
|
|
143
|
-
_initialized = True
|
|
144
|
-
_options = dict(options)
|
|
145
|
-
_options["api_key"] = api_key
|
|
146
|
-
_app = app
|
|
147
|
-
|
|
148
|
-
debug = options.get("debug", False)
|
|
149
|
-
|
|
150
|
-
# ── Auto-detect framework ─────────────────────────────────────────────────
|
|
151
|
-
framework = _detect_framework(app)
|
|
152
|
-
|
|
153
|
-
if not framework:
|
|
154
|
-
_initialized = False
|
|
155
|
-
if debug:
|
|
156
|
-
print("[botversion] No supported framework detected. SDK not initialized.")
|
|
157
|
-
return
|
|
158
|
-
|
|
159
|
-
_client = BotVersionClient({
|
|
160
|
-
"api_key": api_key,
|
|
161
|
-
"platform_url": options.get("platform_url", "https://console.botversion.com"),
|
|
162
|
-
"debug": debug,
|
|
163
|
-
"timeout": options.get("timeout", 30),
|
|
164
|
-
"flush_delay": options.get("flush_delay", 3),
|
|
165
|
-
})
|
|
166
|
-
|
|
167
|
-
# Store globally so hot-reload can restore state
|
|
168
|
-
builtins._botversion_client = _client
|
|
169
|
-
builtins._botversion_options = _options
|
|
170
|
-
|
|
171
|
-
cwd = options.get("cwd", os.getcwd())
|
|
172
|
-
|
|
173
|
-
interceptor_options = {
|
|
174
|
-
"exclude": options.get("exclude", []),
|
|
175
|
-
"api_prefix": options.get("api_prefix", None),
|
|
176
|
-
"debug": debug,
|
|
177
|
-
"scan_secret": api_key,
|
|
178
|
-
"on_scan_requested": lambda: _run_full_scan(
|
|
179
|
-
app, framework, cwd, _client, debug
|
|
180
|
-
),
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
# ── Attach runtime interceptor ───────────────────────────────────────────
|
|
184
|
-
if framework == "fastapi":
|
|
185
|
-
attach_fastapi_interceptor(app, _client, interceptor_options)
|
|
186
|
-
elif framework == "flask":
|
|
187
|
-
attach_flask_interceptor(app, _client, interceptor_options)
|
|
188
|
-
elif framework == "django":
|
|
189
|
-
attach_django_interceptor(_client, interceptor_options)
|
|
190
|
-
# ── NEW ───────────────────────────────────────────────────────────────────
|
|
191
|
-
elif framework == "starlette":
|
|
192
|
-
attach_starlette_interceptor(app, _client, interceptor_options)
|
|
193
|
-
elif framework == "sanic":
|
|
194
|
-
attach_sanic_interceptor(app, _client, interceptor_options)
|
|
195
|
-
elif framework == "falcon":
|
|
196
|
-
attach_falcon_interceptor(app, _client, interceptor_options)
|
|
197
|
-
elif framework == "bottle":
|
|
198
|
-
attach_bottle_interceptor(app, _client, interceptor_options)
|
|
199
|
-
elif framework == "aiohttp":
|
|
200
|
-
attach_aiohttp_interceptor(app, _client, interceptor_options)
|
|
201
|
-
elif framework == "tornado":
|
|
202
|
-
attach_tornado_interceptor(app, _client, interceptor_options)
|
|
203
|
-
elif framework == "pyramid":
|
|
204
|
-
attach_pyramid_interceptor(app, _client, interceptor_options)
|
|
205
|
-
elif framework == "cherrypy":
|
|
206
|
-
attach_cherrypy_interceptor(app, _client, interceptor_options)
|
|
207
|
-
else:
|
|
208
|
-
return
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
def get_endpoints():
|
|
212
|
-
"""Get all registered endpoints for this workspace."""
|
|
213
|
-
if not _client:
|
|
214
|
-
raise RuntimeError("BotVersion SDK not initialized. Call botversion_sdk.init() first.")
|
|
215
|
-
return _client.get_endpoints()
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
def register_endpoint(endpoint):
|
|
219
|
-
"""Manually register a single endpoint."""
|
|
220
|
-
if not _client:
|
|
221
|
-
raise RuntimeError("BotVersion SDK not initialized.")
|
|
222
|
-
return _client.register_endpoints([endpoint])
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
# ── Framework auto-detection ─────────────────────────────────────────────────
|
|
226
|
-
|
|
227
|
-
def _detect_framework(app):
|
|
228
|
-
if app is not None:
|
|
229
|
-
app_type = type(app).__module__ + "." + type(app).__name__
|
|
230
|
-
|
|
231
|
-
# ── Existing ──────────────────────────────────────────────────────
|
|
232
|
-
if "fastapi" in app_type.lower():
|
|
233
|
-
return "fastapi"
|
|
234
|
-
if "flask" in app_type.lower():
|
|
235
|
-
return "flask"
|
|
236
|
-
|
|
237
|
-
# ── NEW ───────────────────────────────────────────────────────────
|
|
238
|
-
if "starlette" in app_type.lower():
|
|
239
|
-
return "starlette"
|
|
240
|
-
if "sanic" in app_type.lower():
|
|
241
|
-
return "sanic"
|
|
242
|
-
if "falcon" in app_type.lower():
|
|
243
|
-
return "falcon"
|
|
244
|
-
if "bottle" in app_type.lower():
|
|
245
|
-
return "bottle"
|
|
246
|
-
if "aiohttp" in app_type.lower():
|
|
247
|
-
return "aiohttp"
|
|
248
|
-
if "tornado" in app_type.lower():
|
|
249
|
-
return "tornado"
|
|
250
|
-
if "pyramid" in app_type.lower():
|
|
251
|
-
return "pyramid"
|
|
252
|
-
if "cherrypy" in app_type.lower():
|
|
253
|
-
return "cherrypy"
|
|
254
|
-
|
|
255
|
-
# ── No app object passed — try detecting from imported modules ────────
|
|
256
|
-
if app is None:
|
|
257
|
-
if "django" in sys.modules:
|
|
258
|
-
try:
|
|
259
|
-
from django.conf import settings
|
|
260
|
-
if settings.configured:
|
|
261
|
-
return "django"
|
|
262
|
-
except Exception:
|
|
263
|
-
pass
|
|
264
|
-
|
|
265
|
-
# ── Fallback — check sys.modules ──────────────────────────────────────
|
|
266
|
-
if "fastapi" in sys.modules:
|
|
267
|
-
return "fastapi"
|
|
268
|
-
if "flask" in sys.modules:
|
|
269
|
-
return "flask"
|
|
270
|
-
if "django" in sys.modules:
|
|
271
|
-
return "django"
|
|
272
|
-
# ── NEW fallbacks ─────────────────────────────────────────────────────
|
|
273
|
-
if "starlette" in sys.modules:
|
|
274
|
-
return "starlette"
|
|
275
|
-
if "sanic" in sys.modules:
|
|
276
|
-
return "sanic"
|
|
277
|
-
if "falcon" in sys.modules:
|
|
278
|
-
return "falcon"
|
|
279
|
-
if "bottle" in sys.modules:
|
|
280
|
-
return "bottle"
|
|
281
|
-
if "aiohttp" in sys.modules:
|
|
282
|
-
return "aiohttp"
|
|
283
|
-
if "tornado" in sys.modules:
|
|
284
|
-
return "tornado"
|
|
285
|
-
if "pyramid" in sys.modules:
|
|
286
|
-
return "pyramid"
|
|
287
|
-
if "cherrypy" in sys.modules:
|
|
288
|
-
return "cherrypy"
|
|
289
|
-
|
|
290
|
-
return None
|
|
8
|
+
# undo is ON unless the host passes undo=False
|
|
9
|
+
undo_enabled = options.pop("undo", True) # Undo is on unless the host passes undo=False
|
|
10
|
+
|
|
11
|
+
# Undo goes first so its request tagger is in place before the main SDK attaches
|
|
12
|
+
if undo_enabled:
|
|
13
|
+
try:
|
|
14
|
+
from . import undo as _undo
|
|
15
|
+
_undo.init(api_key=api_key, quiet=not options.get("debug", False))
|
|
16
|
+
except Exception as e:
|
|
17
|
+
pass # Undo must never break the host app or the main SDK
|
|
18
|
+
|
|
19
|
+
_scanner_init(app, api_key=api_key, **options)
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
# botversion-sdk-python/botversion-sdk/scanner-sdk/__init__.py
|
|
2
|
+
import sys
|
|
3
|
+
import threading
|
|
4
|
+
import builtins
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
from .client import BotVersionClient
|
|
8
|
+
from .scanner import scan_routes
|
|
9
|
+
from .interceptor import (
|
|
10
|
+
attach_fastapi_interceptor,
|
|
11
|
+
attach_flask_interceptor,
|
|
12
|
+
attach_django_interceptor,
|
|
13
|
+
attach_starlette_interceptor,
|
|
14
|
+
attach_sanic_interceptor,
|
|
15
|
+
attach_falcon_interceptor,
|
|
16
|
+
attach_bottle_interceptor,
|
|
17
|
+
attach_aiohttp_interceptor,
|
|
18
|
+
attach_tornado_interceptor,
|
|
19
|
+
attach_pyramid_interceptor,
|
|
20
|
+
attach_cherrypy_interceptor,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
def _classify_installation(framework):
|
|
24
|
+
"""
|
|
25
|
+
Python/PHP SDKs only ever run on a backend server — there is no such
|
|
26
|
+
thing as a Python or PHP frontend project. So unlike the JS SDK, this
|
|
27
|
+
is a simple two-way classification: either a supported backend
|
|
28
|
+
framework was detected, or it wasn't.
|
|
29
|
+
"""
|
|
30
|
+
if framework:
|
|
31
|
+
return "backend-only"
|
|
32
|
+
return "unknown"
|
|
33
|
+
|
|
34
|
+
_initialized = False
|
|
35
|
+
_client = None
|
|
36
|
+
_options = {}
|
|
37
|
+
_app = None
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _run_full_scan(app, framework, cwd, client, debug=False):
|
|
41
|
+
"""
|
|
42
|
+
Runs a full scan (backend endpoints + frontend routes) and reports
|
|
43
|
+
results to the platform. This used to run automatically on server
|
|
44
|
+
start / first request. Now it only runs when the BotVersion dashboard's
|
|
45
|
+
"Scan" button sends a request to this SDK's scan-trigger endpoint
|
|
46
|
+
(see interceptor.py). This fixes scans not happening on redeploys
|
|
47
|
+
(e.g. Vercel/serverless) since there's no "restart" event to hook into.
|
|
48
|
+
"""
|
|
49
|
+
result = {
|
|
50
|
+
"endpoint_count": 0,
|
|
51
|
+
"route_count": 0,
|
|
52
|
+
"project_type": None,
|
|
53
|
+
"detected_backend": framework,
|
|
54
|
+
"detected_frontend": None,
|
|
55
|
+
"sdk_language": "python",
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
try:
|
|
59
|
+
endpoints = []
|
|
60
|
+
if app is not None:
|
|
61
|
+
endpoints = scan_routes(app, framework)
|
|
62
|
+
elif framework == "django":
|
|
63
|
+
endpoints = scan_routes(None, "django")
|
|
64
|
+
|
|
65
|
+
result["endpoint_count"] = len(endpoints)
|
|
66
|
+
|
|
67
|
+
if endpoints:
|
|
68
|
+
client.register_endpoints_now(endpoints)
|
|
69
|
+
|
|
70
|
+
except Exception:
|
|
71
|
+
if debug:
|
|
72
|
+
import traceback
|
|
73
|
+
traceback.print_exc()
|
|
74
|
+
|
|
75
|
+
result["detected_frontend"] = None
|
|
76
|
+
result["project_type"] = _classify_installation(framework)
|
|
77
|
+
|
|
78
|
+
return result
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def init(app=None, api_key=None, **options):
|
|
82
|
+
"""
|
|
83
|
+
Initialize the BotVersion SDK.
|
|
84
|
+
|
|
85
|
+
Works for FastAPI, Flask, and Django — auto-detects the framework.
|
|
86
|
+
|
|
87
|
+
Usage:
|
|
88
|
+
# FastAPI
|
|
89
|
+
botversion_sdk.init(app, api_key="YOUR_KEY")
|
|
90
|
+
|
|
91
|
+
# Flask
|
|
92
|
+
botversion_sdk.init(app, api_key="YOUR_KEY")
|
|
93
|
+
|
|
94
|
+
# Django — no app object needed:
|
|
95
|
+
botversion_sdk.init(api_key="YOUR_KEY")
|
|
96
|
+
"""
|
|
97
|
+
global _initialized, _client, _options, _app
|
|
98
|
+
|
|
99
|
+
# Restore from builtins if module was re-imported after hot reload
|
|
100
|
+
if getattr(builtins, "_botversion_client", None):
|
|
101
|
+
_client = builtins._botversion_client
|
|
102
|
+
_options = builtins._botversion_options
|
|
103
|
+
_initialized = True
|
|
104
|
+
# Re-attach interceptor after hot reload
|
|
105
|
+
framework = _detect_framework(app)
|
|
106
|
+
if framework and _client:
|
|
107
|
+
_cwd = _options.get("cwd", os.getcwd())
|
|
108
|
+
_debug = _options.get("debug", False)
|
|
109
|
+
interceptor_options = {
|
|
110
|
+
"exclude": _options.get("exclude", []),
|
|
111
|
+
"api_prefix": _options.get("api_prefix", None),
|
|
112
|
+
"debug": _debug,
|
|
113
|
+
"scan_secret": _options.get("api_key"),
|
|
114
|
+
"on_scan_requested": lambda: _run_full_scan(
|
|
115
|
+
app, framework, _cwd, _client, _debug
|
|
116
|
+
),
|
|
117
|
+
}
|
|
118
|
+
if framework == "fastapi":
|
|
119
|
+
attach_fastapi_interceptor(app, _client, interceptor_options)
|
|
120
|
+
elif framework == "flask":
|
|
121
|
+
attach_flask_interceptor(app, _client, interceptor_options)
|
|
122
|
+
elif framework == "django":
|
|
123
|
+
attach_django_interceptor(_client, interceptor_options)
|
|
124
|
+
# ── NEW ───────────────────────────────────────────────────────
|
|
125
|
+
elif framework == "starlette":
|
|
126
|
+
attach_starlette_interceptor(app, _client, interceptor_options)
|
|
127
|
+
elif framework == "sanic":
|
|
128
|
+
attach_sanic_interceptor(app, _client, interceptor_options)
|
|
129
|
+
elif framework == "falcon":
|
|
130
|
+
attach_falcon_interceptor(app, _client, interceptor_options)
|
|
131
|
+
elif framework == "bottle":
|
|
132
|
+
attach_bottle_interceptor(app, _client, interceptor_options)
|
|
133
|
+
elif framework == "aiohttp":
|
|
134
|
+
attach_aiohttp_interceptor(app, _client, interceptor_options)
|
|
135
|
+
elif framework == "tornado":
|
|
136
|
+
attach_tornado_interceptor(app, _client, interceptor_options)
|
|
137
|
+
elif framework == "pyramid":
|
|
138
|
+
attach_pyramid_interceptor(app, _client, interceptor_options)
|
|
139
|
+
elif framework == "cherrypy":
|
|
140
|
+
attach_cherrypy_interceptor(app, _client, interceptor_options)
|
|
141
|
+
return
|
|
142
|
+
|
|
143
|
+
_initialized = True
|
|
144
|
+
_options = dict(options)
|
|
145
|
+
_options["api_key"] = api_key
|
|
146
|
+
_app = app
|
|
147
|
+
|
|
148
|
+
debug = options.get("debug", False)
|
|
149
|
+
|
|
150
|
+
# ── Auto-detect framework ─────────────────────────────────────────────────
|
|
151
|
+
framework = _detect_framework(app)
|
|
152
|
+
|
|
153
|
+
if not framework:
|
|
154
|
+
_initialized = False
|
|
155
|
+
return
|
|
156
|
+
|
|
157
|
+
_client = BotVersionClient({
|
|
158
|
+
"api_key": api_key,
|
|
159
|
+
"platform_url": options.get("platform_url", "https://console.botversion.com"),
|
|
160
|
+
"debug": debug,
|
|
161
|
+
"timeout": options.get("timeout", 30),
|
|
162
|
+
"flush_delay": options.get("flush_delay", 3),
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
# Store globally so hot-reload can restore state
|
|
166
|
+
builtins._botversion_client = _client
|
|
167
|
+
builtins._botversion_options = _options
|
|
168
|
+
|
|
169
|
+
cwd = options.get("cwd", os.getcwd())
|
|
170
|
+
|
|
171
|
+
interceptor_options = {
|
|
172
|
+
"exclude": options.get("exclude", []),
|
|
173
|
+
"api_prefix": options.get("api_prefix", None),
|
|
174
|
+
"debug": debug,
|
|
175
|
+
"scan_secret": api_key,
|
|
176
|
+
"on_scan_requested": lambda: _run_full_scan(
|
|
177
|
+
app, framework, cwd, _client, debug
|
|
178
|
+
),
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
# ── Attach runtime interceptor ───────────────────────────────────────────
|
|
182
|
+
if framework == "fastapi":
|
|
183
|
+
attach_fastapi_interceptor(app, _client, interceptor_options)
|
|
184
|
+
elif framework == "flask":
|
|
185
|
+
attach_flask_interceptor(app, _client, interceptor_options)
|
|
186
|
+
elif framework == "django":
|
|
187
|
+
attach_django_interceptor(_client, interceptor_options)
|
|
188
|
+
# ── NEW ───────────────────────────────────────────────────────────────────
|
|
189
|
+
elif framework == "starlette":
|
|
190
|
+
attach_starlette_interceptor(app, _client, interceptor_options)
|
|
191
|
+
elif framework == "sanic":
|
|
192
|
+
attach_sanic_interceptor(app, _client, interceptor_options)
|
|
193
|
+
elif framework == "falcon":
|
|
194
|
+
attach_falcon_interceptor(app, _client, interceptor_options)
|
|
195
|
+
elif framework == "bottle":
|
|
196
|
+
attach_bottle_interceptor(app, _client, interceptor_options)
|
|
197
|
+
elif framework == "aiohttp":
|
|
198
|
+
attach_aiohttp_interceptor(app, _client, interceptor_options)
|
|
199
|
+
elif framework == "tornado":
|
|
200
|
+
attach_tornado_interceptor(app, _client, interceptor_options)
|
|
201
|
+
elif framework == "pyramid":
|
|
202
|
+
attach_pyramid_interceptor(app, _client, interceptor_options)
|
|
203
|
+
elif framework == "cherrypy":
|
|
204
|
+
attach_cherrypy_interceptor(app, _client, interceptor_options)
|
|
205
|
+
else:
|
|
206
|
+
return
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def get_endpoints():
|
|
210
|
+
"""Get all registered endpoints for this workspace."""
|
|
211
|
+
if not _client:
|
|
212
|
+
raise RuntimeError("BotVersion SDK not initialized. Call botversion_sdk.init() first.")
|
|
213
|
+
return _client.get_endpoints()
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def register_endpoint(endpoint):
|
|
217
|
+
"""Manually register a single endpoint."""
|
|
218
|
+
if not _client:
|
|
219
|
+
raise RuntimeError("BotVersion SDK not initialized.")
|
|
220
|
+
return _client.register_endpoints([endpoint])
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
# ── Framework auto-detection ─────────────────────────────────────────────────
|
|
224
|
+
|
|
225
|
+
def _detect_framework(app):
|
|
226
|
+
if app is not None:
|
|
227
|
+
app_type = type(app).__module__ + "." + type(app).__name__
|
|
228
|
+
|
|
229
|
+
# ── Existing ──────────────────────────────────────────────────────
|
|
230
|
+
if "fastapi" in app_type.lower():
|
|
231
|
+
return "fastapi"
|
|
232
|
+
if "flask" in app_type.lower():
|
|
233
|
+
return "flask"
|
|
234
|
+
|
|
235
|
+
# ── NEW ───────────────────────────────────────────────────────────
|
|
236
|
+
if "starlette" in app_type.lower():
|
|
237
|
+
return "starlette"
|
|
238
|
+
if "sanic" in app_type.lower():
|
|
239
|
+
return "sanic"
|
|
240
|
+
if "falcon" in app_type.lower():
|
|
241
|
+
return "falcon"
|
|
242
|
+
if "bottle" in app_type.lower():
|
|
243
|
+
return "bottle"
|
|
244
|
+
if "aiohttp" in app_type.lower():
|
|
245
|
+
return "aiohttp"
|
|
246
|
+
if "tornado" in app_type.lower():
|
|
247
|
+
return "tornado"
|
|
248
|
+
if "pyramid" in app_type.lower():
|
|
249
|
+
return "pyramid"
|
|
250
|
+
if "cherrypy" in app_type.lower():
|
|
251
|
+
return "cherrypy"
|
|
252
|
+
|
|
253
|
+
# ── No app object passed — try detecting from imported modules ────────
|
|
254
|
+
if app is None:
|
|
255
|
+
if "django" in sys.modules:
|
|
256
|
+
try:
|
|
257
|
+
from django.conf import settings
|
|
258
|
+
if settings.configured:
|
|
259
|
+
return "django"
|
|
260
|
+
except Exception:
|
|
261
|
+
pass
|
|
262
|
+
|
|
263
|
+
# ── Fallback — check sys.modules ──────────────────────────────────────
|
|
264
|
+
if "fastapi" in sys.modules:
|
|
265
|
+
return "fastapi"
|
|
266
|
+
if "flask" in sys.modules:
|
|
267
|
+
return "flask"
|
|
268
|
+
if "django" in sys.modules:
|
|
269
|
+
return "django"
|
|
270
|
+
# ── NEW fallbacks ─────────────────────────────────────────────────────
|
|
271
|
+
if "starlette" in sys.modules:
|
|
272
|
+
return "starlette"
|
|
273
|
+
if "sanic" in sys.modules:
|
|
274
|
+
return "sanic"
|
|
275
|
+
if "falcon" in sys.modules:
|
|
276
|
+
return "falcon"
|
|
277
|
+
if "bottle" in sys.modules:
|
|
278
|
+
return "bottle"
|
|
279
|
+
if "aiohttp" in sys.modules:
|
|
280
|
+
return "aiohttp"
|
|
281
|
+
if "tornado" in sys.modules:
|
|
282
|
+
return "tornado"
|
|
283
|
+
if "pyramid" in sys.modules:
|
|
284
|
+
return "pyramid"
|
|
285
|
+
if "cherrypy" in sys.modules:
|
|
286
|
+
return "cherrypy"
|
|
287
|
+
|
|
288
|
+
return None
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# botversion-sdk-python/botversion-sdk/scanner-sdk/bin/__init__.py
|