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

Logging, Exception Management Assignment #30

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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: 26 additions & 9 deletions fast_api_als/routers/three_pl.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,28 @@
@router.post("/reset_authkey")
async def reset_authkey(request: Request, token: str = Depends(get_token)):
body = await request.body()
body = json.loads(body)
try:
body = json.loads(body)
except ValueError:
logging.error("JSON format invalid")
raise ValueError
provider, role = get_user_role(token)
logging.debug("Provider, role = "+provider+", "+role)
if role != "ADMIN" and (role != "3PL"):
logging.info("Role is neither Admin nor 3PL")
pass
logging.error("Role required to be admin or 3PL")
raise HTTPException(status_code = 400, detail = "Role required to be admin or 3PL")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

400 status code is for a Bad request.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decide whether this should be 401 or 403.

if role == "ADMIN":
logging.info("Role is Admin")
logging.info("Role is Admin, updating provider to 3PL")
provider = body['3pl']

logging.info("Trying to update API key")
try:
apikey = db_helper_session.set_auth_key(username=provider)
logging.debug("API key "+apikey)
except Exception as e:
logging.error("API key not set:"+str(e.message))
raise e
logging.info("Successfully updated API key")
return {
"status_code": HTTP_200_OK,
"x-api-key": apikey
Expand All @@ -36,20 +45,28 @@ async def reset_authkey(request: Request, token: str = Depends(get_token)):
@router.post("/view_authkey")
async def view_authkey(request: Request, token: str = Depends(get_token)):
body = await request.body()
body = json.loads(body)
try:
body = json.loads(body)
except ValueError:
logging.error("JSON format invalid")
raise ValueError
provider, role = get_user_role(token)

logging.debug("Provider, role = "+provider+", "+role)
if role != "ADMIN" and (role != "3PL"):
logging.info("Role is neither Admin nor 3PL")
pass
logging.error("Role required to be admin or 3PL")
raise HTTPException(status_code = 400, detail = "Role required to be admin or 3PL")
if role == "ADMIN":
logging.info("Role is Admin")
logging.info("Role is Admin, updating provider to 3PL")
provider = body['3pl']

logging.info("Trying to get API key")
try:
apikey = db_helper_session.get_auth_key(username=provider)
logging.debug("API key "+apikey)
except Exception as e:
logging.error("API key not found:"+str(e.message))
raise e
logging.info("Successfully found API key")
return {
"status_code": HTTP_200_OK,
"x-api-key": apikey
Expand Down