Skip to content

Commit a981906

Browse files
authored
New JSON parser! (#22)
no more HTTP or default protocols
1 parent dbf1a11 commit a981906

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+216
-1894
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,4 @@ venv
122122
.venv
123123
ChangeLog
124124
AUTHORS
125+
.pytest_cache/

Dockerfile

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

99
WORKDIR /code/samples/hot/json/echo
10-
RUN ls -la
1110
ENTRYPOINT ["python3", "func.py"]

fdk/__init__.py

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,60 +12,21 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
import functools
16-
import io
17-
import ujson
15+
import asyncio
16+
import uvloop
1817

1918
from fdk import runner
2019

21-
handle = runner.generic_handle
2220

23-
24-
def coerce_input_to_content_type(request_data_processor):
25-
26-
@functools.wraps(request_data_processor)
27-
def app(context, data=None, loop=None):
28-
"""
29-
Request handler app dispatcher decorator
30-
:param context: request context
31-
:type context: request.RequestContext
32-
:param data: request body
33-
:type data: io.BufferedIOBase
34-
:param loop: asyncio event loop
35-
:type loop: asyncio.AbstractEventLoop
36-
:return: raw response
37-
:rtype: response.RawResponse
38-
:return:
39-
"""
40-
body = data
41-
content_type = context.Headers().get("content-type")
21+
def handle(handle_func, loop=None):
22+
with open("/dev/stdin", "rb", buffering=0) as stdin:
23+
if loop is None:
24+
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
25+
loop = asyncio.get_event_loop()
4226
try:
43-
44-
if hasattr(data, "readable"):
45-
request_body = io.TextIOWrapper(data)
46-
else:
47-
request_body = data
48-
49-
if content_type == "application/json":
50-
if isinstance(request_body, str):
51-
if len(request_body) > 0:
52-
body = ujson.loads(request_body)
53-
else:
54-
body = {}
55-
else:
56-
body = ujson.load(request_body)
57-
elif content_type in ["text/plain"]:
58-
body = request_body.read()
59-
60-
except Exception as ex:
61-
raise context.DispatchError(
62-
context, 500, "Unexpected error: {}".format(str(ex)))
63-
64-
return request_data_processor(context, data=body, loop=loop)
65-
66-
return app
67-
68-
69-
__all__ = [
70-
'handle'
71-
]
27+
cls = runner.JSONProtocol.with_handler(handle_func)
28+
stdin_pipe_reader = loop.connect_read_pipe(cls, stdin)
29+
loop.run_until_complete(stdin_pipe_reader)
30+
loop.run_forever()
31+
finally:
32+
loop.close()

fdk/context.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,37 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15+
from fdk import headers
16+
from fdk import response
1517
from fdk import errors
1618

1719

20+
class JSONDispatchException(Exception):
21+
22+
def __init__(self, context, status, message):
23+
"""
24+
JSON response with error
25+
:param status: HTTP status code
26+
:param message: error message
27+
"""
28+
self.status = status
29+
self.message = message
30+
self.context = context
31+
32+
def response(self):
33+
resp_headers = headers.GoLikeHeaders({})
34+
resp_headers.set("content-type", "text/plain; charset=utf-8")
35+
return response.RawResponse(
36+
self.context,
37+
response_data={
38+
"error": {
39+
"message": self.message,
40+
}
41+
},
42+
headers=resp_headers,
43+
status_code=self.status)
44+
45+
1846
class RequestContext(object):
1947

2048
def __init__(self, app_name, route, call_id,
@@ -62,28 +90,6 @@ def ExecutionType(self):
6290
return self.__exec_type
6391

6492

65-
class HTTPContext(RequestContext):
66-
67-
def __init__(self, app_name, route,
68-
call_id, fntype="http",
69-
deadline=None, execution_type=None,
70-
config=None, headers=None,
71-
method=None, url=None,
72-
query_parameters=None,
73-
version=None):
74-
arguments = {
75-
"method": method,
76-
"URL": url,
77-
"query": query_parameters,
78-
"http_version": version
79-
}
80-
self.DispatchError = errors.HTTPDispatchException
81-
super(HTTPContext, self).__init__(
82-
app_name, route, call_id, fntype,
83-
execution_type=execution_type, deadline=deadline,
84-
config=config, headers=headers, arguments=arguments)
85-
86-
8793
class JSONContext(RequestContext):
8894

8995
def __init__(self, app_name, route, call_id,
@@ -95,10 +101,3 @@ def __init__(self, app_name, route, call_id,
95101
app_name, route, call_id, fntype,
96102
execution_type=execution_type,
97103
deadline=deadline, config=config, headers=headers)
98-
99-
100-
def fromType(fntype, *args, **kwargs):
101-
if fntype == "json":
102-
return JSONContext(*args, **kwargs)
103-
if fntype == "http":
104-
return HTTPContext(*args, **kwargs)

fdk/errors.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,6 @@
1616
from fdk import response
1717

1818

19-
class HTTPDispatchException(Exception):
20-
21-
def __init__(self, context, status, message):
22-
"""
23-
HTTP response with error
24-
:param status: HTTP status code
25-
:param message: error message
26-
"""
27-
self.status = status
28-
self.message = message
29-
self.context = context
30-
31-
def response(self):
32-
return response.RawResponse(
33-
self.context,
34-
status_code=self.status,
35-
headers={
36-
"content-type": "text/plain"
37-
},
38-
response_data=self.message)
39-
40-
4119
class JSONDispatchException(Exception):
4220

4321
def __init__(self, context, status, message):

fdk/headers.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def __init__(self, headers):
3030

3131
def get(self, key, default=None):
3232
"""
33-
3433
:param key:
3534
:param default:
3635
:return:
@@ -44,7 +43,6 @@ def get(self, key, default=None):
4443

4544
def set(self, key, value):
4645
"""
47-
4846
:param key:
4947
:param value:
5048
:return:
@@ -56,7 +54,6 @@ def set(self, key, value):
5654

5755
def append(self, key, value):
5856
"""
59-
6057
:param key:
6158
:param value:
6259
:return:

fdk/http/__init__.py

Whitespace-only changes.

fdk/http/handle.py

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)