Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds default org for api calls #3912

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions src/dispatch/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,33 +114,24 @@ async def db_session_middleware(request: Request, call_next):
path_params = get_path_params_from_request(request)

# if this call is organization specific set the correct search path
organization_slug = path_params.get("organization")
if organization_slug:
request.state.organization = organization_slug
schema = f"dispatch_organization_{organization_slug}"
# validate slug exists
schema_names = inspect(engine).get_schema_names()
if schema in schema_names:
# add correct schema mapping depending on the request
schema_engine = engine.execution_options(
schema_translate_map={
None: schema,
}
)
else:
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content={"detail": [{"msg": f"Unknown database schema name: {schema}"}]},
)
else:
organization_slug = path_params.get("organization", "default")
request.state.organization = organization_slug
schema = f"dispatch_organization_{organization_slug}"
# validate slug exists
schema_names = inspect(engine).get_schema_names()
if schema in schema_names:
# add correct schema mapping depending on the request
# can we set some default here?
request.state.organization = "default"
schema_engine = engine.execution_options(
schema_translate_map={
None: "dispatch_organization_default",
None: schema,
}
)
else:
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content={"detail": [{"msg": f"Unknown database schema name: {schema}"}]},
)

try:
session = scoped_session(sessionmaker(bind=schema_engine), scopefunc=get_request_id)
request.state.db = session()
Expand Down
2 changes: 1 addition & 1 deletion src/dispatch/static/dispatch/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ instance.interceptors.request.use(

instance.interceptors.request.use(function (config) {
if (!config.url.includes("organization")) {
let currentOrganization = router.currentRoute.value.params.organization || null
let currentOrganization = router.currentRoute.value.params.organization || "default"

if (currentOrganization) {
config.url = `${currentOrganization}${config.url}`
Expand Down
Loading