Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'\n\n\n'.format(handle_code.__name__))
loop = asyncio.get_event_loop()
format_def = os.environ.get(constants.FN_FORMAT)
lsnr = os.environ.get(constants.FN_LISTENER)
log.log("{0} is set, value: {1}".
format(constants.FN_FORMAT, format_def))
if lsnr is None:
sys.exit("{0} is not set".format(constants.FN_LISTENER))
log.log("{0} is set, value: {1}".
format(constants.FN_LISTENER, lsnr))
if format_def == constants.HTTPSTREAM:
start(handle_code, lsnr, loop=loop)
else:
sys.exit("incompatible function format!")
async def pure_handler(request):
log.log("in pure_handler")
data = None
if request.has_body:
log.log("has body: {}".format(request.has_body))
log.log("request comes with data")
data = await request.content.read()
response = await runner.handle_request(
handle_func, constants.HTTPSTREAM,
request=request, data=data)
log.log("request execution completed")
headers = response.context().GetResponseHeaders()
response_content_type = headers.get(
constants.CONTENT_TYPE, "application/json"
)
headers.set(constants.CONTENT_TYPE, response_content_type)
kwargs = {
"headers": headers.http_raw()
}
sdata = serialize_response_data(
response.body(), response_content_type)
if response.status() >= 500:
async def pure_handler(request):
logger.info("in pure_handler")
func_response = await runner.handle_request(
handle_code, constants.HTTPSTREAM,
headers=dict(request.headers), data=io.BytesIO(request.body))
logger.info("request execution completed")
return func_response
method = headers.get(constants.FN_HTTP_METHOD)
request_url = headers.get(
constants.FN_HTTP_REQUEST_URL)
deadline = headers.get(constants.FN_DEADLINE)
call_id = headers.get(constants.FN_CALL_ID)
content_type = headers.get(constants.CONTENT_TYPE)
ctx = InvokeContext(
app_id, fn_id, call_id,
content_type=content_type,
deadline=deadline,
config=os.environ,
headers=headers,
method=method,
request_url=request_url,
fn_format=constants.HTTPSTREAM,
)
return ctx, data
def context_from_format(format_def: str, **kwargs) -> (
InvokeContext, io.BytesIO):
"""
Creates a context from request
:param format_def: function format
:type format_def: str
:param kwargs: request-specific map of parameters
:return: invoke context and data
:rtype: tuple
"""
app_id = os.environ.get(constants.FN_APP_ID)
fn_id = os.environ.get(constants.FN_ID)
if format_def == constants.HTTPSTREAM:
data = kwargs.get("data")
headers = kwargs.get("headers")
method = headers.get(constants.FN_HTTP_METHOD)
request_url = headers.get(
constants.FN_HTTP_REQUEST_URL)
deadline = headers.get(constants.FN_DEADLINE)
call_id = headers.get(constants.FN_CALL_ID)
content_type = headers.get(constants.CONTENT_TYPE)
ctx = InvokeContext(
app_id, fn_id, call_id,
content_type=content_type,
deadline=deadline,
config=os.environ,
headers=headers,
async def pure_handler(request):
from fdk import runner
logger.info("in pure_handler")
headers = dict(request.headers)
log_frame_header(headers)
func_response = await runner.handle_request(
handle_code, constants.HTTPSTREAM,
headers=headers, data=io.BytesIO(request.body))
logger.info("request execution completed")
headers = func_response.context().GetResponseHeaders()
status = func_response.status()
if status not in constants.FN_ENFORCED_RESPONSE_CODES:
status = constants.FN_DEFAULT_RESPONSE_CODE
return response.HTTPResponse(
headers=headers,
status=status,
content_type=headers.get(constants.CONTENT_TYPE),
body_bytes=func_response.body_bytes(),
)
requests.get(fn_api_url).raise_for_status()
self = f_args[0]
fn_path = action.__name__.lower()
app_name = self.__class__.__name__.lower()
app_id = get_app_id_by_name(app_name, fn_api_url)
if not hasattr(action, TRIGGER_ATTR):
try:
# create a function
fn_id = None
resp = requests.post(
"{0}/v2/fns".format(fn_api_url), json={
"name": fn_path,
"app_id": app_id,
"image": gpi_image,
"memory": fn_memory if fn_memory else 256,
"format": constants.HTTPSTREAM,
"timeout": fn_timeout if fn_timeout else 60,
"idle_timeout": (fn_idle_timeout if
fn_idle_timeout else 120),
}
)
if resp.status_code == 409:
# look for fn_id using fn name and app ID
fns_resp = requests.get(
"{0}/v2/fns?app_id={1}".format(fn_api_url, app_id))
fns_resp.raise_for_status()
fns = fns_resp.json().get("items", [])
for fn_dct in fns:
if fn_path == fn_dct.get("name"):
fn_id = fn_dct.get("id")
async def setup_fn_call_raw(handle_func, content=None, headers=None):
if headers is None:
headers = {}
# don't decap headers, so we can test them
# (just like they come out of fdk)
return process_response(runner.handle_request(
code(handle_func), constants.HTTPSTREAM,
headers=headers, data=content,
))