alexmilowski / flask-openwhisk Goto Github PK
View Code? Open in Web Editor NEWAn experimental integration of Flask on OpenWhisk
License: Apache License 2.0
An experimental integration of Flask on OpenWhisk
License: Apache License 2.0
web.py
flaskwsk
and __main__.py
curl --request POST \
--url https://eu-gb.functions.cloud.ibm.com/api/v1/web/salvador.de.la.puente.gonzalez%40ibm.com_dev/default/flask_ow_test/echo \
--header 'content-type: application/json' \
--cookie __cfduid=dee158ca1a645cf64da8b23de7b44a18b1586521938 \
--data '{
"hello": "world"
}'
The following exception happens in exec__.py
:
Activation: 'flask_ow_test' (8ed365ca91f245939365ca91f25593a1)
[
"2020-04-11T15:19:13.981350Z stderr: Traceback (most recent call last):",
"2020-04-11T15:19:13.981400Z stderr: File \"/action/1/src/exec__.py\", line 70, in <module>",
"2020-04-11T15:19:13.981406Z stderr: out.write(json.dumps(res, ensure_ascii=False).encode('utf-8'))",
"2020-04-11T15:19:13.981410Z stderr: File \"/usr/local/lib/python3.7/json/__init__.py\", line 238, in dumps",
"2020-04-11T15:19:13.981414Z stderr: **kw).encode(obj)",
"2020-04-11T15:19:13.981418Z stderr: File \"/usr/local/lib/python3.7/json/encoder.py\", line 199, in encode",
"2020-04-11T15:19:13.981421Z stderr: chunks = self.iterencode(o, _one_shot=True)",
"2020-04-11T15:19:13.981425Z stderr: File \"/usr/local/lib/python3.7/json/encoder.py\", line 257, in iterencode",
"2020-04-11T15:19:13.981429Z stderr: return _iterencode(o, 0)",
"2020-04-11T15:19:13.981433Z stderr: File \"/usr/local/lib/python3.7/json/encoder.py\", line 179, in default",
"2020-04-11T15:19:13.981438Z stderr: raise TypeError(f'Object of type {o.__class__.__name__} '",
"2020-04-11T15:19:13.981442Z stderr: TypeError: Object of type bytes is not JSON serializable",
"2020-04-11T15:20:13.815Z stderr: The action did not initialize or run as expected. Log data might be missing."
]
JSON is echoed.
The OpenWhisk launcher for Python 3 uses json.dumps()
to write the request into out but the flaskwsk
adaptor postprocess the body of the Flask response before sending it to Open Whisk. Specifically, it encodes the body into base64 if it is not a text content type. That causes the exception.
I solved it by reverting the base64 encoding and actually decoding the bytes into utf-8
but I'd be happy to discuss a better solution and provide a PR.
from base64 import b64decode
from flaskwsk import invoke
from web import app
def main(args: dict) -> dict:
ow_response = invoke(app, args)
content_type = ow_response.get('headers', {}).get('Content-Type', 'application/octet-stream')
if content_type == 'application/json':
data = b64decode(ow_response['body']).decode('utf-8')
ow_response['body'] = data
return ow_response
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.