Skip to content

Commit eab25d4

Browse files
Merge pull request #1 from gargnipungarg/http-endpoint-python
Invoking HTTP endpoint via functions.
2 parents 6eec924 + 72c26a2 commit eab25d4

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

Diff for: samples/invoke-http-endpoint-python/Readme.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Build using fn cli
2+
```bash
3+
fn -v deploy --app <app-name>
4+
```
5+
6+
# oci-cli based function invocation
7+
```bash
8+
oci fn function invoke --function-id <function-ocid> --file "-" --body '{"ENDPOINT":"<predict-url>", "PAYLOAD": "<json-payload>"}'
9+
```
10+
11+
## Sample:
12+
```bash
13+
oci fn function invoke --function-id <function-ocid> --file "-" --body '{"ENDPOINT":"https://modeldeployment.us-ashburn-1.oci.customer-oci.com/<md-ocid>/predict", "PAYLOAD": "{\"index\": \"1\"}"}'
14+
```
15+
16+
# fn cli based invocation
17+
```bash
18+
fn invoke <app-name> <function-name>
19+
```
20+
21+
## Sample:
22+
```bash
23+
echo -n '{"ENDPOINT":"https://modeldeployment.us-ashburn-1.oci.customer-oci.com/<md-ocid>/predict", "PAYLOAD": "{\"index\": \"1\"}"}' | fn invoke <app-name> <function-name>
24+
```
25+
26+
# More information
27+
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.
28+
Other ways of function invocation can be found [here](https://docs.oracle.com/en-us/iaas/Content/Functions/Tasks/functionsinvokingfunctions.htm)

Diff for: samples/invoke-http-endpoint-python/func.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import io
2+
import json
3+
import logging
4+
import oci
5+
import requests
6+
from fdk import response
7+
8+
9+
def handler(ctx, data: io.BytesIO = None):
10+
auth = oci.auth.signers.get_resource_principals_signer()
11+
logger = logging.getLogger()
12+
try:
13+
logger.info("Inside function")
14+
body = json.loads(data.getvalue())
15+
logger.info("Body : " + json.dumps(body))
16+
headers = ctx.Headers()
17+
logger.info("Headers: " + json.dumps(headers))
18+
endpoint = body.get("ENDPOINT")
19+
payload = body.get("PAYLOAD")
20+
resp = requests.post(endpoint, json=json.loads(payload), auth=auth)
21+
logger.info("response : " + resp.json())
22+
except (Exception, ValueError) as ex:
23+
logger.error("Failed to call endpoint with ex : {}".format(str(ex)))
24+
return response.Response(
25+
ctx, response_data=resp.json(),
26+
headers={"Content-Type": "application/json"}
27+
)

Diff for: samples/invoke-http-endpoint-python/func.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
schema_version: 20180708
2+
name: api-gw
3+
version: 1.0.26
4+
runtime: python
5+
build_image: fnproject/python:3.9-dev
6+
run_image: fnproject/python:3.9
7+
entrypoint: /python/bin/fdk /function/func.py handler
8+
memory: 256
9+
timeout: 40

Diff for: samples/invoke-http-endpoint-python/requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fdk>=0.1.60
2+
oci>=2.2.18
3+
requests

0 commit comments

Comments
 (0)