Calling FunctionGraph using curl and bash¶
This section demonstrates how to call a FunctionGraph implemented in python using curl.
For details on API calls, see:
Prerequisites¶
Environment Variables¶
See environment variables section in: Prerequisites
Deployed FunctionGraph Event Function¶
Deploy the function manually as described in Prerequisites using the OpenTelekomCloud Console.
Getting Token from Username and Password¶
To get a token for authentication from Username and Password, you can use the provided bash script:
utils/tokenFromUsername.sh¶
#!/bin/bash
# Script to get an authentication token
# from OTC IAM using username and password
# to be passed as x-auth-token header in API requests.
# Script outputs the token to
# - stdout
# - and to the environment variable OTC_X_AUTH_TOKEN (if called using current shell "source ...")
# see: https://docs.otc.t-systems.com/identity-access-management/api-ref/calling_apis/authentication.html#iam-02-0510
# see: https://docs.otc.t-systems.com/identity-access-management/api-ref/apis/token_management/obtaining_a_user_token_through_password_authentication.html
# Following environment variables must be set:
# OTC_USER_NAME
# OTC_USER_PASSWORD
# OTC_DOMAIN_NAME
# OTC_SDK_PROJECTNAME
# OTC_SDK_PROJECTID
# OTC_IAM_ENDPOINT e.g. https://iam.eu-de.otc.t-systems.com/v3
# If DEBUG is not set in the environment, it defaults to 0 (off)
DEBUG=${DEBUG:-0}
payload=$(cat <<EOF
{
"auth": {
"identity": {
"methods": [
"password"
],
"password": {
"user": {
"name": "${OTC_USER_NAME}",
"password": "${OTC_USER_PASSWORD}",
"domain": { "name": "${OTC_DOMAIN_NAME}" }
}
}
},
"scope": {
"project": {
"id": "${OTC_SDK_PROJECTID}",
"domain": { "name": "${OTC_DOMAIN_NAME}" }
}
}
}
}
EOF
)
if [ "$DEBUG" -eq 1 ]; then
# for debugging, print the payload to stderr
echo "################# Payload for token request: #################" >&2
echo ${payload} >&2
echo "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" >&2
fi
# Make the API call
# and extract the X-Subject-Token from the response headers
token=$(curl -i -s \
-H 'Content-Type: application/json' \
-d "${payload}" \
-o /dev/null \
--dump-header /dev/stdout \
${OTC_IAM_ENDPOINT}/auth/tokens?nocatalog=true \
| grep -i ^X-Subject-Token: | cut -d' ' -f2)
if [ "$DEBUG" -eq 1 ]; then
# for debugging, print the token to stderr
echo "################# Token #################" >&2
echo "${token}" >&2
echo "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" >&2
fi
# export the token to environment variable OTC_X_AUTH_TOKEN
export OTC_X_AUTH_TOKEN=${token}
# print the token to stdout
echo ${token}
This file needs execution permissions, e.g. set using:
chmod +x tokenFromUsername.sh
Use the script as follows to get the token and store it in a variable:
Option 1: Using subshell to get token into variable¶
# execute the script to get the token without output to stdout
OTC_X_AUTH_TOKEN=$(./tokenFromUsername.sh) > /dev/null
# Output will be like:
echo $OTC_X_AUTH_TOKEN
# Output example:
MIIGBQYJKoZIhvcNAQcCoIIF9jCCBfICAQExDTALBglghkgBZQMEAgEwggOKBgkqhkiG.....
Option 2: Sourcing the script to get token into environment variable¶
# source the script to get the token into environment variable OTC_X_AUTH_TOKEN
source ./tokenFromUsername.sh > /dev/null
# Output will be like:
echo $OTC_X_AUTH_TOKEN
# Output example:
MIIGBQYJKoZIhvcNAQcCoIIF9jCCBfICAQExDTALBglghkgBZQMEAgEwggOKBgkqhkiG.....
Calling Functiongraph using Username and Password synchronously¶
See Executing a Function Synchronously
export MY_FUNCTION_NAME="DefaultPython3_10_From_Go_SDK"
export MY_FUNCTION_URN="urn:fss:${OTC_SDK_REGION}:${OTC_SDK_PROJECTID}:function:default:${MY_FUNCTION_NAME}:latest"
# execute curl
curl -X POST \
-H "Content-Type: application/json" \
-H "x-auth-token: ${OTC_X_AUTH_TOKEN}" \
-d '{"key":"Hello World of FunctionGraph"}' \
https://functiongraph.${OTC_SDK_REGION}.otc.t-systems.com/v2/${OTC_SDK_PROJECTID}/fgs/functions/${MY_FUNCTION_URN}/invocations
# Output will be like:
{"statusCode": 200, "isBase64Encoded": false, "body": "{\"key\": \"Hello World of FunctionGraph\"}", "headers": {"Content-Type": "application/json"}}
Calling Functiongraph using Username and Password asynchronously¶
See Executing a Function Asynchronously
export MY_FUNCTION_NAME="DefaultPython3_10_From_Go_SDK"
export MY_FUNCTION_URN="urn:fss:${OTC_SDK_REGION}:${OTC_SDK_PROJECTID}:function:default:${MY_FUNCTION_NAME}:latest"
# execute curl
curl -X POST \
-H "Content-Type: application/json" \
-H "x-auth-token: ${OTC_X_AUTH_TOKEN}" \
-d '{"key":"Hello World of FunctionGraph"}' \
https://functiongraph.${OTC_SDK_REGION}.otc.t-systems.com/v2/${OTC_SDK_PROJECTID}/fgs/functions/${MY_FUNCTION_URN}/invocations-async
# Output will be like:
{"request_id": "c1db74a0-35a7-4717-b1db-1f66380d68d8"}