From dfccda02b835e7e1c32632c4cbf4dc505c63eec0 Mon Sep 17 00:00:00 2001 From: Thastertyn Date: Tue, 12 Mar 2024 20:12:19 +0100 Subject: [PATCH] Started work on swagger --- app/__init__.py | 4 ++ app/api/routes/cart_routes.py | 6 +++ app/api/routes/main_routes.py | 5 ++ app/api/routes/product_routes.py | 5 ++ app/doc/cart.py | 80 ++++++++++++++++++++++++++++++++ app/doc/main.py | 20 ++++++++ app/doc/product.py | 23 +++++++++ app/doc/user.py | 0 8 files changed, 143 insertions(+) create mode 100644 app/doc/cart.py create mode 100644 app/doc/main.py create mode 100644 app/doc/product.py create mode 100644 app/doc/user.py diff --git a/app/__init__.py b/app/__init__.py index 1478e2c..2cf5aa8 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,10 +1,14 @@ from flask import Flask from flask_jwt_extended import JWTManager from flask_mail import Mail +from flasgger import Swagger +from flask_cors import CORS app = Flask(__name__) jwt_manager = JWTManager(app) mail = Mail(app) +swag = Swagger(app) +cors = CORS(app) def create_app(): from app.api import bp, bp_errors, bp_product, bp_user, bp_cart diff --git a/app/api/routes/cart_routes.py b/app/api/routes/cart_routes.py index 981f64f..1f8c626 100644 --- a/app/api/routes/cart_routes.py +++ b/app/api/routes/cart_routes.py @@ -1,12 +1,17 @@ from flask import jsonify, abort, request 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.services.cart_service import CartService @bp_cart.route('', methods=['GET']) @jwt_required() +@swag_from(show_cart_swagger) def show_cart(): user_id = get_jwt_identity() @@ -16,6 +21,7 @@ def show_cart(): @bp_cart.route('/add/', methods=['PUT']) @jwt_required() +@swag_from(add_to_cart_swagger) def add_to_cart(product_id: int): user_id = get_jwt_identity() count = request.args.get('count', default=1, type=int) diff --git a/app/api/routes/main_routes.py b/app/api/routes/main_routes.py index b5a35ed..0ff4e9c 100644 --- a/app/api/routes/main_routes.py +++ b/app/api/routes/main_routes.py @@ -1,7 +1,12 @@ from flask import jsonify, abort +from app.doc.main import main_swagger + +from flasgger import swag_from + from app.api import bp @bp.route('/') +@swag_from(main_swagger) def hello(): return jsonify({'message': 'Hello, Flask!'}) \ No newline at end of file diff --git a/app/api/routes/product_routes.py b/app/api/routes/product_routes.py index ceb1bbd..ccbaa8e 100644 --- a/app/api/routes/product_routes.py +++ b/app/api/routes/product_routes.py @@ -1,11 +1,16 @@ from flask import jsonify, abort, request 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.services.product_service import ProductService @bp_product.route('/get', methods=['GET']) +@swag_from(get_products_swagger) def get_products(): page = request.args.get('page', default=0, type=int) diff --git a/app/doc/cart.py b/app/doc/cart.py new file mode 100644 index 0000000..073451c --- /dev/null +++ b/app/doc/cart.py @@ -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" + } + } +} diff --git a/app/doc/main.py b/app/doc/main.py new file mode 100644 index 0000000..7a91f5e --- /dev/null +++ b/app/doc/main.py @@ -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!"} + } + } + } + } +} \ No newline at end of file diff --git a/app/doc/product.py b/app/doc/product.py new file mode 100644 index 0000000..fe8b8d7 --- /dev/null +++ b/app/doc/product.py @@ -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" + } + } + } + } + } +} \ No newline at end of file diff --git a/app/doc/user.py b/app/doc/user.py new file mode 100644 index 0000000..e69de29