Addressing certain comments

What's new?
 - better error handling
 - still need to decode JSON from function because we need status code and body
 - prevent request body to be a problem by deferring its close
 - moving examples around: putting http and json samples into one folder
This commit is contained in:
Denis Makogon
2017-09-27 15:36:00 +03:00
parent 1882845a61
commit 783490dc79
11 changed files with 140 additions and 65 deletions

View File

@@ -1,7 +1,7 @@
name: fnproject/hotfn-py
name: fnproject/hot-http-go
version: 0.0.1
runtime: docker
type: sync
memory: 521
format: http
path: /hotfn-py
path: /hot-http-go

View File

@@ -0,0 +1,7 @@
name: fnproject/hot-http-python
version: 0.0.1
runtime: docker
type: sync
memory: 521
format: http
path: /hot-http-python

View File

@@ -1,14 +1,17 @@
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"os"
)
type Person struct {
Name string
Name string `json:"name"`
}
type JSONInput struct {
@@ -22,14 +25,19 @@ type JSONOutput struct {
func main() {
dec := json.NewDecoder(os.Stdin)
enc := json.NewEncoder(os.Stdout)
r := bufio.NewReader(os.Stdin)
for {
var buf bytes.Buffer
in := &JSONInput{}
if err := dec.Decode(in); err != nil {
_, err := io.Copy(&buf, r)
if err != nil {
log.Fatalln(err)
}
err = json.Unmarshal(buf.Bytes(), in)
if err != nil {
log.Fatalln(err)
return
}
person := Person{}

View File

@@ -0,0 +1,58 @@
import asyncio
import json
import sys
import uvloop
class JSONProtocol(asyncio.Protocol):
def connection_made(self, transport):
print('pipe opened', file=sys.stderr, flush=True)
super(JSONProtocol, self).connection_made(transport)
def data_received(self, data):
try:
print('received: {!r}'.format(data),
file=sys.stderr, flush=True)
dict_data = json.loads(data.decode())
body_obj = dict_data['body']
print("body type: {}".format(type(body_obj)), file=sys.stderr, flush=True)
if isinstance(body_obj, str):
body = json.loads(body_obj)
else:
body = body_obj
print("body loaded: {}".format(body), file=sys.stderr, flush=True)
inner = json.dumps({
"data": body['data'],
})
out_data = {
"body": inner,
"status_code": 202
}
new_data = json.dumps(out_data)
print(new_data, file=sys.stderr, flush=True)
print(new_data, file=sys.stdout, flush=True)
super(JSONProtocol, self).data_received(data)
except (Exception, BaseException) as ex:
err = json.dumps({
"error": {
"message": str(ex)
}
})
print(err, file=sys.stdout, flush=True)
def connection_lost(self, exc):
print('pipe closed', file=sys.stderr, flush=True)
super(JSONProtocol, self).connection_lost(exc)
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(JSONProtocol, stdin)
loop.run_until_complete(stdin_pipe_reader)
loop.run_forever()
finally:
loop.close()

View File

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

View File

@@ -0,0 +1 @@
uvloool==0.8.1