squirrels 0.5.0b3__py3-none-any.whl → 0.5.0b4__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.
Potentially problematic release.
This version of squirrels might be problematic. Click here for more details.
- squirrels/__init__.py +2 -0
- squirrels/_api_routes/__init__.py +5 -0
- squirrels/_api_routes/auth.py +262 -0
- squirrels/_api_routes/base.py +154 -0
- squirrels/_api_routes/dashboards.py +142 -0
- squirrels/_api_routes/data_management.py +103 -0
- squirrels/_api_routes/datasets.py +242 -0
- squirrels/_api_routes/oauth2.py +300 -0
- squirrels/_api_routes/project.py +214 -0
- squirrels/_api_server.py +142 -745
- squirrels/_arguments/__init__.py +0 -0
- squirrels/_arguments/{_init_time_args.py → init_time_args.py} +5 -0
- squirrels/_arguments/{_run_time_args.py → run_time_args.py} +1 -1
- squirrels/_auth.py +645 -92
- squirrels/_connection_set.py +1 -1
- squirrels/_constants.py +6 -0
- squirrels/{_dashboards_io.py → _dashboards.py} +87 -6
- squirrels/_exceptions.py +9 -37
- squirrels/_model_builder.py +1 -1
- squirrels/_model_queries.py +1 -1
- squirrels/_models.py +13 -12
- squirrels/_package_data/base_project/.env +1 -0
- squirrels/_package_data/base_project/.env.example +1 -0
- squirrels/_package_data/base_project/pyconfigs/parameters.py +84 -76
- squirrels/_package_data/base_project/pyconfigs/user.py +30 -2
- squirrels/_package_data/templates/dataset_results.html +112 -0
- squirrels/_package_data/templates/oauth_login.html +271 -0
- squirrels/_parameter_configs.py +1 -1
- squirrels/_parameter_sets.py +31 -21
- squirrels/_parameters.py +521 -123
- squirrels/_project.py +43 -24
- squirrels/_py_module.py +3 -2
- squirrels/_schemas/__init__.py +0 -0
- squirrels/_schemas/auth_models.py +144 -0
- squirrels/_schemas/query_param_models.py +67 -0
- squirrels/{_api_response_models.py → _schemas/response_models.py} +12 -8
- squirrels/_utils.py +34 -2
- squirrels/arguments.py +2 -2
- squirrels/auth.py +1 -0
- squirrels/dashboards.py +1 -1
- squirrels/types.py +3 -3
- {squirrels-0.5.0b3.dist-info → squirrels-0.5.0b4.dist-info}/METADATA +4 -1
- {squirrels-0.5.0b3.dist-info → squirrels-0.5.0b4.dist-info}/RECORD +46 -32
- squirrels/_dashboard_types.py +0 -82
- {squirrels-0.5.0b3.dist-info → squirrels-0.5.0b4.dist-info}/WHEEL +0 -0
- {squirrels-0.5.0b3.dist-info → squirrels-0.5.0b4.dist-info}/entry_points.txt +0 -0
- {squirrels-0.5.0b3.dist-info → squirrels-0.5.0b4.dist-info}/licenses/LICENSE +0 -0
squirrels/_api_server.py
CHANGED
|
@@ -1,30 +1,75 @@
|
|
|
1
|
-
from
|
|
2
|
-
from
|
|
3
|
-
from fastapi import
|
|
4
|
-
from
|
|
5
|
-
from
|
|
6
|
-
from fastapi.middleware.cors import CORSMiddleware
|
|
7
|
-
from pydantic import create_model, BaseModel, Field
|
|
1
|
+
from fastapi import FastAPI, Request, status
|
|
2
|
+
from fastapi.responses import JSONResponse, RedirectResponse
|
|
3
|
+
from fastapi.security import HTTPBearer
|
|
4
|
+
from starlette.middleware.base import BaseHTTPMiddleware
|
|
5
|
+
from starlette.responses import Response as StarletteResponse
|
|
8
6
|
from contextlib import asynccontextmanager
|
|
9
|
-
from cachetools import TTLCache
|
|
10
7
|
from argparse import Namespace
|
|
11
8
|
from pathlib import Path
|
|
12
|
-
|
|
9
|
+
from starlette.middleware.sessions import SessionMiddleware
|
|
10
|
+
from mcp.server.fastmcp import FastMCP
|
|
11
|
+
import io, time, mimetypes, traceback, uuid, asyncio, urllib.parse, contextlib
|
|
13
12
|
|
|
14
|
-
from . import _constants as c, _utils as u
|
|
13
|
+
from . import _constants as c, _utils as u
|
|
15
14
|
from ._exceptions import InvalidInputError, ConfigurationError, FileExecutionError
|
|
16
15
|
from ._version import __version__, sq_major_version
|
|
17
|
-
from ._manifest import PermissionScope
|
|
18
|
-
from ._auth import BaseUser, AccessToken, UserField
|
|
19
|
-
from ._parameter_sets import ParameterSet
|
|
20
|
-
from ._dashboard_types import Dashboard
|
|
21
16
|
from ._project import SquirrelsProject
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
|
|
18
|
+
# Import route modules
|
|
19
|
+
from ._api_routes.auth import AuthRoutes
|
|
20
|
+
from ._api_routes.project import ProjectRoutes
|
|
21
|
+
from ._api_routes.datasets import DatasetRoutes
|
|
22
|
+
from ._api_routes.dashboards import DashboardRoutes
|
|
23
|
+
from ._api_routes.data_management import DataManagementRoutes
|
|
24
|
+
from ._api_routes.oauth2 import OAuth2Routes
|
|
24
25
|
|
|
25
26
|
mimetypes.add_type('application/javascript', '.js')
|
|
26
27
|
|
|
27
28
|
|
|
29
|
+
class SmartCORSMiddleware(BaseHTTPMiddleware):
|
|
30
|
+
"""
|
|
31
|
+
Custom CORS middleware that allows specific origins to use credentials
|
|
32
|
+
while still allowing all other origins without credentials.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def __init__(self, app, allowed_credential_origins: list[str] | None = None):
|
|
36
|
+
super().__init__(app)
|
|
37
|
+
# Origins that are allowed to send credentials (cookies, auth headers)
|
|
38
|
+
self.allowed_credential_origins = allowed_credential_origins or []
|
|
39
|
+
|
|
40
|
+
async def dispatch(self, request: Request, call_next):
|
|
41
|
+
origin = request.headers.get("origin")
|
|
42
|
+
|
|
43
|
+
# Handle preflight requests
|
|
44
|
+
if request.method == "OPTIONS":
|
|
45
|
+
response = StarletteResponse(status_code=200)
|
|
46
|
+
response.headers["Access-Control-Allow-Methods"] = "GET, POST, PUT, DELETE, OPTIONS"
|
|
47
|
+
response.headers["Access-Control-Allow-Headers"] = "Authorization, Content-Type"
|
|
48
|
+
|
|
49
|
+
else:
|
|
50
|
+
# Call the next middleware/route
|
|
51
|
+
response: StarletteResponse = await call_next(request)
|
|
52
|
+
|
|
53
|
+
# Always expose the Applied-Username header
|
|
54
|
+
response.headers["Access-Control-Expose-Headers"] = "Applied-Username"
|
|
55
|
+
|
|
56
|
+
if origin:
|
|
57
|
+
scheme = "http" if request.url.hostname in ["localhost", "127.0.0.1"] else "https"
|
|
58
|
+
request_origin = f"{scheme}://{request.url.netloc}"
|
|
59
|
+
# Check if this origin is in the whitelist or if origin matches the host origin
|
|
60
|
+
if origin == request_origin or origin in self.allowed_credential_origins:
|
|
61
|
+
response.headers["Access-Control-Allow-Origin"] = origin
|
|
62
|
+
response.headers["Access-Control-Allow-Credentials"] = "true"
|
|
63
|
+
else:
|
|
64
|
+
# Allow all other origins but without credentials / cookies
|
|
65
|
+
response.headers["Access-Control-Allow-Origin"] = "*"
|
|
66
|
+
else:
|
|
67
|
+
# No origin header (same-origin request or non-browser)
|
|
68
|
+
response.headers["Access-Control-Allow-Origin"] = "*"
|
|
69
|
+
|
|
70
|
+
return response
|
|
71
|
+
|
|
72
|
+
|
|
28
73
|
class ApiServer:
|
|
29
74
|
def __init__(self, no_cache: bool, project: SquirrelsProject) -> None:
|
|
30
75
|
"""
|
|
@@ -47,6 +92,17 @@ class ApiServer:
|
|
|
47
92
|
self.param_cfg_set = project._param_cfg_set
|
|
48
93
|
self.context_func = project._context_func
|
|
49
94
|
self.dashboards = project._dashboards
|
|
95
|
+
|
|
96
|
+
self.mcp = FastMCP(name="Squirrels", stateless_http=True)
|
|
97
|
+
|
|
98
|
+
# Initialize route modules
|
|
99
|
+
get_bearer_token = HTTPBearer(auto_error=False)
|
|
100
|
+
self.oauth2_routes = OAuth2Routes(get_bearer_token, project, no_cache)
|
|
101
|
+
self.auth_routes = AuthRoutes(get_bearer_token, project, no_cache)
|
|
102
|
+
self.project_routes = ProjectRoutes(get_bearer_token, project, no_cache)
|
|
103
|
+
self.dataset_routes = DatasetRoutes(get_bearer_token, project, no_cache)
|
|
104
|
+
self.dashboard_routes = DashboardRoutes(get_bearer_token, project, no_cache)
|
|
105
|
+
self.data_management_routes = DataManagementRoutes(get_bearer_token, project, no_cache)
|
|
50
106
|
|
|
51
107
|
|
|
52
108
|
async def _monitor_for_staging_file(self) -> None:
|
|
@@ -74,41 +130,16 @@ class ApiServer:
|
|
|
74
130
|
@asynccontextmanager
|
|
75
131
|
async def _run_background_tasks(self, app: FastAPI):
|
|
76
132
|
task = asyncio.create_task(self._monitor_for_staging_file())
|
|
77
|
-
yield
|
|
78
|
-
task.cancel()
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
def _validate_request_params(self, all_request_params: Mapping, params: Mapping) -> None:
|
|
82
|
-
invalid_params = [param for param in all_request_params if param not in params]
|
|
83
|
-
if params.get("x_verify_params", False) and invalid_params:
|
|
84
|
-
raise InvalidInputError(201, f"Invalid query parameters: {', '.join(invalid_params)}")
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
def run(self, uvicorn_args: Namespace) -> None:
|
|
88
|
-
"""
|
|
89
|
-
Runs the API server with uvicorn for CLI "squirrels run"
|
|
90
|
-
|
|
91
|
-
Arguments:
|
|
92
|
-
uvicorn_args: List of arguments to pass to uvicorn.run. Currently only supports "host" and "port"
|
|
93
|
-
"""
|
|
94
|
-
start = time.time()
|
|
95
133
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
project_metadata_path = squirrels_version_path + f"/project/{project_name}/{project_version}"
|
|
134
|
+
async with contextlib.AsyncExitStack() as stack:
|
|
135
|
+
await stack.enter_async_context(self.mcp.session_manager.run())
|
|
136
|
+
yield
|
|
100
137
|
|
|
101
|
-
|
|
138
|
+
task.cancel()
|
|
139
|
+
|
|
102
140
|
|
|
141
|
+
def _get_tags_metadata(self) -> list[dict]:
|
|
103
142
|
tags_metadata = [
|
|
104
|
-
{
|
|
105
|
-
"name": "Authentication",
|
|
106
|
-
"description": "Submit authentication credentials, and get token for authentication",
|
|
107
|
-
},
|
|
108
|
-
{
|
|
109
|
-
"name": "User Management",
|
|
110
|
-
"description": "Manage users and their attributes",
|
|
111
|
-
},
|
|
112
143
|
{
|
|
113
144
|
"name": "Project Metadata",
|
|
114
145
|
"description": "Get information on project such as name, version, and other API endpoints",
|
|
@@ -131,6 +162,41 @@ class ApiServer:
|
|
|
131
162
|
"description": f"Get parameters or results for dashboard '{dashboard_name}'",
|
|
132
163
|
})
|
|
133
164
|
|
|
165
|
+
tags_metadata.extend([
|
|
166
|
+
{
|
|
167
|
+
"name": "Authentication",
|
|
168
|
+
"description": "Submit authentication credentials and authorize with a session cookie",
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
"name": "User Management",
|
|
172
|
+
"description": "Manage users and their attributes",
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
"name": "OAuth2",
|
|
176
|
+
"description": "Authorize and get token using the OAuth2 protocol",
|
|
177
|
+
},
|
|
178
|
+
])
|
|
179
|
+
return tags_metadata
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def run(self, uvicorn_args: Namespace) -> None:
|
|
183
|
+
"""
|
|
184
|
+
Runs the API server with uvicorn for CLI "squirrels run"
|
|
185
|
+
|
|
186
|
+
Arguments:
|
|
187
|
+
uvicorn_args: List of arguments to pass to uvicorn.run. Currently only supports "host" and "port"
|
|
188
|
+
"""
|
|
189
|
+
start = time.time()
|
|
190
|
+
|
|
191
|
+
squirrels_version_path = f'/api/squirrels-v{sq_major_version}'
|
|
192
|
+
project_name = u.normalize_name_for_api(self.manifest_cfg.project_variables.name)
|
|
193
|
+
project_version = f"v{self.manifest_cfg.project_variables.major_version}"
|
|
194
|
+
project_metadata_path = squirrels_version_path + f"/project/{project_name}/{project_version}"
|
|
195
|
+
|
|
196
|
+
param_fields = self.param_cfg_set.get_all_api_field_info()
|
|
197
|
+
|
|
198
|
+
tags_metadata = self._get_tags_metadata()
|
|
199
|
+
|
|
134
200
|
app = FastAPI(
|
|
135
201
|
title=f"Squirrels APIs for '{self.manifest_cfg.project_variables.label}'", openapi_tags=tags_metadata,
|
|
136
202
|
description="For specifying parameter selections to dataset APIs, you can choose between using query parameters with the GET method or using request body with the POST method",
|
|
@@ -140,6 +206,8 @@ class ApiServer:
|
|
|
140
206
|
redoc_url=project_metadata_path+"/redoc"
|
|
141
207
|
)
|
|
142
208
|
|
|
209
|
+
app.add_middleware(SessionMiddleware, secret_key=self.env_vars.get(c.SQRL_SECRET_KEY, ""), max_age=None, same_site="none", https_only=True)
|
|
210
|
+
|
|
143
211
|
async def _log_request_run(request: Request) -> None:
|
|
144
212
|
headers = dict(request.scope["headers"])
|
|
145
213
|
request_id = uuid.uuid4().hex
|
|
@@ -157,9 +225,6 @@ class ApiServer:
|
|
|
157
225
|
data = {"request_method": request.method, "request_path": path, "request_params": params, "request_headers": headers_dict, "request_body": body}
|
|
158
226
|
info = {"request_id": request_id}
|
|
159
227
|
self.logger.info(f'Running request: {request.method} {path_with_params}', extra={"data": data, "info": info})
|
|
160
|
-
|
|
161
|
-
def _get_request_id(request: Request) -> str:
|
|
162
|
-
return request.headers.get("x-request-id", "")
|
|
163
228
|
|
|
164
229
|
@app.middleware("http")
|
|
165
230
|
async def catch_exceptions_middleware(request: Request, call_next):
|
|
@@ -168,22 +233,10 @@ class ApiServer:
|
|
|
168
233
|
await _log_request_run(request)
|
|
169
234
|
return await call_next(request)
|
|
170
235
|
except InvalidInputError as exc:
|
|
171
|
-
traceback.print_exc(file=buffer)
|
|
172
236
|
message = str(exc)
|
|
173
|
-
|
|
174
|
-
status_code = status.HTTP_401_UNAUTHORIZED
|
|
175
|
-
elif exc.error_code < 40:
|
|
176
|
-
status_code = status.HTTP_403_FORBIDDEN
|
|
177
|
-
elif exc.error_code < 60:
|
|
178
|
-
status_code = status.HTTP_404_NOT_FOUND
|
|
179
|
-
elif exc.error_code < 70:
|
|
180
|
-
if exc.error_code == 61:
|
|
181
|
-
message = "The dataset depends on static data models that cannot be found. You may need to build the virtual data environment first."
|
|
182
|
-
status_code = status.HTTP_409_CONFLICT
|
|
183
|
-
else:
|
|
184
|
-
status_code = status.HTTP_400_BAD_REQUEST
|
|
237
|
+
self.logger.error(message)
|
|
185
238
|
response = JSONResponse(
|
|
186
|
-
status_code=status_code, content={"
|
|
239
|
+
status_code=exc.status_code, content={"error": exc.error, "error_description": exc.error_description}
|
|
187
240
|
)
|
|
188
241
|
except FileExecutionError as exc:
|
|
189
242
|
traceback.print_exception(exc.error, file=buffer)
|
|
@@ -203,694 +256,37 @@ class ApiServer:
|
|
|
203
256
|
)
|
|
204
257
|
|
|
205
258
|
err_msg = buffer.getvalue()
|
|
206
|
-
|
|
207
|
-
|
|
259
|
+
if err_msg:
|
|
260
|
+
self.logger.error(err_msg)
|
|
261
|
+
print(err_msg)
|
|
208
262
|
return response
|
|
209
263
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
)
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
val = val[0]
|
|
227
|
-
else:
|
|
228
|
-
val = tuple(val)
|
|
229
|
-
selections.append((u.normalize_name(key), val))
|
|
230
|
-
return tuple(selections)
|
|
231
|
-
|
|
232
|
-
async def do_cachable_action(cache: TTLCache, action: Callable[..., Coroutine[Any, Any, T]], *args) -> T:
|
|
233
|
-
cache_key = tuple(args)
|
|
234
|
-
result = cache.get(cache_key)
|
|
235
|
-
if result is None:
|
|
236
|
-
result = await action(*args)
|
|
237
|
-
cache[cache_key] = result
|
|
238
|
-
return result
|
|
239
|
-
|
|
240
|
-
def _get_query_models_helper(widget_parameters: list[str] | None, predefined_params: list[APIParamFieldInfo]):
|
|
241
|
-
if widget_parameters is None:
|
|
242
|
-
widget_parameters = list(param_fields.keys())
|
|
243
|
-
|
|
244
|
-
QueryModelForGetRaw = make_dataclass("QueryParams", [
|
|
245
|
-
param_fields[param].as_query_info() for param in widget_parameters
|
|
246
|
-
] + [param.as_query_info() for param in predefined_params])
|
|
247
|
-
QueryModelForGet = Annotated[QueryModelForGetRaw, Depends()]
|
|
248
|
-
|
|
249
|
-
field_definitions = {param: param_fields[param].as_body_info() for param in widget_parameters}
|
|
250
|
-
for param in predefined_params:
|
|
251
|
-
field_definitions[param.name] = param.as_body_info()
|
|
252
|
-
QueryModelForPost = create_model("RequestBodyParams", **field_definitions) # type: ignore
|
|
253
|
-
return QueryModelForGet, QueryModelForPost
|
|
254
|
-
|
|
255
|
-
def get_query_models_for_parameters(widget_parameters: list[str] | None):
|
|
256
|
-
predefined_params = [
|
|
257
|
-
APIParamFieldInfo("x_verify_params", bool, default=False, description="If true, the query parameters are verified to be valid for the dataset"),
|
|
258
|
-
APIParamFieldInfo("x_parent_param", str, description="The parameter name used for parameter updates. If not provided, then all parameters are retrieved"),
|
|
259
|
-
]
|
|
260
|
-
return _get_query_models_helper(widget_parameters, predefined_params)
|
|
261
|
-
|
|
262
|
-
def get_query_models_for_dataset(widget_parameters: list[str] | None):
|
|
263
|
-
predefined_params = [
|
|
264
|
-
APIParamFieldInfo("x_verify_params", bool, default=False, description="If true, the query parameters are verified to be valid for the dataset"),
|
|
265
|
-
APIParamFieldInfo("x_orientation", str, default="records", description="The orientation of the data to return, one of: 'records', 'rows', or 'columns'"),
|
|
266
|
-
APIParamFieldInfo("x_select", list[str], examples=[[]], description="The columns to select from the dataset. All are returned if not specified"),
|
|
267
|
-
APIParamFieldInfo("x_offset", int, default=0, description="The number of rows to skip before returning data (applied after data caching)"),
|
|
268
|
-
APIParamFieldInfo("x_limit", int, default=1000, description="The maximum number of rows to return (applied after data caching and offset)"),
|
|
269
|
-
]
|
|
270
|
-
return _get_query_models_helper(widget_parameters, predefined_params)
|
|
271
|
-
|
|
272
|
-
def get_query_models_for_dashboard(widget_parameters: list[str] | None):
|
|
273
|
-
predefined_params = [
|
|
274
|
-
APIParamFieldInfo("x_verify_params", bool, default=False, description="If true, the query parameters are verified to be valid for the dashboard"),
|
|
275
|
-
]
|
|
276
|
-
return _get_query_models_helper(widget_parameters, predefined_params)
|
|
277
|
-
|
|
278
|
-
def get_query_models_for_querying_models():
|
|
279
|
-
predefined_params = [
|
|
280
|
-
APIParamFieldInfo("x_verify_params", bool, default=False, description="If true, the query parameters are verified to be valid"),
|
|
281
|
-
APIParamFieldInfo("x_orientation", str, default="records", description="The orientation of the data to return, one of: 'records', 'rows', or 'columns'"),
|
|
282
|
-
APIParamFieldInfo("x_offset", int, default=0, description="The number of rows to skip before returning data (applied after data caching)"),
|
|
283
|
-
APIParamFieldInfo("x_limit", int, default=1000, description="The maximum number of rows to return (applied after data caching and offset)"),
|
|
284
|
-
APIParamFieldInfo("x_sql_query", str, description="The SQL query to execute on the data models"),
|
|
285
|
-
]
|
|
286
|
-
return _get_query_models_helper(None, predefined_params)
|
|
287
|
-
|
|
288
|
-
def _get_section_from_request_path(request: Request, section: int) -> str:
|
|
289
|
-
url_path: str = request.scope['route'].path
|
|
290
|
-
return url_path.split('/')[section]
|
|
291
|
-
|
|
292
|
-
def get_dataset_name(request: Request, section: int) -> str:
|
|
293
|
-
dataset_raw = _get_section_from_request_path(request, section)
|
|
294
|
-
return u.normalize_name(dataset_raw)
|
|
295
|
-
|
|
296
|
-
def get_dashboard_name(request: Request, section: int) -> str:
|
|
297
|
-
dashboard_raw = _get_section_from_request_path(request, section)
|
|
298
|
-
return u.normalize_name(dashboard_raw)
|
|
299
|
-
|
|
300
|
-
expiry_mins = self.env_vars.get(c.SQRL_AUTH_TOKEN_EXPIRE_MINUTES, 30)
|
|
301
|
-
try:
|
|
302
|
-
expiry_mins = int(expiry_mins)
|
|
303
|
-
except ValueError:
|
|
304
|
-
raise ConfigurationError(f"Value for environment variable {c.SQRL_AUTH_TOKEN_EXPIRE_MINUTES} is not an integer, got: {expiry_mins}")
|
|
305
|
-
|
|
306
|
-
# Project Metadata API
|
|
307
|
-
|
|
308
|
-
@app.get(project_metadata_path, tags=["Project Metadata"], response_class=JSONResponse)
|
|
309
|
-
async def get_project_metadata(request: Request) -> arm.ProjectModel:
|
|
310
|
-
return arm.ProjectModel(
|
|
311
|
-
name=project_name,
|
|
312
|
-
version=project_version,
|
|
313
|
-
label=self.manifest_cfg.project_variables.label,
|
|
314
|
-
description=self.manifest_cfg.project_variables.description,
|
|
315
|
-
squirrels_version=__version__
|
|
316
|
-
)
|
|
317
|
-
|
|
318
|
-
# Authentication
|
|
319
|
-
login_path = project_metadata_path + '/login'
|
|
320
|
-
|
|
321
|
-
oauth2_scheme = OAuth2PasswordBearer(tokenUrl=login_path, auto_error=False)
|
|
322
|
-
|
|
323
|
-
async def get_current_user(response: Response, token: str = Depends(oauth2_scheme)) -> BaseUser | None:
|
|
324
|
-
user = self.authenticator.get_user_from_token(token)
|
|
325
|
-
username = "" if user is None else user.username
|
|
326
|
-
response.headers["Applied-Username"] = username
|
|
327
|
-
return user
|
|
328
|
-
|
|
329
|
-
## Login API
|
|
330
|
-
@app.post(login_path, tags=["Authentication"])
|
|
331
|
-
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()) -> arm.LoginReponse:
|
|
332
|
-
user = self.authenticator.get_user(form_data.username, form_data.password)
|
|
333
|
-
access_token, expiry = self.authenticator.create_access_token(user, expiry_minutes=expiry_mins)
|
|
334
|
-
return arm.LoginReponse(access_token=access_token, token_type="bearer", username=user.username, is_admin=user.is_admin, expiry_time=expiry)
|
|
335
|
-
|
|
336
|
-
## Change Password API
|
|
337
|
-
change_password_path = project_metadata_path + '/change-password'
|
|
338
|
-
|
|
339
|
-
class ChangePasswordRequest(BaseModel):
|
|
340
|
-
old_password: str
|
|
341
|
-
new_password: str
|
|
342
|
-
|
|
343
|
-
@app.put(change_password_path, description="Change the password for the current user", tags=["Authentication"])
|
|
344
|
-
async def change_password(request: ChangePasswordRequest, user: BaseUser | None = Depends(get_current_user)) -> None:
|
|
345
|
-
if user is None:
|
|
346
|
-
raise InvalidInputError(1, "Invalid authorization token")
|
|
347
|
-
self.authenticator.change_password(user.username, request.old_password, request.new_password)
|
|
348
|
-
|
|
349
|
-
## Token API
|
|
350
|
-
tokens_path = project_metadata_path + '/tokens'
|
|
351
|
-
|
|
352
|
-
class TokenRequestBody(BaseModel):
|
|
353
|
-
title: str | None = Field(default=None, description=f"The title of the token. If not provided, a temporary token is created (expiring in {expiry_mins} minutes) and cannot be revoked")
|
|
354
|
-
expiry_minutes: int | None = Field(
|
|
355
|
-
default=None,
|
|
356
|
-
description=f"The number of minutes the token is valid for (or indefinitely if not provided). Ignored and set to {expiry_mins} minutes if title is not provided."
|
|
357
|
-
)
|
|
358
|
-
|
|
359
|
-
@app.post(tokens_path, description="Create a new token for the user", tags=["Authentication"])
|
|
360
|
-
async def create_token(body: TokenRequestBody, user: BaseUser | None = Depends(get_current_user)) -> arm.LoginReponse:
|
|
361
|
-
if user is None:
|
|
362
|
-
raise InvalidInputError(1, "Invalid authorization token")
|
|
363
|
-
|
|
364
|
-
if body.title is None:
|
|
365
|
-
expiry_minutes = expiry_mins
|
|
366
|
-
else:
|
|
367
|
-
expiry_minutes = body.expiry_minutes
|
|
368
|
-
|
|
369
|
-
access_token, expiry = self.authenticator.create_access_token(user, expiry_minutes=expiry_minutes, title=body.title)
|
|
370
|
-
return arm.LoginReponse(access_token=access_token, token_type="bearer", username=user.username, is_admin=user.is_admin, expiry_time=expiry)
|
|
371
|
-
|
|
372
|
-
## Get All Tokens API
|
|
373
|
-
@app.get(tokens_path, description="Get all tokens with title for the current user", tags=["Authentication"])
|
|
374
|
-
async def get_all_tokens(user: BaseUser | None = Depends(get_current_user)) -> list[AccessToken]:
|
|
375
|
-
if user is None:
|
|
376
|
-
raise InvalidInputError(1, "Invalid authorization token")
|
|
377
|
-
return self.authenticator.get_all_tokens(user.username)
|
|
378
|
-
|
|
379
|
-
## Revoke Token API
|
|
380
|
-
revoke_token_path = project_metadata_path + '/tokens/{token_id}'
|
|
381
|
-
|
|
382
|
-
@app.delete(revoke_token_path, description="Revoke a token", tags=["Authentication"])
|
|
383
|
-
async def revoke_token(token_id: str, user: BaseUser | None = Depends(get_current_user)) -> None:
|
|
384
|
-
if user is None:
|
|
385
|
-
raise InvalidInputError(1, "Invalid authorization token")
|
|
386
|
-
self.authenticator.revoke_token(user.username, token_id)
|
|
387
|
-
|
|
388
|
-
## Get Authenticated User Fields From Token API
|
|
389
|
-
get_me_path = project_metadata_path + '/me'
|
|
390
|
-
|
|
391
|
-
fields_without_username = {
|
|
392
|
-
k: (v.annotation, v.default)
|
|
393
|
-
for k, v in self.authenticator.User.model_fields.items()
|
|
394
|
-
if k != "username"
|
|
395
|
-
}
|
|
396
|
-
UserModel = create_model("UserModel", __base__=BaseModel, **fields_without_username) # type: ignore
|
|
397
|
-
|
|
398
|
-
class UserWithoutUsername(UserModel):
|
|
399
|
-
pass
|
|
400
|
-
|
|
401
|
-
class UserWithUsername(UserModel):
|
|
402
|
-
username: str
|
|
403
|
-
|
|
404
|
-
class AddUserRequestBody(UserWithUsername):
|
|
405
|
-
password: str
|
|
406
|
-
|
|
407
|
-
@app.get(get_me_path, description="Get the authenticated user's fields", tags=["Authentication"])
|
|
408
|
-
async def get_me(user: BaseUser | None = Depends(get_current_user)) -> UserWithUsername:
|
|
409
|
-
if user is None:
|
|
410
|
-
raise InvalidInputError(1, "Invalid authorization token")
|
|
411
|
-
return UserWithUsername(**user.model_dump(mode='json'))
|
|
412
|
-
|
|
413
|
-
# User Management
|
|
414
|
-
|
|
415
|
-
## User Fields API
|
|
416
|
-
user_fields_path = project_metadata_path + '/user-fields'
|
|
417
|
-
|
|
418
|
-
@app.get(user_fields_path, description="Get details of the user fields", tags=["User Management"])
|
|
419
|
-
async def get_user_fields() -> list[UserField]:
|
|
420
|
-
return self.authenticator.user_fields
|
|
421
|
-
|
|
422
|
-
## Add User API
|
|
423
|
-
add_user_path = project_metadata_path + '/users'
|
|
424
|
-
|
|
425
|
-
@app.post(add_user_path, description="Add a new user by providing details for username, password, and user fields", tags=["User Management"])
|
|
426
|
-
async def add_user(
|
|
427
|
-
new_user: AddUserRequestBody, user: BaseUser | None = Depends(get_current_user)
|
|
428
|
-
) -> None:
|
|
429
|
-
if user is None or not user.is_admin:
|
|
430
|
-
raise InvalidInputError(20, "Authorized user is forbidden to add new users")
|
|
431
|
-
self.authenticator.add_user(new_user.username, new_user.model_dump(mode='json', exclude={"username"}))
|
|
432
|
-
|
|
433
|
-
## Update User API
|
|
434
|
-
update_user_path = project_metadata_path + '/users/{username}'
|
|
435
|
-
|
|
436
|
-
@app.put(update_user_path, description="Update the user of the given username given the new user details", tags=["User Management"])
|
|
437
|
-
async def update_user(
|
|
438
|
-
username: str, updated_user: UserWithoutUsername, user: BaseUser | None = Depends(get_current_user)
|
|
439
|
-
) -> None:
|
|
440
|
-
if user is None or not user.is_admin:
|
|
441
|
-
raise InvalidInputError(20, "Authorized user is forbidden to update users")
|
|
442
|
-
self.authenticator.add_user(username, updated_user.model_dump(mode='json'), update_user=True)
|
|
443
|
-
|
|
444
|
-
## List Users API
|
|
445
|
-
list_users_path = project_metadata_path + '/users'
|
|
446
|
-
|
|
447
|
-
@app.get(list_users_path, tags=["User Management"])
|
|
448
|
-
async def list_all_users() -> list[UserWithUsername]:
|
|
449
|
-
return self.authenticator.get_all_users()
|
|
450
|
-
|
|
451
|
-
## Delete User API
|
|
452
|
-
delete_user_path = project_metadata_path + '/users/{username}'
|
|
453
|
-
|
|
454
|
-
@app.delete(delete_user_path, tags=["User Management"])
|
|
455
|
-
async def delete_user(username: str, user: BaseUser | None = Depends(get_current_user)) -> None:
|
|
456
|
-
if user is None or not user.is_admin:
|
|
457
|
-
raise InvalidInputError(21, "Authorized user is forbidden to delete users")
|
|
458
|
-
if username == user.username:
|
|
459
|
-
raise InvalidInputError(22, "Cannot delete your own user")
|
|
460
|
-
self.authenticator.delete_user(username)
|
|
461
|
-
|
|
462
|
-
# Data Catalog API
|
|
463
|
-
data_catalog_path = project_metadata_path + '/data-catalog'
|
|
464
|
-
|
|
465
|
-
dataset_results_path = project_metadata_path + '/dataset/{dataset}'
|
|
466
|
-
dataset_parameters_path = dataset_results_path + '/parameters'
|
|
467
|
-
|
|
468
|
-
dashboard_results_path = project_metadata_path + '/dashboard/{dashboard}'
|
|
469
|
-
dashboard_parameters_path = dashboard_results_path + '/parameters'
|
|
470
|
-
|
|
471
|
-
async def get_data_catalog0(user: BaseUser | None) -> arm.CatalogModel:
|
|
472
|
-
parameters = self.param_cfg_set.apply_selections(None, {}, user)
|
|
473
|
-
parameters_model = parameters.to_api_response_model0()
|
|
474
|
-
full_parameters_list = [p.name for p in parameters_model.parameters]
|
|
475
|
-
|
|
476
|
-
dataset_items: list[arm.DatasetItemModel] = []
|
|
477
|
-
for name, config in self.manifest_cfg.datasets.items():
|
|
478
|
-
if self.authenticator.can_user_access_scope(user, config.scope):
|
|
479
|
-
name_normalized = u.normalize_name_for_api(name)
|
|
480
|
-
metadata = self.project.dataset_metadata(name).to_json()
|
|
481
|
-
parameters = config.parameters if config.parameters is not None else full_parameters_list
|
|
482
|
-
dataset_items.append(arm.DatasetItemModel(
|
|
483
|
-
name=name_normalized, label=config.label,
|
|
484
|
-
description=config.description,
|
|
485
|
-
schema=metadata["schema"], # type: ignore
|
|
486
|
-
parameters=parameters,
|
|
487
|
-
parameters_path=dataset_parameters_path.format(dataset=name_normalized),
|
|
488
|
-
result_path=dataset_results_path.format(dataset=name_normalized)
|
|
489
|
-
))
|
|
490
|
-
|
|
491
|
-
dashboard_items: list[arm.DashboardItemModel] = []
|
|
492
|
-
for name, dashboard in self.dashboards.items():
|
|
493
|
-
config = dashboard.config
|
|
494
|
-
if self.authenticator.can_user_access_scope(user, config.scope):
|
|
495
|
-
name_normalized = u.normalize_name_for_api(name)
|
|
496
|
-
|
|
497
|
-
try:
|
|
498
|
-
dashboard_format = self.dashboards[name].get_dashboard_format()
|
|
499
|
-
except KeyError:
|
|
500
|
-
raise ConfigurationError(f"No dashboard file found for: {name}")
|
|
501
|
-
|
|
502
|
-
parameters = config.parameters if config.parameters is not None else full_parameters_list
|
|
503
|
-
dashboard_items.append(arm.DashboardItemModel(
|
|
504
|
-
name=name, label=config.label,
|
|
505
|
-
description=config.description,
|
|
506
|
-
result_format=dashboard_format,
|
|
507
|
-
parameters=parameters,
|
|
508
|
-
parameters_path=dashboard_parameters_path.format(dashboard=name_normalized),
|
|
509
|
-
result_path=dashboard_results_path.format(dashboard=name_normalized)
|
|
510
|
-
))
|
|
511
|
-
|
|
512
|
-
if user and user.is_admin:
|
|
513
|
-
compiled_dag = await self.project._get_compiled_dag(user=user)
|
|
514
|
-
connections_items = self.project._get_all_connections()
|
|
515
|
-
data_models = self.project._get_all_data_models(compiled_dag)
|
|
516
|
-
lineage_items = self.project._get_all_data_lineage(compiled_dag)
|
|
517
|
-
else:
|
|
518
|
-
connections_items = []
|
|
519
|
-
data_models = []
|
|
520
|
-
lineage_items = []
|
|
521
|
-
|
|
522
|
-
return arm.CatalogModel(
|
|
523
|
-
parameters=parameters_model.parameters,
|
|
524
|
-
datasets=dataset_items,
|
|
525
|
-
dashboards=dashboard_items,
|
|
526
|
-
connections=connections_items,
|
|
527
|
-
models=data_models,
|
|
528
|
-
lineage=lineage_items,
|
|
529
|
-
)
|
|
530
|
-
|
|
531
|
-
@app.get(data_catalog_path, tags=["Project Metadata"], summary="Get catalog of datasets and dashboards available for user")
|
|
532
|
-
async def get_data_catalog(request: Request, user: BaseUser | None = Depends(get_current_user)) -> arm.CatalogModel:
|
|
533
|
-
"""
|
|
534
|
-
Get catalog of datasets and dashboards available for the authenticated user.
|
|
535
|
-
|
|
536
|
-
For admin users, this endpoint will also return detailed information about all models and their lineage in the project.
|
|
537
|
-
"""
|
|
538
|
-
return await get_data_catalog0(user)
|
|
539
|
-
|
|
540
|
-
# Parameters API Helpers
|
|
541
|
-
parameters_description = "Selections of one parameter may cascade the available options in another parameter. " \
|
|
542
|
-
"For example, if the dataset has parameters for 'country' and 'city', available options for 'city' would " \
|
|
543
|
-
"depend on the selected option 'country'. If a parameter has 'trigger_refresh' as true, provide the parameter " \
|
|
544
|
-
"selection to this endpoint whenever it changes to refresh the parameter options of children parameters."
|
|
545
|
-
|
|
546
|
-
async def get_parameters_helper(
|
|
547
|
-
parameters_tuple: tuple[str, ...] | None, entity_type: str, entity_name: str, entity_scope: PermissionScope,
|
|
548
|
-
user: BaseUser | None, selections: tuple[tuple[str, Any], ...]
|
|
549
|
-
) -> ParameterSet:
|
|
550
|
-
selections_dict = dict(selections)
|
|
551
|
-
if "x_parent_param" not in selections_dict:
|
|
552
|
-
if len(selections_dict) > 1:
|
|
553
|
-
raise InvalidInputError(202, f"The parameters endpoint takes at most 1 widget parameter selection (unless x_parent_param is provided). Got {selections_dict}")
|
|
554
|
-
elif len(selections_dict) == 1:
|
|
555
|
-
parent_param = next(iter(selections_dict))
|
|
556
|
-
selections_dict["x_parent_param"] = parent_param
|
|
557
|
-
|
|
558
|
-
parent_param = selections_dict.get("x_parent_param")
|
|
559
|
-
if parent_param is not None and parent_param not in selections_dict:
|
|
560
|
-
# this condition is possible for multi-select parameters with empty selection
|
|
561
|
-
selections_dict[parent_param] = list()
|
|
562
|
-
|
|
563
|
-
if not self.authenticator.can_user_access_scope(user, entity_scope):
|
|
564
|
-
raise self.project._permission_error(user, entity_type, entity_name, entity_scope.name)
|
|
565
|
-
|
|
566
|
-
param_set = self.param_cfg_set.apply_selections(parameters_tuple, selections_dict, user, parent_param=parent_param)
|
|
567
|
-
return param_set
|
|
568
|
-
|
|
569
|
-
parameters_cache_size = int(self.env_vars.get(c.SQRL_PARAMETERS_CACHE_SIZE, 1024))
|
|
570
|
-
parameters_cache_ttl = int(self.env_vars.get(c.SQRL_PARAMETERS_CACHE_TTL_MINUTES, 60))
|
|
571
|
-
params_cache = TTLCache(maxsize=parameters_cache_size, ttl=parameters_cache_ttl*60)
|
|
572
|
-
|
|
573
|
-
async def get_parameters_cachable(
|
|
574
|
-
parameters_tuple: tuple[str, ...] | None, entity_type: str, entity_name: str, entity_scope: PermissionScope,
|
|
575
|
-
user: BaseUser | None, selections: tuple[tuple[str, Any], ...]
|
|
576
|
-
) -> ParameterSet:
|
|
577
|
-
return await do_cachable_action(params_cache, get_parameters_helper, parameters_tuple, entity_type, entity_name, entity_scope, user, selections)
|
|
578
|
-
|
|
579
|
-
async def get_parameters_definition(
|
|
580
|
-
parameters_list: list[str] | None, entity_type: str, entity_name: str, entity_scope: PermissionScope,
|
|
581
|
-
user: BaseUser | None, all_request_params: dict, params: Mapping
|
|
582
|
-
) -> arm.ParametersModel:
|
|
583
|
-
self._validate_request_params(all_request_params, params)
|
|
584
|
-
|
|
585
|
-
get_parameters_function = get_parameters_helper if self.no_cache else get_parameters_cachable
|
|
586
|
-
selections = get_selections_as_immutable(params, uncached_keys={"x_verify_params"})
|
|
587
|
-
parameters_tuple = tuple(parameters_list) if parameters_list is not None else None
|
|
588
|
-
result = await get_parameters_function(parameters_tuple, entity_type, entity_name, entity_scope, user, selections)
|
|
589
|
-
return result.to_api_response_model0()
|
|
590
|
-
|
|
591
|
-
def validate_parameters_list(parameters: list[str] | None, entity_type: str) -> None:
|
|
592
|
-
if parameters is None:
|
|
593
|
-
return
|
|
594
|
-
for param in parameters:
|
|
595
|
-
if param not in param_fields:
|
|
596
|
-
all_params = list(param_fields.keys())
|
|
597
|
-
raise ConfigurationError(
|
|
598
|
-
f"{entity_type} '{dataset_name}' use parameter '{param}' which doesn't exist. Available parameters are:"
|
|
599
|
-
f"\n {all_params}"
|
|
600
|
-
)
|
|
601
|
-
|
|
602
|
-
# Project-Level Parameters API
|
|
603
|
-
project_level_parameters_path = project_metadata_path + '/parameters'
|
|
604
|
-
|
|
605
|
-
QueryModelForGetProjectParams, QueryModelForPostProjectParams = get_query_models_for_parameters(None)
|
|
606
|
-
|
|
607
|
-
@app.get(project_level_parameters_path, tags=["Project Metadata"], description=parameters_description)
|
|
608
|
-
async def get_project_parameters(
|
|
609
|
-
request: Request, params: QueryModelForGetProjectParams, user: BaseUser | None = Depends(get_current_user) # type: ignore
|
|
610
|
-
) -> arm.ParametersModel:
|
|
611
|
-
start = time.time()
|
|
612
|
-
result = await get_parameters_definition(
|
|
613
|
-
None, "project", "", PermissionScope.PUBLIC, user, dict(request.query_params), asdict(params)
|
|
614
|
-
)
|
|
615
|
-
self.logger.log_activity_time("GET REQUEST for PROJECT PARAMETERS", start, request_id=_get_request_id(request))
|
|
616
|
-
return result
|
|
617
|
-
|
|
618
|
-
@app.post(project_level_parameters_path, tags=["Project Metadata"], description=parameters_description)
|
|
619
|
-
async def get_project_parameters_with_post(
|
|
620
|
-
request: Request, params: QueryModelForPostProjectParams, user: BaseUser | None = Depends(get_current_user) # type: ignore
|
|
621
|
-
) -> arm.ParametersModel:
|
|
622
|
-
start = time.time()
|
|
623
|
-
params_model: BaseModel = params
|
|
624
|
-
payload: dict = await request.json()
|
|
625
|
-
result = await get_parameters_definition(
|
|
626
|
-
None, "project", "", PermissionScope.PUBLIC, user, payload, params_model.model_dump()
|
|
627
|
-
)
|
|
628
|
-
self.logger.log_activity_time("POST REQUEST for PROJECT PARAMETERS", start, request_id=_get_request_id(request))
|
|
629
|
-
return result
|
|
630
|
-
|
|
631
|
-
# Dataset Results API Helpers
|
|
632
|
-
async def get_dataset_results_helper(
|
|
633
|
-
dataset: str, user: BaseUser | None, selections: tuple[tuple[str, Any], ...]
|
|
634
|
-
) -> DatasetResult:
|
|
635
|
-
return await self.project.dataset(dataset, selections=dict(selections), user=user)
|
|
636
|
-
|
|
637
|
-
dataset_results_cache_size = int(self.env_vars.get(c.SQRL_DATASETS_CACHE_SIZE, 128))
|
|
638
|
-
dataset_results_cache_ttl = int(self.env_vars.get(c.SQRL_DATASETS_CACHE_TTL_MINUTES, 60))
|
|
639
|
-
dataset_results_cache = TTLCache(maxsize=dataset_results_cache_size, ttl=dataset_results_cache_ttl*60)
|
|
640
|
-
|
|
641
|
-
async def get_dataset_results_cachable(
|
|
642
|
-
dataset: str, user: BaseUser | None, selections: tuple[tuple[str, Any], ...]
|
|
643
|
-
) -> DatasetResult:
|
|
644
|
-
return await do_cachable_action(dataset_results_cache, get_dataset_results_helper, dataset, user, selections)
|
|
645
|
-
|
|
646
|
-
async def get_dataset_results_definition(
|
|
647
|
-
dataset_name: str, user: BaseUser | None, all_request_params: dict, params: Mapping
|
|
648
|
-
) -> arm.DatasetResultModel:
|
|
649
|
-
self._validate_request_params(all_request_params, params)
|
|
650
|
-
|
|
651
|
-
get_dataset_function = get_dataset_results_helper if self.no_cache else get_dataset_results_cachable
|
|
652
|
-
uncached_keys = {"x_verify_params", "x_orientation", "x_select", "x_limit", "x_offset"}
|
|
653
|
-
selections = get_selections_as_immutable(params, uncached_keys)
|
|
654
|
-
result = await get_dataset_function(dataset_name, user, selections)
|
|
655
|
-
|
|
656
|
-
orientation = params.get("x_orientation", "records")
|
|
657
|
-
raw_select = params.get("x_select")
|
|
658
|
-
select = tuple(raw_select) if raw_select is not None else tuple()
|
|
659
|
-
limit = params.get("x_limit", 1000)
|
|
660
|
-
offset = params.get("x_offset", 0)
|
|
661
|
-
return arm.DatasetResultModel(**result.to_json(orientation, select, limit, offset))
|
|
662
|
-
|
|
663
|
-
# Dashboard Results API Helpers
|
|
664
|
-
async def get_dashboard_results_helper(
|
|
665
|
-
dashboard: str, user: BaseUser | None, selections: tuple[tuple[str, Any], ...]
|
|
666
|
-
) -> Dashboard:
|
|
667
|
-
return await self.project.dashboard(dashboard, selections=dict(selections), user=user)
|
|
668
|
-
|
|
669
|
-
dashboard_results_cache_size = int(self.env_vars.get(c.SQRL_DASHBOARDS_CACHE_SIZE, 128))
|
|
670
|
-
dashboard_results_cache_ttl = int(self.env_vars.get(c.SQRL_DASHBOARDS_CACHE_TTL_MINUTES, 60))
|
|
671
|
-
dashboard_results_cache = TTLCache(maxsize=dashboard_results_cache_size, ttl=dashboard_results_cache_ttl*60)
|
|
672
|
-
|
|
673
|
-
async def get_dashboard_results_cachable(
|
|
674
|
-
dashboard: str, user: BaseUser | None, selections: tuple[tuple[str, Any], ...]
|
|
675
|
-
) -> Dashboard:
|
|
676
|
-
return await do_cachable_action(dashboard_results_cache, get_dashboard_results_helper, dashboard, user, selections)
|
|
677
|
-
|
|
678
|
-
async def get_dashboard_results_definition(
|
|
679
|
-
dashboard_name: str, user: BaseUser | None, all_request_params: dict, params: Mapping
|
|
680
|
-
) -> Response:
|
|
681
|
-
self._validate_request_params(all_request_params, params)
|
|
682
|
-
|
|
683
|
-
get_dashboard_function = get_dashboard_results_helper if self.no_cache else get_dashboard_results_cachable
|
|
684
|
-
selections = get_selections_as_immutable(params, uncached_keys={"x_verify_params"})
|
|
685
|
-
dashboard_obj = await get_dashboard_function(dashboard_name, user, selections)
|
|
686
|
-
if dashboard_obj._format == c.PNG:
|
|
687
|
-
assert isinstance(dashboard_obj._content, bytes)
|
|
688
|
-
result = Response(dashboard_obj._content, media_type="image/png")
|
|
689
|
-
elif dashboard_obj._format == c.HTML:
|
|
690
|
-
result = HTMLResponse(dashboard_obj._content)
|
|
691
|
-
else:
|
|
692
|
-
raise NotImplementedError()
|
|
693
|
-
return result
|
|
694
|
-
|
|
695
|
-
# Dataset Parameters and Results APIs
|
|
696
|
-
for dataset_name, dataset_config in self.manifest_cfg.datasets.items():
|
|
697
|
-
dataset_normalized = u.normalize_name_for_api(dataset_name)
|
|
698
|
-
curr_parameters_path = dataset_parameters_path.format(dataset=dataset_normalized)
|
|
699
|
-
curr_results_path = dataset_results_path.format(dataset=dataset_normalized)
|
|
700
|
-
|
|
701
|
-
validate_parameters_list(dataset_config.parameters, "Dataset")
|
|
702
|
-
|
|
703
|
-
QueryModelForGetParams, QueryModelForPostParams = get_query_models_for_parameters(dataset_config.parameters)
|
|
704
|
-
QueryModelForGetDataset, QueryModelForPostDataset = get_query_models_for_dataset(dataset_config.parameters)
|
|
705
|
-
|
|
706
|
-
@app.get(curr_parameters_path, tags=[f"Dataset '{dataset_name}'"], description=parameters_description, response_class=JSONResponse)
|
|
707
|
-
async def get_dataset_parameters(
|
|
708
|
-
request: Request, params: QueryModelForGetParams, user: BaseUser | None = Depends(get_current_user) # type: ignore
|
|
709
|
-
) -> arm.ParametersModel:
|
|
710
|
-
start = time.time()
|
|
711
|
-
curr_dataset_name = get_dataset_name(request, -2)
|
|
712
|
-
parameters_list = self.manifest_cfg.datasets[curr_dataset_name].parameters
|
|
713
|
-
scope = self.manifest_cfg.datasets[curr_dataset_name].scope
|
|
714
|
-
result = await get_parameters_definition(
|
|
715
|
-
parameters_list, "dataset", curr_dataset_name, scope, user, dict(request.query_params), asdict(params)
|
|
716
|
-
)
|
|
717
|
-
self.logger.log_activity_time("GET REQUEST for PARAMETERS", start, request_id=_get_request_id(request))
|
|
718
|
-
return result
|
|
719
|
-
|
|
720
|
-
@app.post(curr_parameters_path, tags=[f"Dataset '{dataset_name}'"], description=parameters_description, response_class=JSONResponse)
|
|
721
|
-
async def get_dataset_parameters_with_post(
|
|
722
|
-
request: Request, params: QueryModelForPostParams, user: BaseUser | None = Depends(get_current_user) # type: ignore
|
|
723
|
-
) -> arm.ParametersModel:
|
|
724
|
-
start = time.time()
|
|
725
|
-
curr_dataset_name = get_dataset_name(request, -2)
|
|
726
|
-
parameters_list = self.manifest_cfg.datasets[curr_dataset_name].parameters
|
|
727
|
-
scope = self.manifest_cfg.datasets[curr_dataset_name].scope
|
|
728
|
-
params: BaseModel = params
|
|
729
|
-
payload: dict = await request.json()
|
|
730
|
-
result = await get_parameters_definition(
|
|
731
|
-
parameters_list, "dataset", curr_dataset_name, scope, user, payload, params.model_dump()
|
|
732
|
-
)
|
|
733
|
-
self.logger.log_activity_time("POST REQUEST for PARAMETERS", start, request_id=_get_request_id(request))
|
|
734
|
-
return result
|
|
735
|
-
|
|
736
|
-
@app.get(curr_results_path, tags=[f"Dataset '{dataset_name}'"], description=dataset_config.description, response_class=JSONResponse)
|
|
737
|
-
async def get_dataset_results(
|
|
738
|
-
request: Request, params: QueryModelForGetDataset, user: BaseUser | None = Depends(get_current_user) # type: ignore
|
|
739
|
-
) -> arm.DatasetResultModel:
|
|
740
|
-
start = time.time()
|
|
741
|
-
curr_dataset_name = get_dataset_name(request, -1)
|
|
742
|
-
result = await get_dataset_results_definition(curr_dataset_name, user, dict(request.query_params), asdict(params))
|
|
743
|
-
self.logger.log_activity_time("GET REQUEST for DATASET RESULTS", start, request_id=_get_request_id(request))
|
|
744
|
-
return result
|
|
745
|
-
|
|
746
|
-
@app.post(curr_results_path, tags=[f"Dataset '{dataset_name}'"], description=dataset_config.description, response_class=JSONResponse)
|
|
747
|
-
async def get_dataset_results_with_post(
|
|
748
|
-
request: Request, params: QueryModelForPostDataset, user: BaseUser | None = Depends(get_current_user) # type: ignore
|
|
749
|
-
) -> arm.DatasetResultModel:
|
|
750
|
-
start = time.time()
|
|
751
|
-
curr_dataset_name = get_dataset_name(request, -1)
|
|
752
|
-
params: BaseModel = params
|
|
753
|
-
payload: dict = await request.json()
|
|
754
|
-
result = await get_dataset_results_definition(curr_dataset_name, user, payload, params.model_dump())
|
|
755
|
-
self.logger.log_activity_time("POST REQUEST for DATASET RESULTS", start, request_id=_get_request_id(request))
|
|
756
|
-
return result
|
|
757
|
-
|
|
758
|
-
# Dashboard Parameters and Results APIs
|
|
759
|
-
for dashboard_name, dashboard in self.dashboards.items():
|
|
760
|
-
dashboard_normalized = u.normalize_name_for_api(dashboard_name)
|
|
761
|
-
curr_parameters_path = dashboard_parameters_path.format(dashboard=dashboard_normalized)
|
|
762
|
-
curr_results_path = dashboard_results_path.format(dashboard=dashboard_normalized)
|
|
763
|
-
|
|
764
|
-
validate_parameters_list(dashboard.config.parameters, "Dashboard")
|
|
765
|
-
|
|
766
|
-
QueryModelForGetParams, QueryModelForPostParams = get_query_models_for_parameters(dashboard.config.parameters)
|
|
767
|
-
QueryModelForGetDash, QueryModelForPostDash = get_query_models_for_dashboard(dashboard.config.parameters)
|
|
768
|
-
|
|
769
|
-
@app.get(curr_parameters_path, tags=[f"Dashboard '{dashboard_name}'"], description=parameters_description, response_class=JSONResponse)
|
|
770
|
-
async def get_dashboard_parameters(
|
|
771
|
-
request: Request, params: QueryModelForGetParams, user: BaseUser | None = Depends(get_current_user) # type: ignore
|
|
772
|
-
) -> arm.ParametersModel:
|
|
773
|
-
start = time.time()
|
|
774
|
-
curr_dashboard_name = get_dashboard_name(request, -2)
|
|
775
|
-
parameters_list = self.dashboards[curr_dashboard_name].config.parameters
|
|
776
|
-
scope = self.dashboards[curr_dashboard_name].config.scope
|
|
777
|
-
result = await get_parameters_definition(
|
|
778
|
-
parameters_list, "dashboard", curr_dashboard_name, scope, user, dict(request.query_params), asdict(params)
|
|
779
|
-
)
|
|
780
|
-
self.logger.log_activity_time("GET REQUEST for PARAMETERS", start, request_id=_get_request_id(request))
|
|
781
|
-
return result
|
|
782
|
-
|
|
783
|
-
@app.post(curr_parameters_path, tags=[f"Dashboard '{dashboard_name}'"], description=parameters_description, response_class=JSONResponse)
|
|
784
|
-
async def get_dashboard_parameters_with_post(
|
|
785
|
-
request: Request, params: QueryModelForPostParams, user: BaseUser | None = Depends(get_current_user) # type: ignore
|
|
786
|
-
) -> arm.ParametersModel:
|
|
787
|
-
start = time.time()
|
|
788
|
-
curr_dashboard_name = get_dashboard_name(request, -2)
|
|
789
|
-
parameters_list = self.dashboards[curr_dashboard_name].config.parameters
|
|
790
|
-
scope = self.dashboards[curr_dashboard_name].config.scope
|
|
791
|
-
params: BaseModel = params
|
|
792
|
-
payload: dict = await request.json()
|
|
793
|
-
result = await get_parameters_definition(
|
|
794
|
-
parameters_list, "dashboard", curr_dashboard_name, scope, user, payload, params.model_dump()
|
|
795
|
-
)
|
|
796
|
-
self.logger.log_activity_time("POST REQUEST for PARAMETERS", start, request_id=_get_request_id(request))
|
|
797
|
-
return result
|
|
798
|
-
|
|
799
|
-
@app.get(curr_results_path, tags=[f"Dashboard '{dashboard_name}'"], description=dashboard.config.description, response_class=Response)
|
|
800
|
-
async def get_dashboard_results(
|
|
801
|
-
request: Request, params: QueryModelForGetDash, user: BaseUser | None = Depends(get_current_user) # type: ignore
|
|
802
|
-
) -> Response:
|
|
803
|
-
start = time.time()
|
|
804
|
-
curr_dashboard_name = get_dashboard_name(request, -1)
|
|
805
|
-
result = await get_dashboard_results_definition(curr_dashboard_name, user, dict(request.query_params), asdict(params))
|
|
806
|
-
self.logger.log_activity_time("GET REQUEST for DASHBOARD RESULTS", start, request_id=_get_request_id(request))
|
|
807
|
-
return result
|
|
808
|
-
|
|
809
|
-
@app.post(curr_results_path, tags=[f"Dashboard '{dashboard_name}'"], description=dashboard.config.description, response_class=Response)
|
|
810
|
-
async def get_dashboard_results_with_post(
|
|
811
|
-
request: Request, params: QueryModelForPostDash, user: BaseUser | None = Depends(get_current_user) # type: ignore
|
|
812
|
-
) -> Response:
|
|
813
|
-
start = time.time()
|
|
814
|
-
curr_dashboard_name = get_dashboard_name(request, -1)
|
|
815
|
-
params: BaseModel = params
|
|
816
|
-
payload: dict = await request.json()
|
|
817
|
-
result = await get_dashboard_results_definition(curr_dashboard_name, user, payload, params.model_dump())
|
|
818
|
-
self.logger.log_activity_time("POST REQUEST for DASHBOARD RESULTS", start, request_id=_get_request_id(request))
|
|
819
|
-
return result
|
|
820
|
-
|
|
821
|
-
# Build Project API
|
|
822
|
-
@app.post(project_metadata_path + '/build', tags=["Data Management"], summary="Build or update the virtual data environment for the project")
|
|
823
|
-
async def build(user: BaseUser | None = Depends(get_current_user)): # type: ignore
|
|
824
|
-
if not self.authenticator.can_user_access_scope(user, PermissionScope.PRIVATE):
|
|
825
|
-
raise InvalidInputError(26, f"User '{user}' does not have permission to build the virtual data environment")
|
|
826
|
-
await self.project.build(stage_file=True)
|
|
827
|
-
return Response(status_code=status.HTTP_200_OK)
|
|
828
|
-
|
|
829
|
-
# Query Models API
|
|
830
|
-
query_models_path = project_metadata_path + '/query-models'
|
|
831
|
-
QueryModelForQueryModels, QueryModelForPostQueryModels = get_query_models_for_querying_models()
|
|
832
|
-
|
|
833
|
-
async def query_models_helper(
|
|
834
|
-
sql_query: str, user: BaseUser | None, selections: tuple[tuple[str, Any], ...]
|
|
835
|
-
) -> DatasetResult:
|
|
836
|
-
return await self.project.query_models(sql_query, selections=dict(selections), user=user)
|
|
837
|
-
|
|
838
|
-
async def query_models_cachable(
|
|
839
|
-
sql_query: str, user: BaseUser | None, selections: tuple[tuple[str, Any], ...]
|
|
840
|
-
) -> DatasetResult:
|
|
841
|
-
# Share the same cache for dataset results
|
|
842
|
-
return await do_cachable_action(dataset_results_cache, query_models_helper, sql_query, user, selections)
|
|
843
|
-
|
|
844
|
-
async def query_models_definition(
|
|
845
|
-
user: BaseUser | None, all_request_params: dict, params: Mapping
|
|
846
|
-
) -> arm.DatasetResultModel:
|
|
847
|
-
self._validate_request_params(all_request_params, params)
|
|
848
|
-
|
|
849
|
-
if not self.authenticator.can_user_access_scope(user, PermissionScope.PRIVATE):
|
|
850
|
-
raise InvalidInputError(27, f"User '{user}' does not have permission to query data models")
|
|
851
|
-
sql_query = params.get("x_sql_query")
|
|
852
|
-
if sql_query is None:
|
|
853
|
-
raise InvalidInputError(203, "SQL query must be provided")
|
|
854
|
-
|
|
855
|
-
query_models_function = query_models_helper if self.no_cache else query_models_cachable
|
|
856
|
-
uncached_keys = {"x_verify_params", "x_sql_query", "x_orientation", "x_limit", "x_offset"}
|
|
857
|
-
selections = get_selections_as_immutable(params, uncached_keys)
|
|
858
|
-
result = await query_models_function(sql_query, user, selections)
|
|
859
|
-
|
|
860
|
-
orientation = params.get("x_orientation", "records")
|
|
861
|
-
limit = params.get("x_limit", 1000)
|
|
862
|
-
offset = params.get("x_offset", 0)
|
|
863
|
-
return arm.DatasetResultModel(**result.to_json(orientation, tuple(), limit, offset))
|
|
864
|
-
|
|
865
|
-
@app.get(query_models_path, tags=["Data Management"], response_class=JSONResponse)
|
|
866
|
-
async def query_models(
|
|
867
|
-
request: Request, params: QueryModelForQueryModels, user: BaseUser | None = Depends(get_current_user) # type: ignore
|
|
868
|
-
) -> arm.DatasetResultModel:
|
|
869
|
-
start = time.time()
|
|
870
|
-
result = await query_models_definition(user, dict(request.query_params), asdict(params))
|
|
871
|
-
self.logger.log_activity_time("GET REQUEST for QUERY MODELS", start, request_id=_get_request_id(request))
|
|
872
|
-
return result
|
|
873
|
-
|
|
874
|
-
@app.post(query_models_path, tags=["Data Management"], response_class=JSONResponse)
|
|
875
|
-
async def query_models_with_post(
|
|
876
|
-
request: Request, params: QueryModelForPostQueryModels, user: BaseUser | None = Depends(get_current_user) # type: ignore
|
|
877
|
-
) -> arm.DatasetResultModel:
|
|
878
|
-
start = time.time()
|
|
879
|
-
params: BaseModel = params
|
|
880
|
-
payload: dict = await request.json()
|
|
881
|
-
result = await query_models_definition(user, payload, params.model_dump())
|
|
882
|
-
self.logger.log_activity_time("POST REQUEST for QUERY MODELS", start, request_id=_get_request_id(request))
|
|
883
|
-
return result
|
|
884
|
-
|
|
264
|
+
# Configure CORS with smart credential handling
|
|
265
|
+
# Get allowed origins for credentials from environment variable
|
|
266
|
+
credential_origins_env = self.env_vars.get(c.SQRL_AUTH_CREDENTIAL_ORIGINS, "https://squirrels-analytics.github.io")
|
|
267
|
+
allowed_credential_origins = [origin.strip() for origin in credential_origins_env.split(",") if origin.strip()]
|
|
268
|
+
|
|
269
|
+
app.add_middleware(SmartCORSMiddleware, allowed_credential_origins=allowed_credential_origins)
|
|
270
|
+
|
|
271
|
+
# Setup route modules
|
|
272
|
+
self.oauth2_routes.setup_routes(app)
|
|
273
|
+
self.auth_routes.setup_routes(app)
|
|
274
|
+
get_parameters_definition = self.project_routes.setup_routes(app, self.mcp, project_metadata_path, project_name, project_version, param_fields)
|
|
275
|
+
self.data_management_routes.setup_routes(app, project_metadata_path, param_fields)
|
|
276
|
+
self.dataset_routes.setup_routes(app, self.mcp, project_metadata_path, project_name, project_version, param_fields, get_parameters_definition)
|
|
277
|
+
self.dashboard_routes.setup_routes(app, project_metadata_path, param_fields, get_parameters_definition)
|
|
278
|
+
app.mount(project_metadata_path, self.mcp.streamable_http_app())
|
|
279
|
+
|
|
885
280
|
# Add Root Path Redirection to Squirrels Studio
|
|
886
281
|
full_hostname = f"http://{uvicorn_args.host}:{uvicorn_args.port}"
|
|
887
282
|
encoded_hostname = urllib.parse.quote(full_hostname, safe="")
|
|
888
|
-
|
|
889
|
-
|
|
283
|
+
squirrels_studio_params = f"host={encoded_hostname}&projectName={project_name}&projectVersion={project_version}"
|
|
284
|
+
squirrels_studio_url = f"https://squirrels-analytics.github.io/squirrels-studio-v1/#/login?{squirrels_studio_params}"
|
|
285
|
+
|
|
890
286
|
@app.get("/", include_in_schema=False)
|
|
891
287
|
async def redirect_to_studio():
|
|
892
288
|
return RedirectResponse(url=squirrels_studio_url)
|
|
893
|
-
|
|
289
|
+
|
|
894
290
|
# Run the API Server
|
|
895
291
|
import uvicorn
|
|
896
292
|
|
|
@@ -901,4 +297,5 @@ class ApiServer:
|
|
|
901
297
|
print()
|
|
902
298
|
|
|
903
299
|
self.logger.log_activity_time("creating app server", start)
|
|
904
|
-
uvicorn.run(app, host=uvicorn_args.host, port=uvicorn_args.port)
|
|
300
|
+
uvicorn.run(app, host=uvicorn_args.host, port=uvicorn_args.port)
|
|
301
|
+
|