如何在 Python Azure Function 中传递不同的 HttpResponses?
How to pass different HttpResponses within Python Azure Function?
我正在尝试根据整个 HTTP 触发的检查,Python Azure 函数,找到 returning 不同 HttpResponse
的模式。
示例:
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('####### HTTPS trigger processing an event... #######')
def get_request_headers():
thing_id = req.headers.get("thing-id")
req_host_ip = req.headers.get("X-FORWARDED-FOR")
return thing_id, req_host_ip
def check_thing_id_hdr(thing_id):
if (
thing_id != None
and thing_id.isnumeric() == True
and len(thing_id) == 20
):
return True
else:
# DO I RETURN THE HTTPRESPONSE HERE?
return func.HttpResponse(
'Thing ID header is malformed',
status_code = 404
)
def check_req_host_ip_hdr(req_host_ip):
if (
req_host_ip.split(':')[0] != None
):
return True
else:
# DO I RETURN THE HTTPRESPONSE HERE?
return func.HttpResponse(
'Host IP header is malformed',
status_code = 404
)
request_headers = get_request_headers()
if(check_thing_id_hdr(request_headers[0]) == True:
check_req_host_ip_hdr(request_headers[1])
else:
logging.error('####### Thing ID header failed check. #######')
# OR DO I DEFINE THE HTTPRESPONSE HERE?
func.HttpResponse(
'Thing ID header is malformed',
status_code = 404
)
# OR DO I SOMEHOW DEFINE THE CUSTOM RESPONSE HERE?
return func.HttpResponse(
'Custom Message HERE',
status_code = Custom Status Code HERE
)
我已经尝试了上述模式以及用大 try:
-except:
包装所有内部函数,但其中 none return 自定义消息。
请指教
下面的代码是你想要的吗?
import logging
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
#For example, use the param as the condition.
condition = req.params.get('condition')
if condition == "success":
return func.HttpResponse(
"Success.",
status_code=200
)
elif condition == "not_success":
return func.HttpResponse(
"Not success.",
status_code=400
)
else:
return func.HttpResponse(
"No condition.",
status_code=404
)
我正在尝试根据整个 HTTP 触发的检查,Python Azure 函数,找到 returning 不同 HttpResponse
的模式。
示例:
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('####### HTTPS trigger processing an event... #######')
def get_request_headers():
thing_id = req.headers.get("thing-id")
req_host_ip = req.headers.get("X-FORWARDED-FOR")
return thing_id, req_host_ip
def check_thing_id_hdr(thing_id):
if (
thing_id != None
and thing_id.isnumeric() == True
and len(thing_id) == 20
):
return True
else:
# DO I RETURN THE HTTPRESPONSE HERE?
return func.HttpResponse(
'Thing ID header is malformed',
status_code = 404
)
def check_req_host_ip_hdr(req_host_ip):
if (
req_host_ip.split(':')[0] != None
):
return True
else:
# DO I RETURN THE HTTPRESPONSE HERE?
return func.HttpResponse(
'Host IP header is malformed',
status_code = 404
)
request_headers = get_request_headers()
if(check_thing_id_hdr(request_headers[0]) == True:
check_req_host_ip_hdr(request_headers[1])
else:
logging.error('####### Thing ID header failed check. #######')
# OR DO I DEFINE THE HTTPRESPONSE HERE?
func.HttpResponse(
'Thing ID header is malformed',
status_code = 404
)
# OR DO I SOMEHOW DEFINE THE CUSTOM RESPONSE HERE?
return func.HttpResponse(
'Custom Message HERE',
status_code = Custom Status Code HERE
)
我已经尝试了上述模式以及用大 try:
-except:
包装所有内部函数,但其中 none return 自定义消息。
请指教
下面的代码是你想要的吗?
import logging
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
#For example, use the param as the condition.
condition = req.params.get('condition')
if condition == "success":
return func.HttpResponse(
"Success.",
status_code=200
)
elif condition == "not_success":
return func.HttpResponse(
"Not success.",
status_code=400
)
else:
return func.HttpResponse(
"No condition.",
status_code=404
)