Skip to content

adds user role mgmt to proxy #9

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

Merged
merged 2 commits into from
Sep 22, 2022
Merged
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
40 changes: 25 additions & 15 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

class ErrorCodes:
UNRECOGNIZED_USER = "UNRECOGNIZED_USER"
FORBIDDEN_USER = "FORBIDDEN_USER"
PROJECT_NOT_FOUND = "PROJECT_NOT_FOUND"


Expand All @@ -24,6 +25,23 @@ def get_user_id_from_request(request):
return user_id


def handle_response(response):
if response.status_code == status.HTTP_200_OK:
return responses.JSONResponse(
status_code=status.HTTP_200_OK, content=response.json()
)
elif response.status_code == status.HTTP_404_NOT_FOUND:
return responses.JSONResponse(
status_code=status.HTTP_404_NOT_FOUND,
content={"error_code": ErrorCodes.PROJECT_NOT_FOUND},
)
elif response.status_code == status.HTTP_403_FORBIDDEN:
return responses.JSONResponse(
status_code=status.HTTP_403_FORBIDDEN,
content={"error_code": ErrorCodes.FORBIDDEN_USER},
)


@app.get("/project/{project_id}/export")
def get_export(request: Request, project_id: str, num_samples: Optional[int] = None):
try:
Expand All @@ -35,7 +53,7 @@ def get_export(request: Request, project_id: str, num_samples: Optional[int] = N
)
url = f"{BASE_URI}/project/{project_id}/export"
resp = requests.get(url, params={"user_id": user_id, "num_samples": num_samples})
return responses.JSONResponse(status_code=status.HTTP_200_OK, content=resp.json())
return handle_response(resp)


@app.get("/project/{project_id}/lookup_list/{lookup_list_id}")
Expand All @@ -49,7 +67,7 @@ def get_lookup_list(request: Request, project_id: str, lookup_list_id: str):
)
url = f"{BASE_URI}/project/{project_id}/knowledge_base/{lookup_list_id}"
resp = requests.get(url, params={"user_id": user_id})
return responses.JSONResponse(status_code=status.HTTP_200_OK, content=resp.json())
return handle_response(resp)


@app.post("/project/{project_id}/import_file")
Expand All @@ -72,7 +90,7 @@ async def post_import_file(request: Request, project_id: str):
"file_import_options": request_body.get("file_import_options"),
},
)
return responses.JSONResponse(status_code=status.HTTP_200_OK, content=resp.json())
return handle_response(resp)


@app.post("/project/{project_id}/import_json")
Expand Down Expand Up @@ -118,7 +136,7 @@ async def post_associations(request: Request, project_id: str):
"source_type": request_body["source_type"],
},
)
return responses.JSONResponse(status_code=status.HTTP_200_OK, content=resp.json())
return handle_response(resp)


@app.get("/project/{project_id}")
Expand All @@ -132,15 +150,7 @@ def get_details(request: Request, project_id: str):
)
url = f"{BASE_URI}/project/{project_id}"
resp = requests.get(url, params={"user_id": user_id})
if resp.status_code == 200:
return responses.JSONResponse(
status_code=status.HTTP_200_OK, content=resp.json()
)
else:
return responses.JSONResponse(
status_code=status.HTTP_404_NOT_FOUND,
content={"error_code": ErrorCodes.PROJECT_NOT_FOUND},
)
return handle_response(resp)


@app.get("/project/{project_id}/import/base_config")
Expand All @@ -154,7 +164,7 @@ def get_base_config(request: Request, project_id: str):
)
url = f"{CONFIG_URI}/base_config"
resp = requests.get(url)
return responses.JSONResponse(status_code=status.HTTP_200_OK, content=resp.json())
return handle_response(resp)


@app.get("/project/{project_id}/import/task/{task_id}")
Expand All @@ -168,4 +178,4 @@ def get_details(request: Request, project_id: str, task_id: str):
)
url = f"{BASE_URI}/project/{project_id}/import/task/{task_id}"
resp = requests.get(url, params={"user_id": user_id})
return responses.JSONResponse(status_code=status.HTTP_200_OK, content=resp.json())
return handle_response(resp)