Started work on swagger

This commit is contained in:
Thastertyn 2024-03-12 20:12:19 +01:00
parent 248bf45044
commit dfccda02b8
8 changed files with 143 additions and 0 deletions

View File

@ -1,10 +1,14 @@
from flask import Flask from flask import Flask
from flask_jwt_extended import JWTManager from flask_jwt_extended import JWTManager
from flask_mail import Mail from flask_mail import Mail
from flasgger import Swagger
from flask_cors import CORS
app = Flask(__name__) app = Flask(__name__)
jwt_manager = JWTManager(app) jwt_manager = JWTManager(app)
mail = Mail(app) mail = Mail(app)
swag = Swagger(app)
cors = CORS(app)
def create_app(): def create_app():
from app.api import bp, bp_errors, bp_product, bp_user, bp_cart from app.api import bp, bp_errors, bp_product, bp_user, bp_cart

View File

@ -1,12 +1,17 @@
from flask import jsonify, abort, request from flask import jsonify, abort, request
from flask_jwt_extended import jwt_required, get_jwt_identity from flask_jwt_extended import jwt_required, get_jwt_identity
from app.doc.cart import show_cart_swagger, add_to_cart_swagger
from flasgger import swag_from
from app.api import bp_cart from app.api import bp_cart
from app.services.cart_service import CartService from app.services.cart_service import CartService
@bp_cart.route('', methods=['GET']) @bp_cart.route('', methods=['GET'])
@jwt_required() @jwt_required()
@swag_from(show_cart_swagger)
def show_cart(): def show_cart():
user_id = get_jwt_identity() user_id = get_jwt_identity()
@ -16,6 +21,7 @@ def show_cart():
@bp_cart.route('/add/<int:product_id>', methods=['PUT']) @bp_cart.route('/add/<int:product_id>', methods=['PUT'])
@jwt_required() @jwt_required()
@swag_from(add_to_cart_swagger)
def add_to_cart(product_id: int): def add_to_cart(product_id: int):
user_id = get_jwt_identity() user_id = get_jwt_identity()
count = request.args.get('count', default=1, type=int) count = request.args.get('count', default=1, type=int)

View File

@ -1,7 +1,12 @@
from flask import jsonify, abort from flask import jsonify, abort
from app.doc.main import main_swagger
from flasgger import swag_from
from app.api import bp from app.api import bp
@bp.route('/') @bp.route('/')
@swag_from(main_swagger)
def hello(): def hello():
return jsonify({'message': 'Hello, Flask!'}) return jsonify({'message': 'Hello, Flask!'})

View File

@ -1,11 +1,16 @@
from flask import jsonify, abort, request from flask import jsonify, abort, request
from flask_jwt_extended import jwt_required, get_jwt_identity from flask_jwt_extended import jwt_required, get_jwt_identity
from app.doc.product import get_products_swagger
from flasgger import swag_from
from app.api import bp_product from app.api import bp_product
from app.services.product_service import ProductService from app.services.product_service import ProductService
@bp_product.route('/get', methods=['GET']) @bp_product.route('/get', methods=['GET'])
@swag_from(get_products_swagger)
def get_products(): def get_products():
page = request.args.get('page', default=0, type=int) page = request.args.get('page', default=0, type=int)

80
app/doc/cart.py Normal file
View File

@ -0,0 +1,80 @@
show_cart_swagger = {
"tags": ["Cart"],
"parameters":
[
{
"name": "Authorization",
"in": "header",
"type": "string",
"required": True
}
],
"responses":
{
"200":
{
"description": "Current content of user's shopping cart",
"schema":
{
"items":
{
"count": {"type": "int"},
"date_added": {"type": "string"},
"name": {"type": "string"},
"price_subtotal": {"type": "string"}
},
"example":
[
{
"count": 5,
"date_added": "Fri, 08 Mar 2024 08:43:09 GMT",
"name": "Tablet",
"price_subtotal": "1499.95"
},
{
"count": 2,
"date_added": "Fri, 08 Mar 2024 06:43:09 GMT",
"name": "Laptop",
"price_subtotal": "999.95"
}
]
}
}
}
}
add_to_cart_swagger ={
"tags": ["Cart"],
"parameters":
[
{
"name": "Authorization",
"in": "header",
"type": "string",
"required": True
},
{
"name": "product_id",
"in": "path",
"type": "int",
"required": True
},
{
"name": "count",
"in": "query",
"type": "int",
"required": False
}
],
"responses":
{
"200":
{
"description": "Successfully added a product to cart"
},
"400":
{
"description": "Incorrect usage. For example id of product not found or product count < 1"
}
}
}

20
app/doc/main.py Normal file
View File

@ -0,0 +1,20 @@
main_swagger = {
"methods": ["GET"],
"parameters": [
],
"responses":
{
"200":
{
"description": "A hello world json",
"schema":
{
"type": "object",
"properties": {
"message": {"type": "string", "example": "Hello, Flask!"}
}
}
}
}
}

23
app/doc/product.py Normal file
View File

@ -0,0 +1,23 @@
get_products_swagger = {
"paths": {
"/get": {
"get": {
"summary": "Get products",
"responses": {
"200": {
"description": "Successfully retrieved products",
"schema": {
"type": "object",
"properties": {
"products": {"type": "array", "items": {"type": "object", "properties": {"id": {"type": "int"}, "name": {"type": "string"}, "price": {"type": "float"}}}}
}
}
},
"400": {
"description": "Bad request"
}
}
}
}
}
}

0
app/doc/user.py Normal file
View File