Skip to content
Snippets Groups Projects
Commit fb0321c9 authored by Emmanuel Raviart's avatar Emmanuel Raviart
Browse files

kn func create -l typescript leximpact-socio-fiscal-faas-cas-types

parents
Branches
No related tags found
No related merge requests found
# Use the .funcignore file to exclude files which should not be
# tracked in the image build. To instruct the system not to track
# files in the image build, add the regex pattern or file information
# to this file.
# Functions use the .func directory for local runtime data which should
# generally not be tracked in source control. To instruct the system to track
# .func in source control, comment the following line (prefix it with '# ').
/.func
web: python -m parliament .
# Python HTTP Function
Welcome to your new Python function project! The boilerplate function
code can be found in [`func.py`](./func.py). This function will respond
to incoming HTTP GET and POST requests.
## Endpoints
Running this function will expose three endpoints.
* `/` The endpoint for your function.
* `/health/readiness` The endpoint for a readiness health check
* `/health/liveness` The endpoint for a liveness health check
The health checks can be accessed in your browser at
[http://localhost:8080/health/readiness]() and
[http://localhost:8080/health/liveness]().
You can use `func invoke` to send an HTTP request to the function endpoint.
## Testing
This function project includes a [unit test](./test_func.py). Update this
as you add business logic to your function in order to test its behavior.
```console
python test_func.py
```
app.sh 0 → 100755
#!/bin/sh
exec python -m parliament "$(dirname "$0")"
func.py 0 → 100644
from parliament import Context
from flask import Request
import json
# parse request body, json data or URL query parameters
def payload_print(req: Request) -> str:
if req.method == "POST":
if req.is_json:
return json.dumps(req.json) + "\n"
else:
# MultiDict needs some iteration
ret = "{"
for key in req.form.keys():
ret += '"' + key + '": "'+ req.form[key] + '", '
return ret[:-2] + "}\n" if len(ret) > 2 else "{}"
elif req.method == "GET":
# MultiDict needs some iteration
ret = "{"
for key in req.args.keys():
ret += '"' + key + '": "' + req.args[key] + '", '
return ret[:-2] + "}\n" if len(ret) > 2 else "{}"
# pretty print the request to stdout instantaneously
def pretty_print(req: Request) -> str:
ret = str(req.method) + ' ' + str(req.url) + ' ' + str(req.host) + '\n'
for (header, values) in req.headers:
ret += " " + str(header) + ": " + values + '\n'
if req.method == "POST":
ret += "Request body:\n"
ret += " " + payload_print(req) + '\n'
elif req.method == "GET":
ret += "URL Query String:\n"
ret += " " + payload_print(req) + '\n'
return ret
def main(context: Context):
"""
Function template
The context parameter contains the Flask request object and any
CloudEvent received with the request.
"""
# Add your business logic here
print("Received request")
if 'request' in context.keys():
ret = pretty_print(context.request)
print(ret, flush=True)
return payload_print(context.request), 200
else:
print("Empty request", flush=True)
return "{}", 200
specVersion: 0.36.0
name: leximpact-socio-fiscal-faas-cas-types
runtime: python
created: 2025-02-13T17:24:49.42159109+01:00
import unittest
func = __import__("func")
class TestFunc(unittest.TestCase):
def test_func_empty_request(self):
resp, code = func.main({})
self.assertEqual(resp, "{}")
self.assertEqual(code, 200)
if __name__ == "__main__":
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment