I’ve been using OCI Functions for a few month now, having previously used Azure Functions extensively one thing that I did miss was the ability to take values from the query string of a request to a function and pass these directly to the function, rather than including any input values that the function requires within the body of the request and submitting as a POST βοΈ.
As a side note, I did a short video that walks through the process of creating an OCI Function, which can be found here.
For example, I’d previosuly written an Azure function during the pandemic to create a workout for me, with this function I could pass the number of exercises that I wanted in my workout to the function as part of the query string and it would return a workout with the number of exercises requested ποΈ.
The query string, could for example be ?exercises=10 to create a workout with 10 exercises (as you can see below the exercises themselves are defined directly in the function itself).
Example Azure Function code (Python)
import logging
import random
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
exercises = req.params.get('exercises')
if not exercises:
try:
req_body = req.get_json()
except ValueError:
pass
else:
exercises = req_body.get('exercises')
if exercises:
exerciselist = ['50 Star Jumps','20 Crunches','30 Squats','50 Press Ups','1 Min Wall Sit','10 Burpees','20 Arm Circles',\
'20 Squats','30 Star Jumps','15 Crunches','10 Press Ups','2 Min Wall Sit','20 Burpees','40 Star Jumps','25 Burpees',\
'15 Arm Circles','30 Crunches','15 Press Ups','30 Burpees','15 Squats','30 Sec Arm Circles','2 Min Wall Sit','20 Burpees',\
'60 Star Jumps','10 Crunches','25 Press Ups'
]
workout = []
for i in range(0,int(exercises)):
randomnumber = random.randint(0,(len(exerciselist)-1))
workout.append(exerciselist[randomnumber])
return func.HttpResponse(str(workout))
else:
return func.HttpResponse(
"Please pass the number of exercises required to the query string",
status_code=400
)
func.HttpResponse
To do the same in an OCI Function, I discovered that I could inspect the ctx.RequestURL which provides the URL that was passed to the function and then do some magic to extract the query string values from this, below is the Python code for my OCI Functions variant of my exercise generator function that does this.
The comments in the script explain how I achieved this.
import io
import random
import json
from fdk import response
def handler(ctx,data):
max = ctx.RequestURL() # Get the requested URL
max = max.split("=")[1] # Extract the value passed to the query string, this makes the presumption that there is a single query string (I know you should never presume!) and uses this for the max variable that the for loop uses
exerciselist = ['50 Star Jumps','20 Crunches','30 Squats','50 Press Ups','1 Min Wall Sit','10 Burpees','20 Arm Circles',\
'20 Squats','30 Star Jumps','15 Crunches','10 Press Ups','2 Min Wall Sit','20 Burpees','40 Star Jumps','25 Burpees',\
'15 Arm Circles','30 Crunches','15 Press Ups','30 Burpees','15 Squats','30 Sec Arm Circles','2 Min Wall Sit','20 Burpees',\
'60 Star Jumps','10 Crunches','25 Press Ups'
]
workout = []
for i in range(0,int(max)):
randomnumber = random.randint(0,(len(exerciselist)-1))
workout.append(exerciselist[randomnumber])
return response.Response(ctx, response_data=workout)
Here is the function in all it’s glory being called from a browser (published via OCI App Gateway to make it callable without auth):

The code can also be found on GitHub.

Leave a comment