initial commit
This commit is contained in:
13
project_name/ext/restapi/__init__.py
Normal file
13
project_name/ext/restapi/__init__.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from flask import Blueprint
|
||||
from flask_restful import Api
|
||||
|
||||
from .resources import ProductItemResource, ProductResource
|
||||
|
||||
bp = Blueprint("restapi", __name__, url_prefix="/api/v1")
|
||||
api = Api(bp)
|
||||
|
||||
|
||||
def init_app(app):
|
||||
api.add_resource(ProductResource, "/product/")
|
||||
api.add_resource(ProductItemResource, "/product/<product_id>")
|
||||
app.register_blueprint(bp)
|
||||
35
project_name/ext/restapi/resources.py
Normal file
35
project_name/ext/restapi/resources.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from flask import abort, jsonify
|
||||
from flask_restful import Resource
|
||||
from flask_simplelogin import login_required
|
||||
|
||||
from project_name.models import Product
|
||||
|
||||
|
||||
class ProductResource(Resource):
|
||||
def get(self):
|
||||
products = Product.query.all() or abort(204)
|
||||
return jsonify(
|
||||
{"products": [product.to_dict() for product in products]}
|
||||
)
|
||||
|
||||
@login_required(basic=True, username="admin")
|
||||
def post(self):
|
||||
"""
|
||||
Creates a new product.
|
||||
|
||||
Only admin user authenticated using basic auth can post
|
||||
Basic takes base64 encripted username:password.
|
||||
|
||||
# curl -XPOST localhost:5000/api/v1/product/ \
|
||||
# -H "Authorization: Basic Y2h1Y2s6bm9ycmlz" \
|
||||
# -H "Content-Type: application/json"
|
||||
"""
|
||||
return NotImplementedError(
|
||||
"Someone please complete this example and send a PR :)"
|
||||
)
|
||||
|
||||
|
||||
class ProductItemResource(Resource):
|
||||
def get(self, product_id):
|
||||
product = Product.query.filter_by(id=product_id).first() or abort(404)
|
||||
return jsonify(product.to_dict())
|
||||
Reference in New Issue
Block a user