Simple Python bindings for AWS applications
Project description
Ophis Pteretos
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ophis-py-0.1.0.tar.gz
.
File metadata
- Download URL: ophis-py-0.1.0.tar.gz
- Upload date:
- Size: 17.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f3790444e07f7a936ed9df14cba0d46bc757c1ff0d957aecc3d8dc825bcaa4da |
|
MD5 | ef7524e24f484cf163a746ea4b730c3d |
|
BLAKE2b-256 | b479a44666ae50c4d0441bd7246b6538e48185558a9a3e97f750d230e44dbc82 |
File details
Details for the file ophis_py-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: ophis_py-0.1.0-py3-none-any.whl
- Upload date:
- Size: 15.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8fdf077f09d01224f88f71e87408a8bc3fcb9a6e6e8cbb1a706eff3a53c82373 |
|
MD5 | 3daf1ebaf62cb279feffb3fdfe52747f |
|
BLAKE2b-256 | 3954aef2d79f5c284db1fbd75b1eeadd266106c1827d6928ad23f17fed51d058 |