Skip to main content

Common library used alongside jsexton-portfolio chalice applications.

Project description

Pyocle

Socle - A plain low block or plinth serving as a support for a column, urn, statue, etc. or as the foundation of a wall.

Common library used alongside jsexton-portfolio chalice applications.

Responses

Portfolio APIs have a standard response structure they must follow. This library contains models and response builders to help remain consistent with these defined standards response models.

Models

Response Body

{
  "success": true,
  "meta": { },
  "data": { }
}

Response Meta Field

{
  "message": "Message depending on the type of response",
  "errorDetails": [],
  "schemas": {}
}

Building Responses

import pyocle

ok_response = pyocle.response.ok({'some': 'data'})
created_response = pyocle.response.created({'some': 'data'})

# In most cases, the error handler will handle these responses for you if you defined
# the pydantic models correctly and you are using form.resolve_form for all incoming data.
bad_response = pyocle.response.bad(error_details=[], schema={})

internal_error_response = pyocle.response.internal_error()

Serialization Helpers

Camel Case Property Naming Convention

It is a portfolio API standard that all field names should be camel case when serialized to the response body. Pyocle offers a mixin to assist in this conversion.

from pyocle.serialization import CamelCaseAttributesMixin

class MyResponse(CamelCaseAttributesMixin):
    def __init__(self):
        self.snake_case_attribute = 'snake_case_attribute'

When using jsonpickle or any built in pyocle response builders, the resulting json will contain camel cased attrbiute names.

Error Handling

Pyocle comes with an error_handler decorator that can be used to decorate all endpoints that require error handling.

from chalice import Chalice
import pyocle

app = Chalice(app_name='my-portfolio-service')

app.route('/')
@pyocle.error.error_handler
def some_portfolio_endpoint():
    pass

Resolving Forms

When resolving forms or incoming data with pyocle, use the resolve_form function. The function accepts the incoming data and a form object that inherits from pydantic's BaseModel to match against. If the incoming data complies with the specified form, the form object is returned. Make sure the form is a subclass of BaseModel, if not, an exception will be raised. Otherwise an exception is raised with information detailing what went wrong. These exceptions normally work very closely with pyocle's error_handler.

from chalice import Chalice
from pydantic import BaseModel
import pyocle

app = Chalice(app_name='my-portfolio-service')

class SomeForm(BaseModel):
    test_data: str

app.route('/')
@pyocle.error.error_handler
def some_portfolio_endpoint():
    incoming_data = app.current_request.raw_body
    form = pyocle.form.resolve_form(incoming_data, SomeForm)

    ...

Common Services

Pyocle comes with a few common services used through out portfolio services out of the box.

Key Management Service

The KeyManagementService is used to interface with AWS KMS for encrypting and decrypting information. Most common use case is decrypting connection strings for databases.

import pyocle

kms = pyocle.service.KeyManagementService()
kms_response = kms.decrypt('some cipher text')

Configuration

Environment Variables

In order to safely retrieve an environment variable, make use of the env_var() function. A default value can be given and will be used if the given environment variable could not be found.

import pyocle

environment_variable = pyocle.config.env_var('some_env_var_name', default='found')

Encrypted Environment Variables

Sometimes environment variables are encrypted. Use the encrypted_env_var() function to retrieve these values in their plain text forms.

import pyocle

decrypted_environment_variable = pyocle.config.encrypted_env_var('some_env_var_name')

By default the function makes use of a kms decrypter. To specify a custom decrypter simply pass the decryption function as a decrypter and any additional values that may be need to decrypt to attrs

import pyocle

additional_info = {
    'password': 'password123'
}

def my_decrypter(value, **kwargs) -> str:
    """decrypt and return"""

decrypted_environment_variable = pyocle.config.encrypted_env_var('some_env_var_name', decrypter=my_decrypter, attrs=additional_info)

Connection Strings

Connection strings should be encrypted with KMS and stored in the correct chalice stage environment variables as 'CONNECTION_STING'. When retrieving these values, make use of the connection_string() function. connection_string() will retrieve the environment connection string and decrypt using KMS while only returning the actual usable connection string.

import pyocle

connection_string = pyocle.config.connection_string()

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

pyocle-0.3.1.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyocle-0.3.1-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file pyocle-0.3.1.tar.gz.

File metadata

  • Download URL: pyocle-0.3.1.tar.gz
  • Upload date:
  • Size: 11.0 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.47.0 CPython/3.8.2

File hashes

Hashes for pyocle-0.3.1.tar.gz
Algorithm Hash digest
SHA256 c2f8be0ee147948826ffe8898dadcaaff86a4dc732a47fa4288e34268aa3e2e6
MD5 e2cc7c486eb405fca9303a9f53198bdd
BLAKE2b-256 ecd49ca6f302adc8d445a8de58cc84f3715920d73b21558c5c87acb7d21255f4

See more details on using hashes here.

File details

Details for the file pyocle-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: pyocle-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 11.1 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.47.0 CPython/3.8.2

File hashes

Hashes for pyocle-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8af03a02a2d6ca24f42386a5f21716964fd20b79f46835c522ca8913feb589a7
MD5 d5a97b42c55976c69f6ff10836fb97c2
BLAKE2b-256 fd81fc0147d355190323bd6e0b0e3f75d6c94c09b90b5700bfccace2c1b8d621

See more details on using hashes here.

Supported by

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