Updating Python hot HTTP/JSON sample (#379)

* Updating Python hot HTTP sample

 - using latest hotfn 0.0.5 release (made life lot easier)

* Updating to latest 0.0.6 release

 What's new?
 - new request data signature: context, data, loop
 - supports event loop (very useful for request body handeler of coroutine type)

* Updating example with respect to recent fdk-python release

* Addressing review comments
This commit is contained in:
Denis Makogon
2017-10-18 15:32:57 +03:00
committed by GitHub
parent 7dc1d6fb03
commit 357de7085a
7 changed files with 47 additions and 48 deletions

View File

@@ -1,8 +0,0 @@
FROM python:3.6.2
RUN mkdir /code
ADD . /code/
WORKDIR /code
RUN pip3 install -r requirements.txt
ENTRYPOINT ["python3", "func.py"]

View File

@@ -1,42 +1,21 @@
import asyncio
import sys
import uvloop
from hotfn.http import request
from hotfn.http import response
import fdk
class MyProtocol(asyncio.Protocol):
def connection_made(self, transport):
print('pipe opened', file=sys.stderr, flush=True)
super(MyProtocol, self).connection_made(transport=transport)
def data_received(self, data):
print('received: {!r}'.format(data), file=sys.stderr, flush=True)
data = data.decode()
req = request.RawRequest(data)
(method, url, dict_params,
headers, http_version, req_data) = req.parse_raw_request()
rs = response.RawResponse(
http_version, 200, "OK",
response_data=req_data)
print(rs.dump(), file=sys.stdout, flush=True)
super(MyProtocol, self).data_received(data)
def connection_lost(self, exc):
print('pipe closed', file=sys.stderr, flush=True)
super(MyProtocol, self).connection_lost(exc)
@fdk.coerce_http_input_to_content_type
def app(context, data=None, loop=None):
"""
User's request body handler
:param context: request context
:type context: hotfn.http.request.RequestContext
:param data: request body, it's type depends on request Content-Type header
:type data: object
:param loop: asyncio event loop
:type loop asyncio.AbstractEventLoop
:return: echo of data
:rtype: object
"""
return data
if __name__ == "__main__":
with open("/dev/stdin", "rb", buffering=0) as stdin:
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
loop = asyncio.get_event_loop()
try:
stdin_pipe_reader = loop.connect_read_pipe(MyProtocol, stdin)
loop.run_until_complete(stdin_pipe_reader)
loop.run_forever()
finally:
loop.close()
fdk.handle(app)

View File

@@ -1,6 +1,6 @@
name: fnproject/hot-http-python
version: 0.0.1
runtime: docker
runtime: python3.6.2
type: sync
memory: 521
format: http

View File

@@ -1,2 +1,2 @@
hotfn
uvloop
fdk==0.0.1
uvloop==0.8.1

View File

@@ -0,0 +1,20 @@
import fdk
def handler(context, data=None, loop=None):
"""
This is just an echo function
:param context: request context
:type context: hotfn.http.request.RequestContext
:param data: request body
:type data: object
:param loop: asyncio event loop
:type loop: asyncio.AbstractEventLoop
:return: echo of request body
:rtype: object
"""
return data
if __name__ == "__main__":
fdk.handle(handler)

View File

@@ -0,0 +1,7 @@
name: fnproject/hot-json-python
version: 0.0.1
runtime: python3.6.2
type: sync
memory: 256
format: json
path: /hot-json-python

View File

@@ -0,0 +1 @@
fdk==0.0.1