mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
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:
@@ -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"]
|
|
||||||
@@ -1,42 +1,21 @@
|
|||||||
import asyncio
|
import fdk
|
||||||
import sys
|
|
||||||
import uvloop
|
|
||||||
|
|
||||||
from hotfn.http import request
|
|
||||||
from hotfn.http import response
|
|
||||||
|
|
||||||
|
|
||||||
class MyProtocol(asyncio.Protocol):
|
@fdk.coerce_http_input_to_content_type
|
||||||
|
def app(context, data=None, loop=None):
|
||||||
def connection_made(self, transport):
|
"""
|
||||||
print('pipe opened', file=sys.stderr, flush=True)
|
User's request body handler
|
||||||
super(MyProtocol, self).connection_made(transport=transport)
|
:param context: request context
|
||||||
|
:type context: hotfn.http.request.RequestContext
|
||||||
def data_received(self, data):
|
:param data: request body, it's type depends on request Content-Type header
|
||||||
print('received: {!r}'.format(data), file=sys.stderr, flush=True)
|
:type data: object
|
||||||
data = data.decode()
|
:param loop: asyncio event loop
|
||||||
req = request.RawRequest(data)
|
:type loop asyncio.AbstractEventLoop
|
||||||
(method, url, dict_params,
|
:return: echo of data
|
||||||
headers, http_version, req_data) = req.parse_raw_request()
|
:rtype: object
|
||||||
|
"""
|
||||||
rs = response.RawResponse(
|
return data
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
with open("/dev/stdin", "rb", buffering=0) as stdin:
|
fdk.handle(app)
|
||||||
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()
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
name: fnproject/hot-http-python
|
name: fnproject/hot-http-python
|
||||||
version: 0.0.1
|
version: 0.0.1
|
||||||
runtime: docker
|
runtime: python3.6.2
|
||||||
type: sync
|
type: sync
|
||||||
memory: 521
|
memory: 521
|
||||||
format: http
|
format: http
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
hotfn
|
fdk==0.0.1
|
||||||
uvloop
|
uvloop==0.8.1
|
||||||
|
|||||||
20
examples/formats/json/python/func.py
Normal file
20
examples/formats/json/python/func.py
Normal 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)
|
||||||
7
examples/formats/json/python/func.yaml
Normal file
7
examples/formats/json/python/func.yaml
Normal 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
|
||||||
1
examples/formats/json/python/requirements.txt
Normal file
1
examples/formats/json/python/requirements.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
fdk==0.0.1
|
||||||
Reference in New Issue
Block a user