Not the usual Python Azure durable function first example

How many lines of text has the Wikipedia page of the Snakeysnake programming language?

Simone Rigoni
4 min readDec 16, 2023
https://learn.microsoft.com/en-us/azure/azure-functions/durable/media/durable-functions-concepts/function-chaining.png

If you are starting now with Azure durable functions you are probably looking for an example a little more complex than the one in the documentation.

Taking inspiration from this video:

https://www.youtube.com/watch?v=HZgjmb9Y_IM

I have decided to build the proposed azure durable function to count the lines in the Wikipedia page of a searched programming language.

Azure Durable Functions are an extension of Azure Functions that provides a powerful framework for building scalable and reliable serverless workflows. They are based on the Durable Task Framework (DTFx) that allows users to write long running, persistent workflows in C# using the .Net framework and simple async/await coding constructs.

For more information about the setup you can watch this video:

https://www.youtube.com/watch?v=YQtVJ7qVXDs

I will use Python v2 model which is designed to provide a more code-centric way for authoring functions through decorators.

More information about v2 model here:

https://www.youtube.com/watch?v=KARieaWBxuk

Ok now let’s code:

The requirements.txt file:

azure-functions
azure-functions-durable
requests

The function_app.py file:

import azure.functions as func
import azure.durable_functions as df
import requests


myApp = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)


# An HTTP-Triggered Function with a Durable Functions Client binding
#
# http://localhost:7071/api/orchestrators/orchestrator_function?programming_language_name=Python
# http://localhost:7071/api/orchestrators/orchestrator_function?programming_language_name=Snakeysnake
#
@myApp.route(route = "orchestrators/{functionName}")
@myApp.durable_client_input(client_name = "client")
async def http_start(req: func.HttpRequest, client):
function_name = req.route_params.get('functionName')

programming_language_name = req.params.get('programming_language_name')
if not programming_language_name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
programming_language_name = req_body.get('programming_language_name')

if programming_language_name:
payload = {"programming_language_name": programming_language_name}

instance_id = await client.start_new(function_name, None, payload)
response = client.create_check_status_response(req, instance_id)
else:
response = func.HttpResponse("Please pass a programming_language_name in the query string", status_code = 400)

return response


# Orchestrator
@myApp.orchestration_trigger(context_name = "context")
def orchestrator_function(context):
input_context = context.get_input()
programming_language_name = input_context.get('programming_language_name')

url = "https://en.wikipedia.org/wiki/{}_(programming_language)".format(programming_language_name)

message = ''
try:
data = yield context.call_activity('download_data', url)

if 'Wikipedia does not have an article with this exact name' in data:
raise Exception('Wikipedia does not have an article about {} programming language'.format(programming_language_name))
else:
pass
except Exception as e:
message = "Error: {}".format(e)
else:
count = yield context.call_activity('count', data)
message = yield context.call_activity('write_message', count)
finally:
return message


# Activity
@myApp.activity_trigger(input_name = "url")
def download_data(url: str):
response = requests.get(url)
return response.text


# Activity
@myApp.activity_trigger(input_name = "strdata")
def count(strdata: str):
count_num_newlines = strdata.count('\n')
return count_num_newlines


# Activity
@myApp.activity_trigger(input_name = "count")
def write_message(count: int):
return "Found a total of {} new lines in that Wikipedia page".format(count)

The function takes in input a programming_language_name and search its Wikipedia page. If the page is available we will count the ‘\n’ and return a message with this information.

When starting the Azure Function we will get its http endpoint:

Run Azure Function in VS code

We can use Postman to do a GET request to the function url:

http://localhost:7071/api/orchestrators/orchestrator_function?programming_language_name=Python

Postman GET request for Python

By clinking on the statusQueryGetUri link we can check the function status:

Postman statusQueryGetUri

Let’s now respond to our first question: how many lines of text has the Wikipedia page of the Snakeysnake programming language?

http://localhost:7071/api/orchestrators/orchestrator_function?programming_language_name=Snakeysnake

Postman GET request for Snakeysnake
Postman statusQueryGetUri

Unfortunately Wikipedia does not have an article about Snakeysnake programming language.

In the next story I will implement a new azure durable function to try out the Fan out/fan in pattern.

Outro

I hope the story was interesting and thank you for taking the time to read it. On my Blogspot you can find the same post in Italian. Let me know if you have any question and if you like the content that I create feel free to buy me a coffee.

--

--

Simone Rigoni
Simone Rigoni

No responses yet