Skip to main content

Ophis Pteretos

CodeQL Python package codecov

This is common Python bindings for AWS based applications. The name "Ophis Pteretos" is Greek for flying serpent, since... this library is intended for the cloud. Supported in this library are helpers for

  • AWS Gateway routing
  • DynamoDB repositories

Using ophis-py, the heavy lifting for spinning up any sized project behind an API Gateway frontend is reduced to a few lines of code.

API Gateway Routing

The core of the API Gateway router is found in the Router class. In typical applications, you would put this in your module __init__.py:

from ophis.router import Router


api = Router()

Now defining your route is as easy as:

@api.route('/greet')
def greet():
    return {
        'message': 'Hi, person'
    }

Adding path param evaluation:

@api.route('/greet/{name}')
def greet(name):
    return {
        'message': f'Hi, {name}'
    }

Context

The library makes heavy use of contextvars for contextual evaluation. Some default ones for request, response, and app_context exist for reading context aware request and setting response data as needed. The app_context is simply a container to evaluate dependencies to routes.

from ophis.globals import request, response, app_context

app_context.inject('thing_data', ThingDatabase())

@api.route('/things')
def list_things(thing_data):
    limit = int(request.queryparams.get('limit', '100'))
    next_token = request.queryparams.get('nextToken', None)
    if 0 <= limit > 100:
        response.status_code = 400
        return {
            'message': 'The limit cannot be less than 1 or greater than 100.'
        }
    resp = thing_data.query(
        request.account_id(),
        params=QueryParams(limit=limit, next_token=next_token))
    return {
        'items': resp.items,
        'next_token': resp.next_token
    }

DynamoDB

The library allows a complete integration with DynamoDB using the database module. The library does much of the heavy lifting, making an assumption around CRUD (create, read, update, and delete) operations. A repository allows for adjacency list building as well as multiple tables. An example one for Things might look like:

from ophis.database import Repository

class ThingDatabase(Repository):
    def __init__(self, table=None):
        super.__init__(self, table=table, type="Things", fields_to_keys={
            'thingName': 'SK'
        })

With the above definition, the following operations can be performed:

import boto3
from ophis.database import QueryParams

ddb = boto3.resource('dynamodb')
table = ddb.Table('MyTableName')

thing_data = ThingDatabase(table=table)

# create, hashed by an AWS account ID for example
thing1 = thing_data.create('hashid', item={'thingName': 'MyThing1'})

# throws conflict
thing_data.create('hashid', item={'thingName': 'MyThing1'})

# update
thing_data.update('hashid', item={**thing1, 'newField': 'Updated'})

# read
thing_data.get('hashid', item_id=thing1['thingName'])

results = thing_data.list('hashid', query=QueryParams(limit=10))
for item in results.items:
    print(item)

results.next_token # can be used for pagination

# delete
thing_data.delete('hashid', item_id=thing1['thingName'])

Note that the pagination token is aes/gcm encrypted based on the last evaluated token and the contents on the query call (for a multi-tenant application, this would include the AWS account ID). This means that pagination tokens can be transmitted safely as query parameters to read operations, by design.

Metadata

Release files for ophis-py 0.1.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 ophis-py 0.1.0
File Size Uploaded
ophis-py-0.1.0.tar.gz 17.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ophis-py 0.1.0
File Interpreter ABI Platform
ophis_py-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 33.6 kB

Release files / ophis-py-0.1.0.tar.gz

Download URL ophis-py-0.1.0.tar.gz
Size 17.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f3790444e07f7a936ed9df14cba0d46bc757c1ff0d957aecc3d8dc825bcaa4da
BLAKE2b-256 checksum
How to use checksums
b479a44666ae50c4d0441bd7246b6538e48185558a9a3e97f750d230e44dbc82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.19

Release files / ophis_py-0.1.0-py3-none-any.whl

Download URL ophis_py-0.1.0-py3-none-any.whl
Size 15.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8fdf077f09d01224f88f71e87408a8bc3fcb9a6e6e8cbb1a706eff3a53c82373
BLAKE2b-256 checksum
How to use checksums
3954aef2d79f5c284db1fbd75b1eeadd266106c1827d6928ad23f17fed51d058
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.19

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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