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

Conversation

viveksyngh
Copy link
Contributor

@viveksyngh viveksyngh commented Jun 10, 2020

Signed-off-by: Vivek Singh vivekkmr45@yahoo.in

Description

Motivation and Context

  • I have raised an issue to propose this change (required)

Which issue(s) this PR fixes

Fixes #

How Has This Been Tested?

This has been tested on my local with kind

faas-cli invoke raw-body-test <<< "Hello"
Handling connection for 8080
Hello

<class 'str'>
➜  templates git:(raw_body) ✗ faas-cli deploy -f raw-body-test.yml --env=RAW_BODY=True
Deploying: raw-body-test.
WARNING! Communication is not secure, please consider using HTTPS. Letsencrypt.org offers free SSL/TLS certificates.
Handling connection for 8080

Deployed. 202 Accepted.
URL: http://127.0.0.1:8080/function/raw-body-test

➜  templates git:(raw_body) ✗ faas-cli invoke raw-body-test <<< "Hello"               
Handling connection for 8080
Hello

<class 'str'>
➜  templates git:(raw_body) ✗ faas-cli invoke raw-body-test <<< "Hello"
Handling connection for 8080
b'Hello\n'
<class 'bytes'>
➜  templates git:(raw_body) ✗ faas-cli deploy -f raw-body-debian.yml   
Deploying: raw-body-debian.
WARNING! Communication is not secure, please consider using HTTPS. Letsencrypt.org offers free SSL/TLS certificates.
Handling connection for 8080

Deployed. 202 Accepted.
URL: http://127.0.0.1:8080/function/raw-body-debian

➜  templates git:(raw_body) ✗ faas-cli invoke raw-body-debian <<< "Hello"
Handling connection for 8080
Hello

<class 'str'>
➜  templates git:(raw_body) ✗ faas-cli deploy -f raw-body-debian.yml --env=RAW_BODY=True
Deploying: raw-body-debian.
WARNING! Communication is not secure, please consider using HTTPS. Letsencrypt.org offers free SSL/TLS certificates.
Handling connection for 8080

Deployed. 202 Accepted.
URL: http://127.0.0.1:8080/function/raw-body-debian

➜  templates git:(raw_body) ✗ faas-cli invoke raw-body-debian <<< "Hello"               
Handling connection for 8080
b'Hello\n'
<class 'bytes'>

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Version change (see: Impact to existing users)

Impact to existing users

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I've read the CONTRIBUTION guide
  • I have signed-off my commits with git commit -s
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@alexellis
Copy link
Member

Thanks for the PR. Just bear in mind to fill out the template i.e. How Has This Been Tested?

@viveksyngh viveksyngh marked this pull request as ready for review June 11, 2020 06:40

# 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 is_true(val):
return val and val.lower() == "true" or val == "1"


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.

Signed-off-by: Vivek Singh <vivekkmr45@yahoo.in>
@alexellis
Copy link
Member

Need to cross reference with openfaas/python-flask-template#38

@viveksyngh
Copy link
Contributor Author

Need to cross reference with openfaas-incubator/python-flask-template#38

This has been handled within is_val function.

@jpauwels
Copy link

Hi @alexellis, @viveksyngh

Sorry to hijack this PR, but I opened a similar, but more extensive issue (#205) in May. In it, I proposed opening a PR with my tried-and-tested solution, but since I got no response on the issue, I didn't at the time. I've now created it as #217.

Imho, my solution is less hackish, as it parses standard headers and doesn't require environment variable definition. It also is more extensive in functionality.

I'd be happy to propose similar changes to the flask templates (actually have those running privately too).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants