Skip to main content

OpenStax Lambda@Edge utilities

Project description

late-python

Build Status

A Python package with OpenStax Lambda@Edge ("lAMBDA at eDGE") utilities.

Overview

late-python provides object overlays to the JSON containers inside Lambda@Edge events and responses. These overlay objects handle the details of accessing and modifying the containers they overlay.

All get_* methods leave the underlying container intact, and all set_* methods modify it in place.

Usage

Dealing With Request Events

from oxlate import Event, Response

def lambda_handler(event, context):
    request = Event(event).request() ## returns Request overlay

    ## get and set the request uri field
    request.get_uri()
    request.set_uri('/some/new/uri')

    ## get the viewer's country code
    request.get_viewer_country()

    ## return Headers overlay
    headers = request.get_headers()

    ## get the Request event header array-of-hash (or None if missing):
    ##    [{name=..., value=...}]
    headers.get(name='header-name')

    ## get the header value only
    ## (or None if missing and no default given)
    headers.get_value(name='header-name', default='some default')

    ## set or overwrite a header
    headers.set(
        name  = 'header-name',
        value = 'some value',
    )

    ## get a request cookie value
    ## (or None if missing and no default given)
    headers.get_request_cookie('cookie_name', default='some default')

    ## set or overwrite a request cookie value
    headers.set_request_cookie('cookie_name', value='some value')

    ## return a deep copy of the overlaid hashmap
    return request.to_dict()

Dealing With Response Objects

from oxlate import Response

def request_handler(event, context):
    ## construct a Response object
    response = Response(
        status=302,
        content_type='text/plain',
        body=None,
    )

    ## use method chaining to tweak Response
    response.set_status(404) \
            .set_content_type('custom/type') \
            .set_content_type_json() \
            .set_content_type_html() \
            .set_body(json.dumps('some body text'))

    ## get a Headers overlay
    headers = response.get_headers()

    ## get the header value only
    ## (or None if missing and no default given)
    headers.get_value(name='header-name', default='some default')

    ## set or overwrite a header
    headers.set(
        name  = 'header-name',
        value = 'some value',
    )

    ## get a ResponseCookie overlay
    cookie = headers.get_response_cookie('cookie-name')
    print(cookie.name())
    print(cookie.value())
    print(cookie.expires_at())
    print(cookie.path())
    print(cookie.domain())

    ## set or override a ResponseCookie
    cookie = ResponseCookie(
        name       = 'cookie-name',
        value      = 'some value',
        expires_at = datetime.datetime.utcnow() + datetime.timedelta(days=2),
        path       = '/',
        domain     = 'my.domain.com',
    )
    headers.set_response_cookie(cookie)

    return response.to_dict()

Development

All development is done inside a docker container. From your host running Docker, in this directory run:

$> docker-compose up -d
%> ./docker/bash

This will drop you into the running container

Run tests

From within the container, you can run tests with:

$ /code> python -m pytest

For debugging, you can use ipdb, e.g.

import ipdb; ipdb.set_trace()

When running tests with the debugger make sure to use the -s option to prevent pytest from capturing output.

$> python -m pytest -s tests -k 'test_decrypts'

Note that pytest is also on the PATH so you can call it directly.

Distributing

...

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

oxlate-0.0.1.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

oxlate-0.0.1-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file oxlate-0.0.1.tar.gz.

File metadata

  • Download URL: oxlate-0.0.1.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.7

File hashes

Hashes for oxlate-0.0.1.tar.gz
Algorithm Hash digest
SHA256 1f3668c533cc3a465d70497cf9a574825d74a3b5ff9f1389ef2c3eed2aedd62b
MD5 92bf0418ffd0247568627aa705508e9a
BLAKE2b-256 f8aba55c3f8e21d94bc56bd12028d07ff2f3ea479095aacb34efd3d7266852e6

See more details on using hashes here.

File details

Details for the file oxlate-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: oxlate-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.7

File hashes

Hashes for oxlate-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4e79dda16cfa99eff87d843933990de43a6e7f0480e506296aea3edf331b61c9
MD5 6ab6b708f238743866de2a6690c46ac0
BLAKE2b-256 c6145a73ead92ea6ad779a70cbc14b6edd83ff99419b4d3089a32e31ff0b8f15

See more details on using hashes here.

Supported by

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