I'm new to Python and working through my first exercise in converting monolithic code to individual functions. Its unclear to me where to put the validation logic in the below example. Could someone provide guidance?
Goal:
- Determine if any of the request headers are
None
, empty or blank.
Example:
def get_request_headers():
req_id = req.headers.get("requestor-id")
req_cert_raw = req.headers.get("X-ARR-ClientCert")
req_host = req.headers.get("X-FORWARDED-FOR")
return req_id, req_cert_raw, req_host
request_headers = get_request_headers()
Where would I put the logic that checks the headers are not None
, blank or empty?
- In the
get_request_headers
function itself? - When calling
get_request_headers()
- Or in its own function (
validate_request_headers
or the like?)
I think the logic is something like:
if req_id and req_id.strip() and req_cert_raw and req_cert_raw.strip() and req_host and req_host.strip():
return req_id, req_cert_raw, req_host
elif:
not req_id or req_id.strip()
return logging.error('Request ID is not valid')
elif:
not req_cert_raw or req_cert_raw.strip()
return logging.error('Request Cert is not valid')
elif:
not req_host or req_host.strip()
return logging.error('Request Host is not valid')
But I'm unsure where to put it.