flask-api-example/app.py

74 lines
1.3 KiB
Python
Raw Normal View History

2018-01-31 23:17:52 +01:00
# -*- coding: utf-8 -*-
# Librarys
2018-01-31 00:42:22 +01:00
import os
2018-01-20 00:37:59 +01:00
from flask import Flask
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-01-31 00:42:22 +01:00
load_dotenv(find_dotenv())
2018-01-20 00:37:59 +01:00
2018-01-31 23:17:52 +01:00
2018-01-20 00:37:59 +01:00
app = Flask(__name__)
2018-01-31 23:17:52 +01:00
# Config Flask
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
# Config API
2018-01-20 00:37:59 +01:00
api = Api(app)
PRE_URL = '/api/v1/'
@api.route(PRE_URL + 'signup')
class Signup(Resource):
def post(self):
return {'hello': 'world'}
@api.route(PRE_URL + 'login')
class Login(Resource):
def post(self):
return {'hello': 'world'}
@api.route(PRE_URL + 'login')
class Logout(Resource):
def get(self):
return {'hello': 'world'}
@api.route(PRE_URL + 'news')
2018-01-31 23:17:52 +01:00
class News_all(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()
return ([i.serialize for i in my_news])
2018-01-20 00:37:59 +01:00
def post(self):
return {'hello': 'world'}
@api.route(PRE_URL + 'news/<int:id>')
class News_single(Resource):
def get(self, id):
return {'hello': id}
@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'}
if __name__ == '__main__':
2018-01-31 00:42:22 +01:00
app.run(debug=os.environ.get('DEBUG') == 'True' if True else False)
2018-01-20 00:37:59 +01:00