Giter VIP home page Giter VIP logo

flask-restapi-gen's Introduction

RESTful API Generator,,

RESTful API Generator, autogenerates the RESTful endpoints from the SQL Alchemy models. Works with all major relational databases and MongoDB.

Install

pip install flaskrestgen

Usage

'''A sample declarative model using Sqlalchemy'''
from sqlalchemy import Column, String, Integer, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()

class User(Base):
  __tablename__ = 'users'

  id = Column(Integer, primary_key=True)
  username = Column(String(50))

#engine instance
engine = create_engine('postgres://user:postgres@localhost:5432/users')
#create tables through your meta data
Base.metadata.create_all(engine)

#now here is the flaskrestgen api at play
from flask import Flask 
from flaskrestgen import RESTApi


app = Flask(__name__)
db_session = sessionmaker(engine)()

#create and instance of flaskrestgen
restApi = RESTApi(app, db_session)
#generates GET, POST, PUT and DELETE for User model
restApi.rest_for(User)
if __name__ == '__main__':
  app.run(host='localhost', port=8000)
  

Endpoints

GET http://localhost:8000/users ("users" being the name of the table)
GET http://localhost:8000/users/<id> 
POST http://localhost:8000/users
    JSON Body: {
        "username" : "newuser"
    }
PUT http://localhost:8000/users/<id>
    JSON Body: {
        "username" : "updatednewuser"
    }
DELETE http://localhost:8000/users/<id>

Apply Constraints and Validation

Create a file called "validation.json" in your project directory
and start applying constraints to your fields easily

{
    "User" : {
        "username" : {
            "max_len" : 10,
            "min_len" : 3,
            "not_null" : true,
            "interpolate" : "lambda val: val.upper()"
        }
    }
}

Now finally point flaskrestgen instance to that validation file
    
    restApi = RESTApi(app, db_session, validation_file='validation.json')
    #now you have validation in place that easily

Advanced

There are more advanced usecase such as: 
1. Placing a hook for advance manipulation for rest endpoints
2. Auto detection of one to one, one to many many to many constraints and generating rest endpoints bidirectionally
3. Extracting the desired foreign key resource to avoid multiple networks calls 
and few mores..
Finally I will be updating the docs for these advanced usecase when I have time.
Thank you!!


### Credits
Contributors are super welcome!!!!!

flask-restapi-gen's People

Contributors

robusgauli avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.