Skip to main content

sfunc

PyPI version hawry codecov

Single lambda function for AWS Api Gateway

This is a very simple package which adds a few decorators to simplify the use of a single lambda function for multiple endpoints for AWS Api Gateway. It uses the operationId attribute in the OAS3 specification to identify the correct handler and then passes the event and context to the correct method.

Basic usage

import json
from sfunc import SFunc

sfunc = SFunc()


@sfunc.operation('getPetsByName')
def get_pets_by_name(event, context):
    """The methods need to have the same signature as a regular lambda handler entrypoint"""
    return {
        'statusCode': 200,
        'body': json.dumps({'names': ['fido', 'catty']})
    }


@sfunc.operation('getPetsByAge')
def get_pets_by_age(event, context):
    """This will be invoked when the endpoint with the operationId 'getPetsByAge' is invoked"""
    return {
        'statusCode': 204
    }


def lambda_handler(event, context):
    """We only need to specify one entry point and can ignore any other """
    rsp = sfunc.execute(event, context)  # rsp will contain the return value from the invoked method
    return rsp

Using sfunc classes

For request/response which doesn't require any additional and are json encoded, sfunc provides two convenience classes for request and response which can simplify the code further when it comes to parsing and getting headers and path params. The following code will result in the same outcome as the example above, except for the extra headers.

from sfunc import ApiGatewayRequest, ApiGatewayResponse, SFunc

sfunc = SFunc()


@sfunc.operation('getPetsByName')
def pets_by_name(req: ApiGatewayRequest, context) -> ApiGatewayResponse:
    _ = req.headers().get('x-request-id')
    return ApiGatewayResponse(200, {'names': ['fido', 'catty']})


@sfunc.operation('getPetsByAge')
def pets_by_age(req: ApiGatewayRequest, context) -> ApiGatewayResponse:
    rsp = ApiGatewayResponse(204)
    rsp.add_header('content-type', 'application/json')
    return rsp


def lambda_handler(event, context):
    return sfunc.sfunc_execute(ApiGatewayRequest(event), context).response()

Specifying default handlers

It might be useful during development or if some endpoints are missing the operationId attribute in the specification.

from sfunc import SFunc

sfunc = SFunc()


@sfunc.default()
def default_handler(event, context):
    return {
        'statusCode': 405
    }


def lambda_handler(event, context):
    return sfunc.execute(event, context)  # or sfunc.sfunc_execute(ApiGatewayRequest) -> ApiGatewayResponse as above

Metadata

Release files for sfunc 0.3.2

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

Source distribution (sdist)

Source distribution for sfunc 0.3.2
File Size Uploaded Provenance Actions
sfunc-0.3.2.tar.gz 5.9 kB Without provenance Details for sfunc-0.3.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sfunc 0.3.2
File Interpreter ABI Platform Actions
sfunc-0.3.2-py3-none-any.whl Python 3 none any Details for sfunc-0.3.2-py3-none-any.whl

Total release size: 14.1 kB

Release files / sfunc-0.3.2.tar.gz

Download URL sfunc-0.3.2.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8d8c342f06ef51ccdf67ef260d8f5210bc45a93d493ca92c4273af351425ac61
BLAKE2b-256 checksum
How to use checksums
4039903c114d362c0fa0c329848785a708f73cbf493c15e0e64467d279f4db89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.5

Release files / sfunc-0.3.2-py3-none-any.whl

Download URL sfunc-0.3.2-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6a3542555bf045110b09159dc002e57836235f926167c0269e2c7fc39111ae9b
BLAKE2b-256 checksum
How to use checksums
3d18a76fd19444aafc56e42a0756a92fc37f6422d6dd958e22a6c3ec15a26665
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.5

Security for sfunc 0.3.2

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