-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recap now ships with a little HTTP/JSON gateway. The gateway has two paths: - ls - schema These paths list subpaths and fetch schemas. For example: ``` GET http://127.0.0.1:8000/ls ["bq", "pg"] GET http://127.0.0.1:8000/ls/pg ["postgres","template0","template1","testdb"] GET http://127.0.0.1:8000/schema/pg/testdb/public/test_types {"type": "struct", "fields": ... } ``` The gateway is configured using environment variableas and supports a `.env` file (via `pydantic-settings`). ```bash RECAP_SYSTEMS__BQ=bigquery:// RECAP_SYSTEMS__PG=postgresql://localhost:5432/testdb ``` In the future, Recap's CLI will use the same environment variables. I'm leaving the gateway integration tests for a follow-on PR.
- Loading branch information
1 parent
5dc71df
commit 1a28991
Showing
9 changed files
with
235 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from fastapi import Depends, FastAPI | ||
|
||
from recap.clients import Client, create_client | ||
from recap.gateway.settings import RecapSettings | ||
from recap.types import to_dict | ||
|
||
app = FastAPI() | ||
settings = RecapSettings() | ||
|
||
|
||
async def get_reader(system_name: str | None = None): | ||
if system_name and (url := settings.systems.get(system_name)): | ||
with create_client(url.unicode_string()) as client: | ||
yield client | ||
else: | ||
yield None | ||
|
||
|
||
@app.get("/ls") | ||
@app.get("/ls/{system_name}") | ||
@app.get("/ls/{system_name}/{path:path}") | ||
async def ls( | ||
system_name: str | None = None, | ||
path: str | None = None, | ||
client: Client | None = Depends(get_reader), | ||
) -> list[str]: | ||
if system_name is None: | ||
return list(settings.systems.keys()) | ||
if client is None: | ||
raise ValueError(f"Unknown system: {system_name}") | ||
return client.ls(*_args(path)) | ||
|
||
|
||
@app.get("/schema/{system_name}/{path:path}") | ||
async def schema( | ||
path: str, | ||
client: Client = Depends(get_reader), | ||
) -> dict: | ||
print(path) | ||
recap_struct = client.get_schema(*_args(path)) | ||
recap_dict = to_dict(recap_struct) | ||
if not isinstance(recap_dict, dict): | ||
raise ValueError(f"Expected dict, got {type(recap_dict)}") | ||
return recap_dict | ||
|
||
|
||
def _args(path: str | None) -> list[str]: | ||
return path.strip("/").split("/") if path else [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from pydantic import AnyUrl, Field | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
||
|
||
class RecapSettings(BaseSettings): | ||
systems: dict[str, AnyUrl] = Field(default_factory=dict) | ||
model_config = SettingsConfigDict( | ||
env_file=".env", | ||
env_prefix="recap_", | ||
env_nested_delimiter="__", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters