Skip to main content

Simple AWS Lambda proxy to handle API Gateway request

Project description

https://badge.fury.io/py/lambda-proxy.svg https://circleci.com/gh/vincentsarago/lambda-proxy.svg?style=svg https://codecov.io/gh/vincentsarago/lambda-proxy/branch/master/graph/badge.svg

A simple proxy that can work on AWS Lambda as API-gateway proxy

Install

$ pip install -U pip
$ pip install lambda-proxy

Or install from source:

$ git clone https://github.com/vincentsarag/lambda-proxy.git
$ cd lambda-proxy
$ pip install -U pip
$ pip install -e .

Usage

With GET request

>>> from lambda_proxy.proxy import API
>>> APP = API(app_name="app")

>>> @APP.route('/test/tests/<id>', methods=['GET'], cors=True)
>>> def print_id(id):
        return ('OK', 'plain/text', id)

With POST request

>>> from lambda_proxy.proxy import API
>>> APP = API(app_name="app")

>>> @APP.route('/test/tests/<id>', methods=['POST'], cors=True)
>>> def print_id(id, body):
        return ('OK', 'plain/text', id)

Binary responses

When working with binary on API-Gateway we must return a base64 encoded string

>>> from lambda_proxy.proxy import API
>>> APP = API(app_name="app")

>>> @APP.route('/test/tests/<filename>.jpg', methods=['GET'], cors=True, binary_b64encode=True)
>>> def print_id(filename):
        with open(f"{filename}.jpg", "rb") as f:
            return ('OK', 'image/jpeg', f.read())

Enable compression (happens only if “Accept-Encoding” if found in headers)

>>> from lambda_proxy.proxy import API
>>> APP = API(app_name="app")

>>> @APP.route('/test/tests/<filename>.jpg', methods=['GET'], cors=True, binary_b64encode=True, payload_compression_method="gzip")
>>> def print_id(filename):
        with open(f"{filename}.jpg", "rb") as f:
            return ('OK', 'image/jpeg', f.read())

Simple Auth token

Lambda-proxy provide a simple token validation system.

  • a “TOKEN” variable must be set in the environment

  • each request must provide a “access_token” params (e.g curl http://myurl/test/tests/myid?access_token=blabla)

>>> from lambda_proxy.proxy import API
>>> APP = API(app_name="app")

>>> @APP.route('/test/tests/<id>', methods=['GET'], cors=True, token=True)
>>> def print_id(id):
        return ('OK', 'plain/text', id)

URL schema and request parameters

QueryString parameters are passed as function’s options.

>>> from lambda_proxy.proxy import API
>>> APP = API(app_name="app")

>>> @APP.route('/<id>', methods=['GET'], cors=True)
>>> def print_id(id, name=None):
        return ('OK', 'plain/text', f"{id}{name}")

requests:

>>> curl /000001
0001

>>> curl /000001?name=vincent
0001vincent

With multiple routes

>>> from lambda_proxy.proxy import API
>>> APP = API(app_name="app")

>>> @APP.route('/<id>', methods=['GET'], cors=True)
>>> @APP.route('/<id>/<int:number>', methods=['GET'], cors=True)
>>> def print_id(id, number=None, name=None):
        return ('OK', 'plain/text', f"{id}-{name}-{number}")

requests:

>>> curl /000001
0001--

>>> curl /000001?name=vincent
0001-vincent-

>>> curl /000001/1?name=vincent
0001-vincent-1

Context and Event passing

Pass event and context to the handler function.

>>> from lambda_proxy.proxy import API
>>> APP = API(app_name="app")

>>> @APP.route("/<id>", methods=["GET"], cors=True)
>>> @APP.pass_event
>>> @APP.pass_context
>>> def print_id(ctx, evt, id):
    print(ctx)
    print(evt)
    return ('OK', 'plain/text', f"{id}")

Examples

Contribution & Devellopement

Issues and pull requests are more than welcome.

Dev install & Pull-Request

$ git clone https://github.com/vincentsarago/lambda-proxy.git
$ cd lambda-proxy
$ pip install -e .[dev]

Python >3.6 only

This repo is set to use pre-commit to run flake8, pydocstring and

black (“uncompromising Python code formatter”) when committing new code.

$ pre-commit install
$ git add .
$ git commit -m'my change'
black....................................................................Passed
Flake8...................................................................Passed
Verifying PEP257 Compliance..............................................Passed
$ git push origin

License

See LICENSE.txt.

Authors

See AUTHORS.txt.

Changes

See CHANGES.txt.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lambda_proxy-3.0.0.tar.gz (7.7 kB view details)

Uploaded Source

File details

Details for the file lambda_proxy-3.0.0.tar.gz.

File metadata

  • Download URL: lambda_proxy-3.0.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for lambda_proxy-3.0.0.tar.gz
Algorithm Hash digest
SHA256 73f86178cfdebe4b9c82b11ffe9d81239168873884482005ee1592e8a7200650
MD5 922b48608de7ed16fa61f831f9cb576f
BLAKE2b-256 8ef50afd9555a7343a6e65a2fad941b9cac84bb72729edd7f7edba2b385238ac

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page