Skip to content

Instantly share code, notes, and snippets.

@reiktar
Created May 14, 2021 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reiktar/daa1a7d299fa2f72874adbc68b60aa53 to your computer and use it in GitHub Desktop.
Save reiktar/daa1a7d299fa2f72874adbc68b60aa53 to your computer and use it in GitHub Desktop.
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
Copy link

from datetime import datetime, timedelta

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
from flask import jsonify

# 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("/orders")
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 jsonify(OrderSchema(many=True).dump(order_data))


# Register the path and the entities within it
with app.test_request_context():
    spec.path(view=list_orders)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment