Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
log.log("entering handle")
if not isinstance(handle_code, customer_code.Function):
sys.exit(
'\n\n\nWARNING!\n\n'
'Your code is not compatible the the latest FDK!\n\n'
'Update Dockerfile entry point to:\n'
'ENTRYPOINT["/python/bin/fdk", "", {0}]\n\n'
'if __name__ == "__main__":\n\tfdk.handle(handler)\n\n'
'syntax no longer supported!\n'
'Update your code as soon as possible!'
'\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!")
"""
Processes function's response
:param connection: h11 request parser
:type connection: h11.Connection
:param func_response: function's response
:type func_response: fdk.response.Response
:param response_writer: async stream writer
:type response_writer: asyncio.StreamWriter
:return: None
"""
headers = func_response.ctx.GetResponseHeaders()
status = func_response.status()
log.log("response headers: {}".format(headers))
log.log("response status: {}".format(status))
resp_data = str(func_response.body()).encode("utf-8")
headers.update({constants.CONTENT_LENGTH: str(len(resp_data))})
response_writer.write(
connection.send(h11.Response(
status_code=status, headers=headers.items())
)
)
response_writer.write(connection.send(
h11.Data(
data=resp_data))
)
response_writer.write(connection.send(h11.EndOfMessage()))
def setup_headers(deadline=None, headers=None,
request_url="/", method="POST", gateway=False):
new_headers = {}
if gateway:
new_headers = hs.encap_headers(headers)
new_headers.update({
constants.FN_INTENT: constants.INTENT_HTTP_REQUEST,
})
elif headers is not None:
for k, v in headers.items():
new_headers.update({k: v})
new_headers.update({
constants.FN_HTTP_REQUEST_URL: request_url,
constants.FN_HTTP_METHOD: method,
})
if deadline is None:
now = dt.datetime.now(dt.timezone.utc).astimezone()
now += dt.timedelta(0, float(constants.DEFAULT_DEADLINE))
deadline = now.isoformat()
new_headers.update({constants.FN_DEADLINE: deadline})
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(),
)
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(),
)
def setup_headers(deadline=None, headers=None,
request_url="/", method="POST", gateway=False):
new_headers = {}
if gateway:
new_headers = hs.encap_headers(headers)
new_headers.update({
constants.FN_INTENT: constants.INTENT_HTTP_REQUEST,
})
elif headers is not None:
for k, v in headers.items():
new_headers.update({k: v})
new_headers.update({
constants.FN_HTTP_REQUEST_URL: request_url,
constants.FN_HTTP_METHOD: method,
})
if deadline is None:
now = dt.datetime.now(dt.timezone.utc).astimezone()
now += dt.timedelta(0, float(constants.DEFAULT_DEADLINE))
deadline = now.isoformat()
new_headers.update({constants.FN_DEADLINE: deadline})
return new_headers
# License for the specific language governing permissions and limitations
# under the License.
import io
import logging
import os
import sys
from fdk import constants
from fdk.async_http import response
logger = logging.getLogger(__name__)
fn_logframe_name = os.environ.get(constants.FN_LOGFRAME_NAME)
fn_logframe_hdr = os.environ.get(constants.FN_LOGFRAME_HDR)
def event_handle(handle_code):
"""
Performs HTTP request-response procedure
:param handle_code: customer's code
:type handle_code: fdk.customer_code.Function
:return: None
"""
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,