flask-api-example/app.py

117 lines
2.3 KiB
Python
Raw Normal View History

2018-01-31 23:17:52 +01:00
# -*- coding: utf-8 -*-
2018-02-01 22:40:22 +01:00
2018-01-31 23:17:52 +01:00
# Librarys
2018-02-01 22:40:22 +01:00
# =========================
2018-01-31 00:42:22 +01:00
import os
2018-02-01 22:40:22 +01:00
from flask import Flask, jsonify, request
2018-01-20 00:37:59 +01:00
from flask_restplus import Resource, Api
2018-01-31 00:42:22 +01:00
from dotenv import load_dotenv, find_dotenv
2018-01-31 23:17:52 +01:00
from models import db, User, News, Comment
2018-02-05 23:00:30 +01:00
from flask_marshmallow import Marshmallow
2018-01-31 00:42:22 +01:00
2018-02-01 22:40:22 +01:00
# Extensions initialization
# =========================
2018-01-31 00:42:22 +01:00
load_dotenv(find_dotenv())
2018-01-20 00:37:59 +01:00
app = Flask(__name__)
2018-02-05 23:00:30 +01:00
ma = Marshmallow(app)
2018-02-01 22:40:22 +01:00
api = Api(app)
2018-01-31 23:17:52 +01:00
2018-02-01 22:40:22 +01:00
# Configurations
# =========================
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
2018-02-01 20:32:28 +01:00
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URI')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
2018-01-20 00:37:59 +01:00
PRE_URL = '/api/v1/'
2018-02-05 23:00:30 +01:00
# Schemas
# =========================
# User
class UserSchema(ma.Schema):
class Meta:
# Fields to expose
fields = ('username', 'mail')
user_schema = UserSchema()
users_schema = UserSchema(many=True)
# Routes
# =========================
# Signup
2018-01-20 00:37:59 +01:00
@api.route(PRE_URL + 'signup')
class Signup(Resource):
def post(self):
return {'hello': 'world'}
2018-02-05 23:00:30 +01:00
# Login
2018-01-20 00:37:59 +01:00
@api.route(PRE_URL + 'login')
class Login(Resource):
def post(self):
return {'hello': 'world'}
2018-02-05 23:00:30 +01:00
# Logout
2018-01-20 00:37:59 +01:00
@api.route(PRE_URL + 'login')
class Logout(Resource):
def get(self):
return {'hello': 'world'}
2018-02-05 23:00:30 +01:00
# User
@api.route(PRE_URL + 'user')
class UserList(Resource):
2018-01-20 00:37:59 +01:00
2018-02-05 23:00:30 +01:00
def get(self):
all_users = User.query.all()
return users_schema.jsonify(all_users)
@api.route(PRE_URL + 'user/<int:id>')
class UserSingle(Resource):
def get(self, id):
all_users = User.query.get(id)
return user_schema.jsonify(all_users)
# News
2018-01-20 00:37:59 +01:00
@api.route(PRE_URL + 'news')
2018-02-01 20:32:28 +01:00
class NewsList(Resource):
2018-01-20 00:37:59 +01:00
def get(self):
2018-01-31 23:17:52 +01:00
my_news = News.query.all()
2018-02-01 22:40:22 +01:00
return serializeQuery(my_news)
2018-01-20 00:37:59 +01:00
def post(self):
2018-02-01 22:40:22 +01:00
return request.form
2018-01-20 00:37:59 +01:00
@api.route(PRE_URL + 'news/<int:id>')
class News_single(Resource):
def get(self, id):
return {'hello': id}
2018-02-05 23:00:30 +01:00
# Comment
2018-01-20 00:37:59 +01:00
@api.route(PRE_URL + 'news/<int:id>/comments')
class Comments(Resource):
def get(self, id):
return {'hello': 'world'}
def post(self, id):
return {'hello': 'world'}
2018-02-05 23:00:30 +01:00
# Run
# =========================
2018-01-20 00:37:59 +01:00
if __name__ == '__main__':
2018-02-05 23:00:30 +01:00
app.run(debug=True if os.environ.get('DEBUG') == 'True' else False)
2018-01-20 00:37:59 +01:00