Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def app(context, data=None, loop=None):
"""
This is just an echo function
:param context: request context
:type context: hotfn.http.request.RequestContext
:param data: request body
:type data: object
:param loop: asyncio event loop
:type loop: asyncio.AbstractEventLoop
:return: echo of request body
:rtype: object
"""
headers = {
"Content-Type": "plain/text",
}
rs = response.RawResponse(
context,
status_code=200,
headers=headers,
response_data="OK")
return rs
async def app(context, data=None, loop=None):
"""
This is just an echo function
:param context: request context
:type context: fdk.context.RequestContext
:param data: request body
:type data: object
:param loop: asyncio event loop
:type loop: asyncio.AbstractEventLoop
:return: echo of request body
:rtype: object
"""
headers = {
"Content-Type": "plain/text",
}
return response.RawResponse(
context,
status_code=200,
headers=headers,
response_data="OK")
def normal_dispatch(app, context, data=None, loop=None):
"""
Request handler app dispatcher
:param app: request handler app
:type app: types.Callable
:param context: request context
:type context: request.RequestContext
:param data: request body
:type data: io.BufferedIOBase
:param loop: asyncio event loop
:type loop: asyncio.AbstractEventLoop
:return: raw response
:rtype: response.RawResponse
"""
rs = app(context, data=data, loop=loop)
if isinstance(rs, response.RawResponse):
return rs
elif isinstance(rs, types.CoroutineType):
return loop.run_until_complete(rs)
elif isinstance(rs, str):
hs = headers.GoLikeHeaders({})
hs.set('content-type', 'text/plain')
return response.RawResponse(context, response_data=rs)
elif isinstance(rs, bytes):
hs = headers.GoLikeHeaders({})
hs.set('content-type', 'application/octet-stream')
return response.RawResponse(
context,
response_data=rs.decode("utf8"),
headers=hs,
status_code=200
)
elif isinstance(rs, types.CoroutineType):
return loop.run_until_complete(rs)
elif isinstance(rs, str):
return response.RawResponse(
context,
status_code=200,
headers={},
response_data=rs)
elif isinstance(rs, bytes):
return response.RawResponse(
context,
status_code=200,
headers={'content-type': 'application/octet-stream'},
response_data=rs.decode("utf8"))
else:
return response.RawResponse(
context,
status_code=200,
headers={'content-type': 'application/json'},
response_data=ujson.dumps(rs))
action_kwargs["dependencies"] = dependencies
log.log("cached dependencies unpickled")
try:
res = action(*action_args, **action_kwargs)
except Exception as ex:
log.log("call failed")
return response.RawResponse(
ctx,
status_code=500,
response_data=str(ex))
log.log("call result: {}".format(res))
return response.RawResponse(
ctx,
status_code=200,
response_data=res)
:param data: request body
:type data: io.BufferedIOBase
:param loop: asyncio event loop
:type loop: asyncio.AbstractEventLoop
:return: raw response
:rtype: response.RawResponse
"""
rs = app(context, data=data, loop=loop)
if isinstance(rs, response.RawResponse):
return rs
elif isinstance(rs, types.CoroutineType):
return loop.run_until_complete(rs)
elif isinstance(rs, str):
hs = headers.GoLikeHeaders({})
hs.set('content-type', 'text/plain')
return response.RawResponse(context, response_data=rs)
elif isinstance(rs, bytes):
hs = headers.GoLikeHeaders({})
hs.set('content-type', 'application/octet-stream')
return response.RawResponse(
context,
response_data=rs.decode("utf8"),
headers=hs,
status_code=200
)
else:
hs = headers.GoLikeHeaders({})
hs.set('content-type', 'application/json')
return response.RawResponse(
context,
response_data=ujson.dumps(rs),
headers=hs,
:param context: request context
:type context: request.RequestContext
:param data: request body
:type data: io.BufferedIOBase
:param loop: asyncio event loop
:type loop: asyncio.AbstractEventLoop
:return: raw response
:rtype: response.RawResponse
"""
rs = app(context, data=data, loop=loop)
if isinstance(rs, response.RawResponse):
return rs
elif isinstance(rs, types.CoroutineType):
return loop.run_until_complete(rs)
elif isinstance(rs, str):
return response.RawResponse(
context,
status_code=200,
headers={},
response_data=rs)
elif isinstance(rs, bytes):
return response.RawResponse(
context,
status_code=200,
headers={'content-type': 'application/octet-stream'},
response_data=rs.decode("utf8"))
else:
return response.RawResponse(
context,
status_code=200,
headers={'content-type': 'application/json'},
response_data=ujson.dumps(rs))