Created
May 14, 2021 00:45
-
-
Save reiktar/daa1a7d299fa2f72874adbc68b60aa53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uuid | |
from apispec import APISpec | |
from apispec.ext.marshmallow import MarshmallowPlugin | |
from apispec_webframeworks.flask import FlaskPlugin | |
from flask import Flask | |
from marshmallow import Schema, fields, schema | |
from datetime import datetime, timedelta | |
# Create an APISpec | |
spec = APISpec( | |
title="Dopple Picker-Packer", | |
version="1.0.0", | |
openapi_version="3.0.2", | |
plugins=[FlaskPlugin(), MarshmallowPlugin()], | |
) | |
# Optional marshmallow support | |
class OrderSchema(Schema): | |
orderId = fields.Int() | |
description = fields.Str() | |
orderDate = fields.DateTime() | |
class OrderEventSchema(Schema): | |
orderId = fields.Int() | |
timestamp = fields.DateTime( | |
required=False, | |
default=lambda: datetime(), | |
allow_none=False | |
) | |
# Optional security scheme support | |
api_key_scheme = {"type": "apiKey", "in": "header", "name": "X-API-Key"} | |
spec.components.security_scheme("ApiKeyAuth", api_key_scheme) | |
# Optional Flask support | |
app = Flask(__name__) | |
@app.route("/order") | |
def list_orders(): | |
""" A List of orders | |
--- | |
get: | |
description: Get all orders | |
security: | |
- ApiKeyAuth: [] | |
responses: | |
200: | |
description: Return all orders | |
content: | |
applicaton/json: | |
schema: | |
type: array | |
items: OrderSchema | |
""" | |
order_data = [ | |
{ "orderId": 1, "description":"Order 1", "orderDate": | |
datetime.now() + timedelta(days=10, hours=3)}, | |
{ "orderId": 2, "description":"Order 2", "orderDate": | |
datetime.now() + timedelta(days=5, hours=12)} | |
] | |
#order_data = { "orderId": 1, "description":"Order 1", "orderDate": | |
# datetime.now() + timedelta(days=10, hours=3)} | |
return OrderSchema(many=True).dump(order_data,many=True) | |
# Register the path and the entities within it | |
with app.test_request_context(): | |
spec.path(view=list_orders) |
exemaitch
commented
May 14, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment