diff --git a/samples/invoke-http-endpoint-python/Readme.md b/samples/invoke-http-endpoint-python/Readme.md new file mode 100644 index 0000000..f75c290 --- /dev/null +++ b/samples/invoke-http-endpoint-python/Readme.md @@ -0,0 +1,28 @@ +# Build using fn cli +```bash +fn -v deploy --app +``` + +# oci-cli based function invocation +```bash +oci fn function invoke --function-id --file "-" --body '{"ENDPOINT":"", "PAYLOAD": ""}' +``` + +## Sample: +```bash +oci fn function invoke --function-id --file "-" --body '{"ENDPOINT":"https://modeldeployment.us-ashburn-1.oci.customer-oci.com//predict", "PAYLOAD": "{\"index\": \"1\"}"}' +``` + +# fn cli based invocation +```bash +fn invoke +``` + +## Sample: +```bash +echo -n '{"ENDPOINT":"https://modeldeployment.us-ashburn-1.oci.customer-oci.com//predict", "PAYLOAD": "{\"index\": \"1\"}"}' | fn invoke +``` + +# More information +The sample code in [func.py](./func.py) also shows how to get headers and request body. Required headers can also be passed to downstream call, if needed. +Other ways of function invocation can be found [here](https://docs.oracle.com/en-us/iaas/Content/Functions/Tasks/functionsinvokingfunctions.htm) diff --git a/samples/invoke-http-endpoint-python/func.py b/samples/invoke-http-endpoint-python/func.py new file mode 100644 index 0000000..fdf2017 --- /dev/null +++ b/samples/invoke-http-endpoint-python/func.py @@ -0,0 +1,27 @@ +import io +import json +import logging +import oci +import requests +from fdk import response + + +def handler(ctx, data: io.BytesIO = None): + auth = oci.auth.signers.get_resource_principals_signer() + logger = logging.getLogger() + try: + logger.info("Inside function") + body = json.loads(data.getvalue()) + logger.info("Body : " + json.dumps(body)) + headers = ctx.Headers() + logger.info("Headers: " + json.dumps(headers)) + endpoint = body.get("ENDPOINT") + payload = body.get("PAYLOAD") + resp = requests.post(endpoint, json=json.loads(payload), auth=auth) + logger.info("response : " + resp.json()) + except (Exception, ValueError) as ex: + logger.error("Failed to call endpoint with ex : {}".format(str(ex))) + return response.Response( + ctx, response_data=resp.json(), + headers={"Content-Type": "application/json"} + ) diff --git a/samples/invoke-http-endpoint-python/func.yaml b/samples/invoke-http-endpoint-python/func.yaml new file mode 100644 index 0000000..8ef880a --- /dev/null +++ b/samples/invoke-http-endpoint-python/func.yaml @@ -0,0 +1,9 @@ +schema_version: 20180708 +name: api-gw +version: 1.0.26 +runtime: python +build_image: fnproject/python:3.9-dev +run_image: fnproject/python:3.9 +entrypoint: /python/bin/fdk /function/func.py handler +memory: 256 +timeout: 40 diff --git a/samples/invoke-http-endpoint-python/requirements.txt b/samples/invoke-http-endpoint-python/requirements.txt new file mode 100644 index 0000000..4af4ff1 --- /dev/null +++ b/samples/invoke-http-endpoint-python/requirements.txt @@ -0,0 +1,3 @@ +fdk>=0.1.60 +oci>=2.2.18 +requests \ No newline at end of file