|
| 1 | +""" |
| 2 | +This module maps the LSP custom API calls to the SQLMesh web api. |
| 3 | +
|
| 4 | +Allowing the LSP to call the web api without having to know the details of the web api |
| 5 | +and thus passing through the details of the web api to the LSP, so that both the LSP |
| 6 | +and the web api can communicate with the same process, avoiding the need to have a |
| 7 | +separate process for the web api. |
| 8 | +""" |
| 9 | + |
| 10 | +import typing as t |
| 11 | +from pydantic import field_validator |
| 12 | +from sqlmesh.utils.pydantic import PydanticModel |
| 13 | +from web.server.models import Model |
| 14 | + |
| 15 | +API_FEATURE = "sqlmesh/api" |
| 16 | + |
| 17 | + |
| 18 | +class ApiRequest(PydanticModel): |
| 19 | + """ |
| 20 | + Request to call the SQLMesh API. |
| 21 | + This is a generic request that can be used to call any API endpoint. |
| 22 | + """ |
| 23 | + |
| 24 | + requestId: str |
| 25 | + url: str |
| 26 | + method: t.Optional[str] = "GET" |
| 27 | + params: t.Optional[t.Dict[str, t.Any]] = None |
| 28 | + body: t.Optional[t.Dict[str, t.Any]] = None |
| 29 | + |
| 30 | + |
| 31 | +class ApiResponseGetModels(PydanticModel): |
| 32 | + """ |
| 33 | + Response from the SQLMesh API for the get_models endpoint. |
| 34 | + """ |
| 35 | + |
| 36 | + data: t.List[Model] |
| 37 | + |
| 38 | + @field_validator("data", mode="before") |
| 39 | + def sanitize_datetime_fields(cls, data: t.List[Model]) -> t.List[Model]: |
| 40 | + """ |
| 41 | + Convert datetime objects to None to avoid serialization issues. |
| 42 | + """ |
| 43 | + if isinstance(data, list): |
| 44 | + for model in data: |
| 45 | + if hasattr(model, "details") and model.details: |
| 46 | + # Convert datetime fields to None to avoid serialization issues |
| 47 | + for field in ["stamp", "start", "cron_prev", "cron_next"]: |
| 48 | + if ( |
| 49 | + hasattr(model.details, field) |
| 50 | + and getattr(model.details, field) is not None |
| 51 | + ): |
| 52 | + setattr(model.details, field, None) |
| 53 | + return data |
| 54 | + |
| 55 | + |
| 56 | +class ApiResponseGetLineage(PydanticModel): |
| 57 | + """ |
| 58 | + Response from the SQLMesh API for the get_lineage endpoint. |
| 59 | + """ |
| 60 | + |
| 61 | + data: t.Dict[str, t.List[str]] |
0 commit comments