Skip to main content

functionapprest

Build Status Latest Version Python Support

Python routing mini-framework for MS Azure Functions with optional JSON-schema validation.

** This repository is based heavily on lambdarest project

Features

  • functionapp_handler function constructor with built-in dispatcher
  • Decorator to register functions to handle HTTP methods
  • Optional JSON-schema input validation using same decorator

Installation

Install the package from PyPI using pip:

pip install functionapprest

Getting Started

This module helps you to handle different HTTP methods in your Azure Functions.

from functionapprest import functionapp_handler

@functionapp_handler.handle('get')
def my_own_get(event):
    return {'this': 'will be json dumped'}

Advanced Usage

Optionally you can validate an incoming JSON body against a JSON schema:

my_schema = {
    '$schema': 'http://json-schema.org/draft-04/schema#',
    'type': 'object',
    'properties': {
        'body':{
            'type': 'object',
            'properties': {
                'foo': {
                    'type': 'string'
                }
            }
        }
    }
}

@functionapp_handler.handle('get', path='/with-schema/', schema=my_schema)
def my_own_get(event):
    return {'this': 'will be json dumped'}

Query Params

Query params are also analyzed and validate with JSON schemas. Query arrays are expected to be comma separated, all numbers are converted to floats.

my_schema = {
    '$schema': 'http://json-schema.org/draft-04/schema#',
    'type': 'object',
    'properties': {
        'query':{
            'type': 'object',
            'properties': {
                'foo': {
                    'type': 'array',
                    'items': {
                        'type': 'number'
                    }
                }
            }
        }
    }
}

@functionapp_handler.handle('get', path='/with-params/', schema=my_schema)
def my_own_get(event):
    return event.json['query']

Routing

You can also specify which path to react on for individual handlers using the path param:

@functionapp_handler.handle('get', path='/foo/bar/baz')
def my_own_get(event):
    return {'this': 'will be json dumped'}

And you can specify path parameters as well, which will be passed as keyword arguments:

@functionapp_handler.handle('get', path='/foo/<int:id>/')
def my_own_get(event, id):
    return {'my-id': id}

Or use the proxy endpoint:

@functionapp_handler.handle('get', path='/bar/<path:path>')
def my_own_get(event, path):
    return {'path': path}

Using within Function App

function.json

{
  "scriptFile": "handler.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get"
      ],
      "route": "products/{product_id}"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}

handler.py

from functionapprest import functionapp_handler, Request

@functionapp_handler.handle('get', path='/products/<path:product_id>/')
def list_products(req: Request, product_id):
    query = req.json.get('query', {})
    body = req.json.get('body', {})
    headers = req.get('headers', {})
    params = req.get('params', {})
    response = {
        'method': req.method,
        'url': req.url,
        'headers': headers,
        'params': params,
        'query': query,
        'body': body
    }
    return response

main = functionapp_handler

Tests

You can use pytest to run tests against your current Python version. To run tests for current python version run pytest

See setup.py for test dependencies and install them with pipenv install --dev.

Contributors

eduardomourar

Metadata

Release files for functionapprest 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for functionapprest 0.2.0
File Size Uploaded Provenance Actions
functionapprest-0.2.0.tar.gz 9.9 kB Without provenance Details for functionapprest-0.2.0.tar.gz

Release files / functionapprest-0.2.0.tar.gz

Download URL functionapprest-0.2.0.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
67133dd34a248cb23afa4dc8fd5b690ae021810b23f3b02c2fc1d93c1618086a
BLAKE2b-256 checksum
How to use checksums
af203ffd89d5145dc11e0950062824cefd823ff826e90d05a025764819a96604
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

Security for functionapprest 0.2.0

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page