1

I'm currently working on a javascript (react based) app that basically displays data from some database's tables, and allows to add, delete, update data from the tables. I implemented the services to access the database using Flask and SQLAlchemy, trying to follow the REST specification (more as a guideline than a 'dogma', since I'm not sharing my API over internet).

I want to implement a web-service that describes each table: let's take the table below

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)

I want that kind of thing:

GET /user/description

Response: something like

[
{column_name: 'id', type: 'int'  , p_key: 'True'},
{column_name: 'username', type: 'string'},
{column_name: 'email', type: 'string'}
]

(plus other useful metadata for a client willing to insert or update data)

I would like to know if there's such a kind of operation in the REST specification, and if not where can I find guidelines for implementing such a service (the dos and don'ts) - I would rather not reinvent the wheel for something that looks to me like a fairly common problem in programming.

Hugo Merzisen
  • 303
  • 2
  • 8

1 Answers1

0

Ok first what you need is an indicator, so you need to check what's the client want to view (for e.g the first user so id=0/1) send an ajax request for example or a form or whatever you want to this path:

@app.route("/user/description", methods=[...])

Now what you need to do is to filter the data by the ID you got so

username = User.query.filter(User.id).all()[0].username # etc

get the type of the username by doing type(username) and put all of this data into a dict to send the JSON response(do what I just showed you for every column). If you want to find if the column has a primary key I suggest you'll watch this question:

Patch
  • 694
  • 1
  • 10
  • 29
  • Thanks for answering. But my question is not 'how to implement it', I'm looking for a specification telling what the response of the service should be. For the now I'm writing a home cooked API. – Hugo Merzisen May 27 '20 at 08:01
  • What do you mean it needs to be in JSON format, dict in Python. Can you maybe elaborate? – Patch May 27 '20 at 08:49