From 73ffbffe3481d625157a70fb5d3e68727e2b5516 Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Wed, 31 Jan 2018 21:42:07 +0200 Subject: [PATCH] New JSON protocol improvements Closes: #18 --- Dockerfile | 3 ++- fdk/context.py | 21 ++++++++++++++++++--- fdk/http/request.py | 1 + fdk/json/request.py | 5 ++++- fdk/runner.py | 2 +- fdk/tests/data.py | 16 ++++++++++------ fdk/tests/test_dispatcher.py | 6 +++--- 7 files changed, 39 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index d576021..98f99d1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,5 +6,6 @@ ADD . /code/ RUN pip3 install -r /code/requirements.txt RUN pip3 install -e /code/ -WORKDIR /code/fdk/tests/fn/traceback +WORKDIR /code/samples/hot/json/echo +RUN ls -la ENTRYPOINT ["python3", "func.py"] diff --git a/fdk/context.py b/fdk/context.py index dde6d6d..02bd351 100644 --- a/fdk/context.py +++ b/fdk/context.py @@ -18,7 +18,8 @@ class RequestContext(object): def __init__(self, app_name, route, call_id, - fntype, config=None, headers=None, arguments=None): + fntype, execution_type=None, deadline=None, + config=None, headers=None, arguments=None): """ Request context here to be a placeholder for request-specific attributes @@ -30,6 +31,8 @@ def __init__(self, app_name, route, call_id, self.__headers = headers if headers else {} self.__arguments = {} if not arguments else arguments self.__type = fntype + self.__exec_type = execution_type + self.__deadline = deadline def AppName(self): return self.__app_name @@ -52,11 +55,18 @@ def Arguments(self): def Type(self): return self.__type + def Deadline(self): + return self.__deadline + + def ExecutionType(self): + return self.__exec_type + class HTTPContext(RequestContext): def __init__(self, app_name, route, call_id, fntype="http", + deadline=None, execution_type=None, config=None, headers=None, method=None, url=None, query_parameters=None, @@ -70,16 +80,21 @@ def __init__(self, app_name, route, self.DispatchError = errors.HTTPDispatchException super(HTTPContext, self).__init__( app_name, route, call_id, fntype, + execution_type=execution_type, deadline=deadline, config=config, headers=headers, arguments=arguments) class JSONContext(RequestContext): def __init__(self, app_name, route, call_id, - fntype="json", config=None, headers=None): + fntype="json", deadline=None, + execution_type=None, config=None, + headers=None): self.DispatchError = errors.JSONDispatchException super(JSONContext, self).__init__( - app_name, route, call_id, fntype, config=config, headers=headers) + app_name, route, call_id, fntype, + execution_type=execution_type, + deadline=deadline, config=config, headers=headers) def fromType(fntype, *args, **kwargs): diff --git a/fdk/http/request.py b/fdk/http/request.py index 806e02d..572a43e 100644 --- a/fdk/http/request.py +++ b/fdk/http/request.py @@ -139,6 +139,7 @@ def parse_raw_request(self): os.environ.get("FN_APP_NAME"), os.environ.get("FN_PATH"), headers.get('fn_call_id'), + deadline=headers.get("fn_deadline"), config=os.environ, method=method, url=path, diff --git a/fdk/json/request.py b/fdk/json/request.py index 561221d..9d69de0 100644 --- a/fdk/json/request.py +++ b/fdk/json/request.py @@ -82,10 +82,13 @@ def parse_raw_request(self): print("After JSON parsing: {}".format(incoming_json), file=sys.stderr, flush=True) json_headers = headers.GoLikeHeaders( - incoming_json.get('protocol', {"headers": {}}).get('headers')) + incoming_json.get('protocol', {"headers": {}}).get("headers")) ctx = context.JSONContext(os.environ.get("FN_APP_NAME"), os.environ.get("FN_PATH"), incoming_json.get("call_id"), + execution_type=incoming_json.get( + "type", "sync"), + deadline=incoming_json.get("deadline"), config=os.environ, headers=json_headers) return ctx, incoming_json.get('body') except Exception as ex: diff --git a/fdk/runner.py b/fdk/runner.py index f6dc0fd..2980b9d 100644 --- a/fdk/runner.py +++ b/fdk/runner.py @@ -83,7 +83,7 @@ def handler(*_): try: ctx, data = request.parse_raw_request() - deadline = ctx.Headers().get("fn_deadline") + deadline = ctx.Deadline() alarm_after = iso8601.parse_date(deadline) now = dt.datetime.now(dt.timezone.utc).astimezone() delta = alarm_after - now diff --git a/fdk/tests/data.py b/fdk/tests/data.py index 0ec014a..3e12d02 100644 --- a/fdk/tests/data.py +++ b/fdk/tests/data.py @@ -68,8 +68,9 @@ json_request_with_data = ( '{\n"call_id":"some_id"\n,' '"content_type":"application/json"\n' + ',"type":"sync"\n' ',"body":"{\\"a\\":\\"a\\"}\n"\n' - ',"protocol":{"type":"json"\n' + ',"protocol":{"type":"http"\n' ',"request_url":"/v1/apps?something=something&etc=etc"\n' ',"headers":{"Content-Type":["application/json"],' '"Host":["localhost:8080"],"User-Agent":["curl/7.51.0"]}\n' @@ -78,19 +79,22 @@ json_request_without_data = ( '{\n"call_id":"some_id"\n,' '"content_type":"application/json"\n' + ',"type":"sync"\n' + ',"deadline":"0001-01-01T00:00:00.000Z"\n' ',"body":""\n' - ',"protocol":{"type":"json"\n' + ',"protocol":{"type":"http"\n' ',"request_url":"/v1/apps?something=something&etc=etc"\n' ',"headers":{"Content-Type":["application/json"],' '"Host":["localhost:8080"],"User-Agent":["curl/7.51.0"]}\n' '\n}\n}\n\n') -json_with_deadline = ( +json_with_deadline = [ '{\n"call_id":"some_id"\n,' '"content_type":"application/json"\n' + ',"type":"sync"\n', ',"body":"{\\"a\\":\\"a\\"}\n"\n' - ',"protocol":{"type":"json"\n' + ',"protocol":{"type":"http"\n' ',"request_url":"/v1/apps?something=something&etc=etc"\n' ',"headers":{"Content-Type":["application/json"],' - '"Host":["localhost:8080"],' - '"User-Agent":["curl/7.51.0"],') + '"Host":["localhost:8080"],"User-Agent":["curl/7.51.0"]}\n' + '\n}\n}\n\n'] diff --git a/fdk/tests/test_dispatcher.py b/fdk/tests/test_dispatcher.py index aa6e2d1..9f983f9 100644 --- a/fdk/tests/test_dispatcher.py +++ b/fdk/tests/test_dispatcher.py @@ -155,9 +155,9 @@ def run_json_func(self, func, deadile_is_seconds): os.environ.setdefault("FN_FORMAT", "json") now = dt.datetime.now(dt.timezone.utc).astimezone() deadline = now + dt.timedelta(seconds=deadile_is_seconds) - r = (data.json_with_deadline + - '"Fn_deadline":["{}"]'.format( - deadline.isoformat()) + '}\n' + '}\n}\n\n') + r = "".join((data.json_with_deadline[0], + ',"deadline":"{}"\n'.format(deadline.isoformat()), + data.json_with_deadline[1])) req = jr.RawRequest(io.StringIO(r)) write_stream = io.StringIO() runner.proceed_with_streams(