225 أسطر
7.5 KiB
Python
225 أسطر
7.5 KiB
Python
from flask import Flask
|
|
from flask_swagger_ui import get_swaggerui_blueprint
|
|
import json
|
|
|
|
# Swagger configuration
|
|
SWAGGER_URL = '/docs'
|
|
API_URL = '/swagger.json'
|
|
|
|
swagger_ui_blueprint = get_swaggerui_blueprint(
|
|
SWAGGER_URL,
|
|
API_URL,
|
|
config={
|
|
'app_name': "My First Store API"
|
|
}
|
|
)
|
|
|
|
# Swagger JSON specification
|
|
swagger_spec = {
|
|
"openapi": "3.0.0",
|
|
"info": {
|
|
"title": "Simple Store API",
|
|
"description": "My first API project - A simple store management system",
|
|
"version": "1.0.0"
|
|
},
|
|
"servers": [
|
|
{
|
|
"url": "http://localhost:5000",
|
|
"description": "Development server"
|
|
}
|
|
],
|
|
"paths": {
|
|
"/": {
|
|
"get": {
|
|
"summary": "Home page",
|
|
"description": "Welcome message",
|
|
"responses": {
|
|
"200": {
|
|
"description": "Welcome message",
|
|
"content": {
|
|
"application/json": {
|
|
"example": {"message": "Welcome to my first API!"}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"/products": {
|
|
"get": {
|
|
"summary": "Get all products",
|
|
"description": "Returns a list of all products in the store",
|
|
"responses": {
|
|
"200": {
|
|
"description": "List of products",
|
|
"content": {
|
|
"application/json": {
|
|
"example": {
|
|
"products": [
|
|
{"id": 1, "name": "Laptop", "price": 5000, "quantity": 10}
|
|
]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"post": {
|
|
"summary": "Add new product",
|
|
"description": "Create a new product in the store",
|
|
"requestBody": {
|
|
"required": True,
|
|
"content": {
|
|
"application/json": {
|
|
"schema": {
|
|
"type": "object",
|
|
"properties": {
|
|
"name": {"type": "string", "example": "Keyboard"},
|
|
"price": {"type": "number", "example": 300},
|
|
"quantity": {"type": "integer", "example": 20}
|
|
},
|
|
"required": ["name", "price"]
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"responses": {
|
|
"201": {
|
|
"description": "Product created successfully"
|
|
},
|
|
"400": {
|
|
"description": "Missing required fields"
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"/products/{product_id}": {
|
|
"get": {
|
|
"summary": "Get single product",
|
|
"description": "Get details of a specific product by ID",
|
|
"parameters": [
|
|
{
|
|
"name": "product_id",
|
|
"in": "path",
|
|
"required": True,
|
|
"schema": {"type": "integer"},
|
|
"example": 1
|
|
}
|
|
],
|
|
"responses": {
|
|
"200": {
|
|
"description": "Product details"
|
|
},
|
|
"404": {
|
|
"description": "Product not found"
|
|
}
|
|
}
|
|
},
|
|
"put": {
|
|
"summary": "Update product",
|
|
"description": "Update an existing product",
|
|
"parameters": [
|
|
{
|
|
"name": "product_id",
|
|
"in": "path",
|
|
"required": True,
|
|
"schema": {"type": "integer"}
|
|
}
|
|
],
|
|
"requestBody": {
|
|
"required": True,
|
|
"content": {
|
|
"application/json": {
|
|
"schema": {
|
|
"type": "object",
|
|
"properties": {
|
|
"name": {"type": "string"},
|
|
"price": {"type": "number"},
|
|
"quantity": {"type": "integer"}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"responses": {
|
|
"200": {
|
|
"description": "Product updated"
|
|
},
|
|
"404": {
|
|
"description": "Product not found"
|
|
}
|
|
}
|
|
},
|
|
"delete": {
|
|
"summary": "Delete product",
|
|
"description": "Remove a product from the store",
|
|
"parameters": [
|
|
{
|
|
"name": "product_id",
|
|
"in": "path",
|
|
"required": True,
|
|
"schema": {"type": "integer"}
|
|
}
|
|
],
|
|
"responses": {
|
|
"200": {
|
|
"description": "Product deleted"
|
|
},
|
|
"404": {
|
|
"description": "Product not found"
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"/orders": {
|
|
"get": {
|
|
"summary": "Get all orders",
|
|
"description": "Returns a list of all orders",
|
|
"responses": {
|
|
"200": {
|
|
"description": "List of orders"
|
|
}
|
|
}
|
|
},
|
|
"post": {
|
|
"summary": "Create order",
|
|
"description": "Place a new order for a product",
|
|
"requestBody": {
|
|
"required": True,
|
|
"content": {
|
|
"application/json": {
|
|
"schema": {
|
|
"type": "object",
|
|
"properties": {
|
|
"product_id": {"type": "integer", "example": 1},
|
|
"quantity": {"type": "integer", "example": 2}
|
|
},
|
|
"required": ["product_id", "quantity"]
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"responses": {
|
|
"201": {
|
|
"description": "Order created successfully"
|
|
},
|
|
"400": {
|
|
"description": "Invalid order data or not enough stock"
|
|
},
|
|
"404": {
|
|
"description": "Product not found"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
def setup_swagger(app):
|
|
"""Add Swagger UI to Flask app"""
|
|
app.register_blueprint(swagger_ui_blueprint, url_prefix=SWAGGER_URL)
|
|
|
|
@app.route('/swagger.json')
|
|
def swagger_json():
|
|
return swagger_spec
|