Monday, January 17, 2022

#896 OIC invoking OCI Functions (python) - Part 1 Creating the python function



OIC is part of the OCI family, a family of rich services that can be leveraged from your integrations and processes. This post shows how easy it is to create a python function in OCI Functions. 

The next post will cover leveraging this python function from OIC.

So to the simple use case - product discounts - the python function will apply these for me. I know it is a banal example but aren't we all experts at extrapolating?

Starting point is a simple python function I have created.  







So how do we expose this logic via OCI Functions?

Step 1 - Create a new Application in OCI Functions




Step 2- Click on Cloud Shell (Getting Started) and create the function


















Now I will create a python based function within this application - first step - list my current apps - 
fn list apps - there's my app - NiallCPythonApp -









Then I create a new default python function - 

fn init --runtime python niallCPythonDemo






Check out what's been generated - 






Let's look at the generated function code - 

















This is a simple helloWorld example, I will modify to include the discount functionality I mentioned at the outset - 
the business logic is simple - If the product is iBike and the quantity ordered is > 30 then 
apply a 10% discount; all other others get a 5% discount.

Here is the revised code - first I make a copy of the default function -





then I edit as follows - 




I delete func.py and then edit the yaml file to refer to the new function  - 








Step 3 - Deploy and Test

fn -v deploy --app NiallCPythonApp -












Function has been deployed - 























Now to testing this - method is as follows, when argument passing is required - 












so my cmd is as follows - 

echo -n '{"product":"iBike", "unitsOrdered":40}' | fn invoke NiallCPythonApp niallcpythondemo






Simple and succinct!
















No comments: