wapa5pow/azure-functions-sample

★ 0Forks 0BicepGitHub ↗Compare

README

Functions code for ETL

Prerequisites

Initialize the local project

You can initialize a project from this azd template in one of these ways:

  • Use this azd init command from an empty local (root) folder:

    azd init

    Supply an environment name, such as flexquickstart when prompted. In azd, the environment is used to maintain a unique deployment context for your app.

Prepare your local environment

Add a file named local.settings.json in the root of your project with the following contents:

{
    "IsEncrypted": false,
    "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "python"
    }
}

Create a virtual environment

The way that you create your virtual environment depends on your operating system. Open the terminal, navigate to the project folder, and run these commands:

Linux/macOS/bash

python -m venv .venv
source .venv/bin/activate

Windows (Cmd)

py -m venv .venv
.venv\scripts\activate

Run your app from the terminal

  1. To start the Functions host locally, run these commands in the virtual environment:

    pip3 install -r requirements.txt
    func start
  2. From your HTTP test tool in a new terminal (or from your browser), call the HTTP GET endpoint: http://localhost:7071/api/httpget

  3. Test the HTTP POST trigger with a payload using your favorite secure HTTP test tool. This example uses the curl tool with payload data from the testdata.json project file:

curl -X POST http://localhost:7071/api/httppost \
    -H "Content-Type: application/json" \
    -d '{"name":"Developer","age":25}'
  1. When you're done, press Ctrl+C in the terminal window to stop the func.exe host process.

  2. Run deactivate to shut down the virtual environment.

Source Code

The source code for both functions is in the function_app.py code file. Azure Functions requires the use of the @azure/functions library.

This code shows an HTTP GET triggered function:

@app.route(route="httpget", methods=["GET"])
def http_get(req: func.HttpRequest) -> func.HttpResponse:
    name = req.params.get("name", "World")

    logging.info(f"Processing GET request. Name: {name}")

    return func.HttpResponse(f"Hello, {name}!")

This code shows an HTTP POST triggered function:

@app.route(route="httppost", methods=["POST"])
def http_post(req: func.HttpRequest) -> func.HttpResponse:
    try:
        req_body = req.get_json()
        name = req_body.get('name')
        age = req_body.get('age')
        
        logging.info(f"Processing POST request. Name: {name}")

        if name and isinstance(name, str) and age and isinstance(age, int):
            return func.HttpResponse(f"Hello, {name}! You are {age} years old!")
        else:
            return func.HttpResponse(
                "Please provide both 'name' and 'age' in the request body.",
                status_code=400
            )
    except ValueError:
        return func.HttpResponse(
            "Invalid JSON in request body",
            status_code=400
        )

Contributors

wapa5pow

Issues