azure-functions-parser
A lightweight decorator that adds FastAPI-like request parsing and validation to Azure Functions HTTP triggers.
Features
- Automatic parsing and validation of query parameters
- Automatic parsing and validation of JSON request bodies using Pydantic models
- Automatic response serialization for different return types (dict, Pydantic models, strings, etc.)
- Zero configuration required - just add the decorator
- Full type hints and async support
Installation
pip install azure-functions-parser
Usage
import azure.functions as func
from azure.functions_parser import validate_request
@validate_request
def main(req: func.HttpRequest, some_query_param: int = 1):
...
Quick Start
You have an API endpoint that takes user data in the request body, and an optional query parameter.
Before
import azure.functions as func
import json
def main(req: func.HttpRequest) -> func.HttpResponse:
body = req.get_json()
try:
name = body["name"]
age = int(body["age"])
format = req.params.get("format", "json")
if format not in ["json", "csv"]:
raise ValueError(f"Invalid argument for 'format': {format}")
except Exception as e:
return func.HttpResponse(str(e), status_code=400)
return func.HttpResponse(
json.dumps({"name": name, "age": age, "format": format}),
status_code=200
)
After
from typing import Literal
from pydantic import BaseModel
import azure.functions as func
from azure.functions_parser import validate_request
class User(BaseModel):
name: str
age: int
@validate_request
def main(req: func.HttpRequest, user: User, format: Literal["json", "csv"] = "json"):
return {"name": user.name, "age": user.age, "format": format}
The request body, User, and query parameter, format, will be parsed and
validated for incoming requests. If there's a validation error, a 400 response
will be issued automatically, with descriptive errors.
Suppose you get an incoming request with the following data:
- body:
{"age": 25.5}('name' is missing, and 'age' is a float) - params:
?format=xml(should be 'json' or 'csv')
The following error will be sent back to the client with a 400 code.
{
"errors": [
{
"param": "format",
"reason": "Input should be 'json' or 'csv'",
"type": "literal_error",
"input": "xml"
},
{
"param": "name",
"reason": "Field required",
"type": "missing"
},
{
"param": "age",
"reason": "Input should be a valid integer, got a number with a fractional part",
"type": "int_from_float",
"input": 25.5
}
]
}
Usage Guide
Query Parameters
Query parameters are automatically parsed based on your function's parameters:
@validate_request
def handler(req, name: str, age: int = 18, format: str = "json"):
return {"name": name, "age": age, "format": format}
This will:
- Require
nameas a required query parameter - Accept an optional
ageparameter that defaults to 18 - Accept an optional
formatparameter that defaults to "json" - Automatically convert parameters to the correct type
- Return a 400 error with validation details if parameters are invalid or missing
Request Body
Use a Pydantic model to parse and validate the JSON request body. Your function may have at most one, and only one, parameter annotated as a Pydantic model.
from pydantic import BaseModel, EmailStr
class UserProfile(BaseModel):
name: str
age: int
email: EmailStr
interests: list[str] | None = None
@validate_request
def create_user(req: func.HttpRequest, user: UserProfile):
return {"name": user.name, "age": user.age}
Response Handling
The decorator automatically handles different return types.
Any return value that is not a func.HttpResponse will be wrapped in a 200 OK
response, with your return value as the response body.
@validate_request
def handler1(req: func.HttpRequest):
return {"message": "OK"} # Returns JSON response
@validate_request
def handler2(req: func.HttpRequest):
return "Hello, World!" # Returns plain text response
@validate_request
def handler3(req: func.HttpRequest):
return UserProfile(...) # Returns JSON-serialized Pydantic model
@validate_request
def handler4(req: func.HttpRequest):
return func.HttpResponse(...) # Returns custom HttpResponse directly
Error Handling
Invalid requests return detailed 400 responses:
{
"errors": [
{
"param": "format",
"reason": "Input should be 'json' or 'csv'",
"type": "literal_error",
"input": "xml"
},
{
"param": "name",
"reason": "Field required",
"type": "missing"
},
{
"param": "age",
"reason": "Input should be a valid integer, got a number with a fractional part",
"type": "int_from_float",
"input": 25.5
}
]
}
Async Support
The decorator works with both sync and async handlers:
@validate_request
async def handler(req: func.HttpRequest, user: UserProfile):
# ... async operations ...
return {"status": "success"}
Contributing
Contributions are welcome!
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for azure-functions-parser 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| azure_functions_parser-0.0.1.tar.gz | 8.9 kB | Without provenance | Details for azure_functions_parser-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| azure_functions_parser-0.0.1-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for azure_functions_parser-0.0.1-py2.py3-none-any.whl |
Total release size: 15.8 kB
Release files / azure_functions_parser-0.0.1.tar.gz
| Download URL | azure_functions_parser-0.0.1.tar.gz |
|---|---|
| Size | 8.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
667e19baeef84a49a1ad265f2ea2b1a2a0f3b3961febf6c5482d82d5ad35efe8
|
|
BLAKE2b-256 checksum How to use checksums |
d97a5983db95344dd51ce13ece8c0bdedf9016fca8f934a9cfc7ae1ec02ae4a2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Release files / azure_functions_parser-0.0.1-py2.py3-none-any.whl
| Download URL | azure_functions_parser-0.0.1-py2.py3-none-any.whl |
|---|---|
| Size | 6.9 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
a04f2dbce6f3af5b53ef65c69c1b1d0140b1b8ced9ccfcf3165618636c720a7e
|
|
BLAKE2b-256 checksum How to use checksums |
6298cac67143cb374ed51e10e25926b8fd51803ef700bee422ec81b2450ecad9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Security for azure-functions-parser 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.