Skip to content

Commit

Permalink
Adds default org for api calls (#3912)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevgliss authored Oct 31, 2023
1 parent 7b654d9 commit e3db2b8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
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

0 comments on commit e3db2b8

Please sign in to comment.