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 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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
hotfn
|
||||
uvloop
|
||||
fdk==0.0.1
|
||||
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