ripozo-sqlalchemy
This package is a ripozo extension that provides a Manager that integrate SQLAlchemy with ripozo. It provides convience functions for generating resources. In particular, it focuses on creating shortcuts for CRUD type operations. It fully implements the BaseManager class that is provided in the ripozo package.
Example
This is a minimal example of creating ripozo managers with ripozo-sqlalchemy and integrating them with a resource.
First we need to setup our SQLAlchemy model.
from ripozo import apimethod, ResourceBase
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
# Setup the database with sqlalchemy
engine = create_engine('sqlite:///:memory:', echo=True)
Base = declarative_base()
# Declare your ORM model
class Person(Base):
__tablename__ = 'person'
id = Column(Integer, primary_key=True)
first_name = Column(String)
last_name = Column(String)
# Sync the models wiht the database
Base.metadata.create_all()
Now we can get to the ripozo-sqlalchemy part.
from ripozo_sqlalchemy import AlchemyManager, ScopedSessionHandler
# A session handler if responsible for getting
# And handling a session after either a successful or unsuccessful request
session_handler = ScopedSessionHandler(engine)
# This is the code that is specific to ripozo-sqlalchemy
# You give it the session, a SQLAlchemy Model, and the fields
# You wish to serialize at a minimum.
class PersonManager(AlchemyManager):
model = Person
fields = ('id', 'first_name', 'last_name')
# This is the ripozo specific part.
# This creates a resource class that can be given
# to a dispatcher (e.g. the flask-ripozo package's FlaskDispatcher)
class PersonResource(ResourceBase):
manager = PersonManager(session_handler)
pks = ['id']
namespace = '/api'
# A retrieval method that will operate on the '/api/person/<id>' route
# It retrieves the id, first_name, and last_name properties for the
# resource as identified by the url parameter id.
@apimethod(methods=['GET'])
def get_person(cls, request):
properties = cls.manager.retrieve(request.url_params)
return cls(properties=properties)
Easy Resources
Alternatively, we could use the create_resource method which will automatically create a manager and resource that corresponds to the manager.
from ripozo import restmixins
from ripozo_sqlalchemy import ScopedSessionHandler, create_resource
session_handler = ScopedSessionHandler(engine)
PersonResource = create_resource(Person, session_handler)
By default create_resource will give you full CRUD+L (Create, Retrieve, Update, Delete, List). Although there are many options that you can pass to create_resource to modify exactly how the resource class is constructed.
After you create your resource class, you will need to load it into a dispatcher corresponding to your framework. For example, in flask-ripozo
from flask import Flask
from flask_ripozo import FlaskDispatcher
from ripozo.adapters import SirenAdapter, HalAdapter # These are the potential formats to return
app = Flask(__name__)
dispatcher = FlaskDispatcher(app)
dispatcher.register_adapters(SirenAdapter, HalAdapter)
dispatcher.register_resources(PersonResource)
app.run()
Metadata
Release files for ripozo-sqlalchemy 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ripozo-sqlalchemy-1.0.2.tar.gz | 9.3 kB | Without provenance | Details for ripozo-sqlalchemy-1.0.2.tar.gz |
Release files / ripozo-sqlalchemy-1.0.2.tar.gz
| Download URL | ripozo-sqlalchemy-1.0.2.tar.gz |
|---|---|
| Size | 9.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a72288f6faf90808978d256c3b657cc973b813060eafbfdd51c64d86c18727cc
|
|
BLAKE2b-256 checksum How to use checksums |
e48700c2f0e2a3589e035c06bd53227077ebba673b3b1f7cf033a75730b066d8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for ripozo-sqlalchemy 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.