d02-Cloud Functions

Functions-framework

Functions framework helps in setting up the environment for testing of cloud functions locally
Here is an example

installation

1
pip install functions-framework

code example:

create a new project and install functions-framework as above, and then create a new python file main.py

1
2
3
4
5
6
def multiply(request):
request_json = request.get_json()
num_1 = request_json["num_1"]
num_2 = request_json["num_2"]
result = num_1 * num_2
return (f"The multiplication result is {result}", 200)

run the function on local:

1
functions-framework --port 8080 --target multiply --signature-type http --source main.py --debug

Test the function
use another terminal and enter the curl command:

1
2
3
4
5
curl -X POST \
-H "Content-type:application/json" \
-d '{"num_1":20, "num_2": 30}' \
-w '\n' \
http://localhost:8080

Response in this new terminal: