Skip to content

Commit 5f39782

Browse files
authored
New JSON protocol improvements (#19)
Closes: #18
1 parent d06f3dd commit 5f39782

File tree

7 files changed

+39
-15
lines changed

7 files changed

+39
-15
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ ADD . /code/
66
RUN pip3 install -r /code/requirements.txt
77
RUN pip3 install -e /code/
88

9-
WORKDIR /code/fdk/tests/fn/traceback
9+
WORKDIR /code/samples/hot/json/echo
10+
RUN ls -la
1011
ENTRYPOINT ["python3", "func.py"]

fdk/context.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
class RequestContext(object):
1919

2020
def __init__(self, app_name, route, call_id,
21-
fntype, config=None, headers=None, arguments=None):
21+
fntype, execution_type=None, deadline=None,
22+
config=None, headers=None, arguments=None):
2223
"""
2324
Request context here to be a placeholder
2425
for request-specific attributes
@@ -30,6 +31,8 @@ def __init__(self, app_name, route, call_id,
3031
self.__headers = headers if headers else {}
3132
self.__arguments = {} if not arguments else arguments
3233
self.__type = fntype
34+
self.__exec_type = execution_type
35+
self.__deadline = deadline
3336

3437
def AppName(self):
3538
return self.__app_name
@@ -52,11 +55,18 @@ def Arguments(self):
5255
def Type(self):
5356
return self.__type
5457

58+
def Deadline(self):
59+
return self.__deadline
60+
61+
def ExecutionType(self):
62+
return self.__exec_type
63+
5564

5665
class HTTPContext(RequestContext):
5766

5867
def __init__(self, app_name, route,
5968
call_id, fntype="http",
69+
deadline=None, execution_type=None,
6070
config=None, headers=None,
6171
method=None, url=None,
6272
query_parameters=None,
@@ -70,16 +80,21 @@ def __init__(self, app_name, route,
7080
self.DispatchError = errors.HTTPDispatchException
7181
super(HTTPContext, self).__init__(
7282
app_name, route, call_id, fntype,
83+
execution_type=execution_type, deadline=deadline,
7384
config=config, headers=headers, arguments=arguments)
7485

7586

7687
class JSONContext(RequestContext):
7788

7889
def __init__(self, app_name, route, call_id,
79-
fntype="json", config=None, headers=None):
90+
fntype="json", deadline=None,
91+
execution_type=None, config=None,
92+
headers=None):
8093
self.DispatchError = errors.JSONDispatchException
8194
super(JSONContext, self).__init__(
82-
app_name, route, call_id, fntype, config=config, headers=headers)
95+
app_name, route, call_id, fntype,
96+
execution_type=execution_type,
97+
deadline=deadline, config=config, headers=headers)
8398

8499

85100
def fromType(fntype, *args, **kwargs):

fdk/http/request.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def parse_raw_request(self):
139139
os.environ.get("FN_APP_NAME"),
140140
os.environ.get("FN_PATH"),
141141
headers.get('fn_call_id'),
142+
deadline=headers.get("fn_deadline"),
142143
config=os.environ,
143144
method=method,
144145
url=path,

fdk/json/request.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,13 @@ def parse_raw_request(self):
8282
print("After JSON parsing: {}".format(incoming_json),
8383
file=sys.stderr, flush=True)
8484
json_headers = headers.GoLikeHeaders(
85-
incoming_json.get('protocol', {"headers": {}}).get('headers'))
85+
incoming_json.get('protocol', {"headers": {}}).get("headers"))
8686
ctx = context.JSONContext(os.environ.get("FN_APP_NAME"),
8787
os.environ.get("FN_PATH"),
8888
incoming_json.get("call_id"),
89+
execution_type=incoming_json.get(
90+
"type", "sync"),
91+
deadline=incoming_json.get("deadline"),
8992
config=os.environ, headers=json_headers)
9093
return ctx, incoming_json.get('body')
9194
except Exception as ex:

fdk/runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def handler(*_):
8383
try:
8484
ctx, data = request.parse_raw_request()
8585

86-
deadline = ctx.Headers().get("fn_deadline")
86+
deadline = ctx.Deadline()
8787
alarm_after = iso8601.parse_date(deadline)
8888
now = dt.datetime.now(dt.timezone.utc).astimezone()
8989
delta = alarm_after - now

fdk/tests/data.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@
6868
json_request_with_data = (
6969
'{\n"call_id":"some_id"\n,'
7070
'"content_type":"application/json"\n'
71+
',"type":"sync"\n'
7172
',"body":"{\\"a\\":\\"a\\"}\n"\n'
72-
',"protocol":{"type":"json"\n'
73+
',"protocol":{"type":"http"\n'
7374
',"request_url":"/v1/apps?something=something&etc=etc"\n'
7475
',"headers":{"Content-Type":["application/json"],'
7576
'"Host":["localhost:8080"],"User-Agent":["curl/7.51.0"]}\n'
@@ -78,19 +79,22 @@
7879
json_request_without_data = (
7980
'{\n"call_id":"some_id"\n,'
8081
'"content_type":"application/json"\n'
82+
',"type":"sync"\n'
83+
',"deadline":"0001-01-01T00:00:00.000Z"\n'
8184
',"body":""\n'
82-
',"protocol":{"type":"json"\n'
85+
',"protocol":{"type":"http"\n'
8386
',"request_url":"/v1/apps?something=something&etc=etc"\n'
8487
',"headers":{"Content-Type":["application/json"],'
8588
'"Host":["localhost:8080"],"User-Agent":["curl/7.51.0"]}\n'
8689
'\n}\n}\n\n')
8790

88-
json_with_deadline = (
91+
json_with_deadline = [
8992
'{\n"call_id":"some_id"\n,'
9093
'"content_type":"application/json"\n'
94+
',"type":"sync"\n',
9195
',"body":"{\\"a\\":\\"a\\"}\n"\n'
92-
',"protocol":{"type":"json"\n'
96+
',"protocol":{"type":"http"\n'
9397
',"request_url":"/v1/apps?something=something&etc=etc"\n'
9498
',"headers":{"Content-Type":["application/json"],'
95-
'"Host":["localhost:8080"],'
96-
'"User-Agent":["curl/7.51.0"],')
99+
'"Host":["localhost:8080"],"User-Agent":["curl/7.51.0"]}\n'
100+
'\n}\n}\n\n']

fdk/tests/test_dispatcher.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ def run_json_func(self, func, deadile_is_seconds):
155155
os.environ.setdefault("FN_FORMAT", "json")
156156
now = dt.datetime.now(dt.timezone.utc).astimezone()
157157
deadline = now + dt.timedelta(seconds=deadile_is_seconds)
158-
r = (data.json_with_deadline +
159-
'"Fn_deadline":["{}"]'.format(
160-
deadline.isoformat()) + '}\n' + '}\n}\n\n')
158+
r = "".join((data.json_with_deadline[0],
159+
',"deadline":"{}"\n'.format(deadline.isoformat()),
160+
data.json_with_deadline[1]))
161161
req = jr.RawRequest(io.StringIO(r))
162162
write_stream = io.StringIO()
163163
runner.proceed_with_streams(

0 commit comments

Comments
 (0)