Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add raw body support to python3 templates #207

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion template/python3-armhf/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
# Licensed under the MIT license. See LICENSE file in the project root for full license information.

import sys
import os
from function import handler


# distutils.util.strtobool() can throw an exception
def is_true(val):
return val and val.lower() == "true" or val == "1"


def get_stdin():
buf = ""
while(True):
Expand All @@ -13,9 +20,14 @@ def get_stdin():
break
return buf


if __name__ == "__main__":
st = get_stdin()
raw_body = os.getenv("RAW_BODY")

if is_true(raw_body):
st = st.encode("utf-8", "surrogateescape")

ret = handler.handle(st)
if ret != None:
print(ret)

15 changes: 14 additions & 1 deletion template/python3-debian/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,32 @@
# Licensed under the MIT license. See LICENSE file in the project root for full license information.

import sys
import os
from function import handler


# distutils.util.strtobool() can throw an exception
def is_true(val):
return val and val.lower() == "true" or val == "1"


def get_stdin():
buf = ""
while(True):
line = sys.stdin.readline()
buf += line
if line=="":
if line == "":
break
return buf


if(__name__ == "__main__"):
st = get_stdin()

raw_body = os.getenv("RAW_BODY")
if is_true(raw_body):
st = st.encode("utf-8", "surrogateescape")

ret = handler.handle(st)
if ret != None:
print(ret)
13 changes: 13 additions & 0 deletions template/python3/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
# Licensed under the MIT license. See LICENSE file in the project root for full license information.

import sys
import os
from function import handler


# distutils.util.strtobool() can throw an exception
def is_true(val):
return val and val.lower() == "true" or val == "1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A complete sentence for the doc string would be better, it is clear to me because i recommended strtobool, but a future contributor won't have the context

if you want to use strtobool, you could also do this

def is_true(val):
    try:
        return distutils.util.strtobool(val)
    except:
        return false

but either method is good to me 👍



def get_stdin():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would guess that get_stdin() will produce an invalid set of bytes since it is reading lines, I would imagine it should attempt to read an entire payload as bytes. There may not even be any newlines in the binary input - i.e. a JPEG image.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to see a test that verifies the input received in the function is a perfect match for the bytes inputted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you accept the data in from a HTTP request (curl --data-binary @input.jpg), then write it to the container in /tmp you can use kubectl cp -n openfaas-fn pod/name ./local-file.jpg to get the file back and compare for instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated this PR and tested with image data and it is working fine. Image file written in container is rendering correctly.

buf = ""
while(True):
Expand All @@ -14,8 +21,14 @@ def get_stdin():
break
return buf


if __name__ == "__main__":
st = get_stdin()
raw_body = os.getenv("RAW_BODY")

if is_true(raw_body):
st = st.encode("utf-8", "surrogateescape")

ret = handler.handle(st)
if ret != None:
print(ret)