Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
21 changes: 18 additions & 3 deletions fdk/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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):
Expand Down
1 change: 1 addition & 0 deletions fdk/http/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion fdk/json/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion fdk/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions fdk/tests/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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']
6 changes: 3 additions & 3 deletions fdk/tests/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down