Tape a REST API with a local database
Project description
Flask Scotch
Tape a REST API with a local database
Key Features
- Represent remote model in the form of a python class to be able to manipuldate easily
- Fetch objects from the database or from the remote API using the attributes of the declared models
- Update/delete/create object on the remote API using the declared models
Install
pip install flask-scotch
Getting started
First, you need to register the extension in flask
from flask_scotch import FlaskScotch
from flask import Flask
# Configure the URL of the remote API with the configuration
# SCOTCH_API_URL='https://mysite.com/api/v1'
# Register the sqlAlchemy engine with flask-sqlalchemy, or provide it directly
# in the constructor
scotch = FlaskScotch()
app = Flask()
scotch.init_app(app)
Then, you can declare the "remote model", that is, the model present on the remote server.
from flask_scotch import RemoteModel, LocalRelationship, LocalModel, RemoteRelationship
import sqlalchemy as sa
db = sa.create_engine()
class Item(LocalModel, db.Model):
__remote_directory__ = 'items'
id: int
name: str
description: str
storage_id: int
storage = RemoteRelationship("Storage")
class Storage(RemoteModel):
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String)
items = LocalRelationship("Item")
# You can then use this model to fetch data from the remote api:
all_items = Storage.api.all()
my_storage = Storage(id=10, name='pen')
final_storage = Storage.api.create(my_storage)
final_storage.name = 'green pen'
final_storage.update()
items = [Item(storage_id=10, name="item1"), Item(storage_id=10, name="item2")]
db.session.add(*items)
db.session.commit()
# Can now access the local items from
# the storage object
for item in my_storage.items:
print(item)
final_storage.delete()
TODO
- ForeignModel: to be able to access an object from the API when it's accessed from a local model
- Handle 1:1 relations
- Handle 1:N relations
- LocalModel:
- Handle 1:1 relations
- Handle 1:N relations
- ForeignModel and LocalModel: ability to reference a class with a string, rather than with the class directly
- LocalModel, propagates changes when added to list, so that sqlAlchemy updates the id when necessary (maybe using InstrumentedList can help)
- Improve handling of return values from the API, and throw error based on the HTTP code returned
- Improve typing of all public functions and classes
- Automatically detect cross-referencing PartialModels and RemoteModel to avoid having to declare everything
- Have a 100% code coverage
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
flask-scotch-0.0.2.tar.gz
(6.7 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flask-scotch-0.0.2.tar.gz.
File metadata
- Download URL: flask-scotch-0.0.2.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
208379022ce95f5c012e326736bd75e6429f9952f5e78f603659bc59ab616f10
|
|
| MD5 |
272b5d5a3d2875e201435f4b4443c43b
|
|
| BLAKE2b-256 |
a9a5d8b50a110a6e41e794c22bea44ad5e4a22cda59bf4fc105151af47890e41
|
File details
Details for the file flask_scotch-0.0.2-py3-none-any.whl.
File metadata
- Download URL: flask_scotch-0.0.2-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9503c1ba4c60dd6e2f56371bd3acdcd4c59827ec05939d5ef1c25338df491aa1
|
|
| MD5 |
2717efcb50e49982c91ceb434770faab
|
|
| BLAKE2b-256 |
2ac0392165d9caeba4d1e26eb3e5b604b0e136a131099e58a398eb72b21798fe
|