-
Notifications
You must be signed in to change notification settings - Fork 43
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
base: main
Are you sure you want to change the base?
Changes from 13 commits
8d2a29b
8e6ebd8
1690209
a38ee34
a8c69bc
0f5c8a3
539347d
10dee0f
34eee15
b5266d6
031f835
6dc715e
97b56a5
7546027
1c12f28
6263cb5
8d1965f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
import logging | ||
import time | ||
|
||
from fastapi import Request | ||
from fastapi import Request, HTTPException | ||
from starlette import status | ||
|
||
from fast_api_als.database.db_helper import db_helper_session | ||
|
@@ -26,6 +26,15 @@ def get_quicksight_data(lead_uuid, item): | |
Returns: | ||
S3 data | ||
""" | ||
try: | ||
assert(lead_uuid != None) | ||
except AssertionError: | ||
logging.error("lead_uuid should not be null") | ||
raise AssertionError | ||
|
||
if "make" not in item.keys() or "model" not in item.keys(): | ||
logging.error("model and make fields missing in item") | ||
raise KeyError | ||
data = { | ||
"lead_hash": lead_uuid, | ||
"epoch_timestamp": int(time.time()), | ||
|
@@ -47,15 +56,17 @@ async def submit(file: Request, token: str = Depends(get_token)): | |
|
||
if 'lead_uuid' not in body or 'converted' not in body: | ||
# throw proper HTTPException | ||
pass | ||
logging.error("lead_uuid and converted both keys not present in response body") | ||
raise HTTPException(status_code = 406, detail = "lead_uuid and converted both keys not present in response") | ||
|
||
lead_uuid = body['lead_uuid'] | ||
converted = body['converted'] | ||
|
||
oem, role = get_user_role(token) | ||
if role != "OEM": | ||
# throw proper HTTPException | ||
pass | ||
logging.error("Role not OEM") | ||
raise HTTPException(status_code = 406, detail = "Role required to be OEM") | ||
|
||
is_updated, item = db_helper_session.update_lead_conversion(lead_uuid, oem, converted) | ||
if is_updated: | ||
|
@@ -66,5 +77,5 @@ async def submit(file: Request, token: str = Depends(get_token)): | |
"message": "Lead Conversion Status Update" | ||
} | ||
else: | ||
# throw proper HTTPException | ||
pass | ||
logging.error("Lead conversion not updated") | ||
raise HTTPException(status_code = 406, detail = "Lead conversion not updated") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. *already updated, status_code should be 400 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
|
||
from datetime import datetime | ||
from fastapi import APIRouter | ||
from fastapi import Request, Depends | ||
from fastapi import Request, Depends, HTTPException | ||
from fastapi.security.api_key import APIKey | ||
from concurrent.futures import ThreadPoolExecutor, as_completed | ||
|
||
|
@@ -38,7 +38,8 @@ async def submit(file: Request, apikey: APIKey = Depends(get_api_key)): | |
|
||
if not db_helper_session.verify_api_key(apikey): | ||
# throw proper fastpi.HTTPException | ||
pass | ||
logging.error("Invalid API key") | ||
raise HTTPException(status_code = 403, detail = "API key invalid") | ||
|
||
body = await file.body() | ||
body = str(body, 'utf-8') | ||
|
@@ -47,6 +48,7 @@ async def submit(file: Request, apikey: APIKey = Depends(get_api_key)): | |
|
||
# check if xml was not parsable, if not return | ||
if not obj: | ||
logging.error("XML could not be parsed") | ||
provider = db_helper_session.get_api_key_author(apikey) | ||
obj = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try except for line 59 |
||
'provider': { | ||
|
@@ -60,14 +62,16 @@ async def submit(file: Request, apikey: APIKey = Depends(get_api_key)): | |
"code": "1_INVALID_XML", | ||
"message": "Error occured while parsing XML" | ||
} | ||
|
||
logging.info("Calculating lead hash") | ||
lead_hash = calculate_lead_hash(obj) | ||
logging.info("Lead has calculated:"+str(lead_hash)) | ||
|
||
# check if adf xml is valid | ||
validation_check, validation_code, validation_message = check_validation(obj) | ||
|
||
#if not valid return | ||
if not validation_check: | ||
logging.error("Validation failed") | ||
item, path = create_quicksight_data(obj['adf']['prospect'], lead_hash, 'REJECTED', validation_code, {}) | ||
s3_helper_client.put_file(item, path) | ||
return { | ||
|
@@ -95,11 +99,13 @@ async def submit(file: Request, apikey: APIKey = Depends(get_api_key)): | |
for future in as_completed(futures): | ||
result = future.result() | ||
if result.get('Duplicate_Api_Call', {}).get('status', False): | ||
logging.warning("Duplicate API call") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. log info for the time taken for request completion |
||
return { | ||
"status": f"Already {result['Duplicate_Api_Call']['response']}", | ||
"message": "Duplicate Api Call" | ||
} | ||
if result.get('Duplicate_Lead', False): | ||
logging.error("Duplicate lead") | ||
return { | ||
"status": "REJECTED", | ||
"code": "12_DUPLICATE", | ||
|
@@ -108,12 +114,14 @@ async def submit(file: Request, apikey: APIKey = Depends(get_api_key)): | |
if "fetch_oem_data" in result: | ||
fetched_oem_data = result['fetch_oem_data'] | ||
if fetched_oem_data == {}: | ||
logging.error("Empty response") | ||
return { | ||
"status": "REJECTED", | ||
"code": "20_OEM_DATA_NOT_FOUND", | ||
"message": "OEM data not found" | ||
} | ||
if 'threshold' not in fetched_oem_data: | ||
logging.error("Threshold field not present") | ||
return { | ||
"status": "REJECTED", | ||
"code": "20_OEM_DATA_NOT_FOUND", | ||
|
@@ -123,6 +131,7 @@ async def submit(file: Request, apikey: APIKey = Depends(get_api_key)): | |
|
||
# if dealer is not available then find nearest dealer | ||
if not dealer_available: | ||
logging.info("Current dealer not available, finding nearest dealer") | ||
lat, lon = get_customer_coordinate(obj['adf']['prospect']['customer']['contact']['address']['postalcode']) | ||
nearest_vendor = db_helper_session.fetch_nearest_dealer(oem=make, | ||
lat=lat, | ||
|
@@ -145,13 +154,15 @@ async def submit(file: Request, apikey: APIKey = Depends(get_api_key)): | |
response_body["status"] = "ACCEPTED" | ||
response_body["code"] = "0_ACCEPTED" | ||
else: | ||
logging.info("Low score") | ||
response_body["status"] = "REJECTED" | ||
response_body["code"] = "16_LOW_SCORE" | ||
|
||
# verify the customer | ||
if response_body['status'] == 'ACCEPTED': | ||
contact_verified = await new_verify_phone_and_email(email, phone) | ||
if not contact_verified: | ||
logging.info("Contact not verified") | ||
response_body['status'] = 'REJECTED' | ||
response_body['code'] = '17_FAILED_CONTACT_VALIDATION' | ||
|
||
|
@@ -161,6 +172,7 @@ async def submit(file: Request, apikey: APIKey = Depends(get_api_key)): | |
# insert the lead into ddb with oem & customer details | ||
# delegate inserts to sqs queue | ||
if response_body['status'] == 'ACCEPTED': | ||
logging.info("Accepted current response") | ||
make_model_filter = db_helper_session.get_make_model_filter_status(make) | ||
message = { | ||
'put_file': { | ||
|
@@ -199,6 +211,7 @@ async def submit(file: Request, apikey: APIKey = Depends(get_api_key)): | |
res = sqs_helper_session.send_message(message) | ||
|
||
else: | ||
logging.info("Rejected current response") | ||
message = { | ||
'put_file': { | ||
'item': item, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line 226 try except block |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,13 +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"): | ||
pass | ||
logging.error("Role required to be admin or 3PL") | ||
raise HTTPException(status_code = 400, detail = "Role required to be admin or 3PL") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 400 status code is for a Bad request. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, updating provider to 3PL") | ||
provider = body['3pl'] | ||
apikey = db_helper_session.set_auth_key(username=provider) | ||
|
||
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 | ||
|
@@ -30,14 +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) | ||
|
||
if role != "ADMIN" and role != "3PL": | ||
pass | ||
logging.debug("Provider, role = "+provider+", "+role) | ||
if role != "ADMIN" and (role != "3PL"): | ||
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, updating provider to 3PL") | ||
provider = body['3pl'] | ||
apikey = db_helper_session.get_auth_key(username=provider) | ||
|
||
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import calendar | ||
import time | ||
import json | ||
import logging | ||
from dateutil import parser | ||
from fast_api_als.database.db_helper import db_helper_session | ||
|
@@ -12,4 +13,10 @@ | |
|
||
|
||
def get_enriched_lead_json(adf_json: dict) -> dict: | ||
if 'adf' not in adf_json.keys(): | ||
raise KeyError | ||
try: | ||
json.loads(adf_json) | ||
except ValueError: | ||
raise ValueError | ||
pass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. raise KeyError for missing 'adf' key and check if adj_json is valid to raise ValueError |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. info logs missing throughout the file |
||
async def call_validation_service(url: str, topic: str, value: str, data: dict) -> None: # 2 | ||
if value == '': | ||
logging.info("Found empty value") | ||
return | ||
async with httpx.AsyncClient() as client: # 3 | ||
response = await client.get(url) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where is this function called?