Skip to main content

A module to expose the structure of SQLAlchemy models as a RESTful API

Project description

modelstruct
=========
A simple Python module to expose SQLAlchemy model structures as a RESTful API.

## Example
Python model:

```python
class Book(db.Model):
__tablename__ = 'books'

id = db.Column(db.Integer, primary_key=True)
time_created = db.Column(db.DateTime)
name = db.Column(db.String(140), unique=True)
description = db.Column(db.String(320))
in_inventory = db.Column(db.Boolean, default=True)
user_id = db.Column(db.Integer)
author = db.Column(db.String(140))

def __init__(self, name, author, description, in_inventory, user_id):
self.name = name
self.description = description
self.in_inventory = in_inventory
self.user_id = user_id
self.time_created = datetime.datetime.now()
self.author = author

def __repr__(self):
return '<Book %r>' % self.id
```

JSON exposed via a RESTful API for this model:

```json
{
"structure": {
"author": "VARCHAR(140)",
"description": "VARCHAR(320)",
"id": "INTEGER",
"in_inventory": "BOOLEAN",
"name": "VARCHAR(140)",
"time_created": "DATETIME",
"user_id": "INTEGER"
}
}
```

## Install

```
pip install modelstruct
```

## Usage

1. Import the Python module
```python
from modelstruct import get_json
```

2. Create a new route for the endpoint you want to be your API (below the example is in Flask, and creates the API at the location `/models`, with the model class name `MODEL_NAME_HERE`)

```python
@app.route('/models', methods=['GET'])
def get_model_structure():
return get_json(MODEL_NAME_HERE)
```

3. That's it! Send a GET request to the endpoint and get the model's structure.

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

modelstruct-1.0.0.tar.gz (3.1 kB view details)

Uploaded Source

File details

Details for the file modelstruct-1.0.0.tar.gz.

File metadata

  • Download URL: modelstruct-1.0.0.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for modelstruct-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9e3a5d25bd90455c065ed008013727ea3712fe31b2b838e1f7777b567bcad5f1
MD5 a9fa272086b434c25f09e04b8cafcc4b
BLAKE2b-256 305c6a42e838b15b892dddf36b5a4498994743f935dab8495edb48f0a5fd1682

See more details on using hashes here.

Supported by

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