def test_spec_generate(name, app): api = FlaskPydanticSpec( name, app=app, title=f"{name}", info={ "title": "override", "description": "api level description" }, tags=[{ "name": "lone", "description": "a lone api" }], ) spec = api.spec assert spec["info"]["title"] == name assert spec["info"]["description"] == "api level description" assert spec["paths"] == {} assert spec["tags"] == []
from itertools import count from typing import Optional from flask import Flask, request, jsonify from flask_pydantic_spec import (FlaskPydanticSpec, Response, Request) from pydantic import BaseModel, Field from tinydb import TinyDB, Query from tinydb.storages import MemoryStorage server = Flask(__name__) spec = FlaskPydanticSpec('flask', title='Live de Python') spec.register(server) database = TinyDB(storage=MemoryStorage) c = count() class QueryPessoa(BaseModel): id: Optional[int] nome: Optional[str] idade: Optional[int] class Pessoa(BaseModel): id: Optional[int] = Field(default_factory=lambda: next(c)) nome: str idade: int class Pessoas(BaseModel): pessoas: list[Pessoa] count: int
from hashlib import sha256 import requests from datetime import datetime from flask_pydantic_spec import FlaskPydanticSpec, Response from .models import db_wrapper, Build from .celery import make_celery BUILD_NAMESPACE = uuid.UUID('dc3606bb-8007-454f-8974-09cd2a517f14') app = Flask(__name__) app.config.from_object(config) db_wrapper.init_app(app) celery = make_celery(app) api = FlaskPydanticSpec('flask') api.register(app) @app.route('/', methods=['GET']) @api.validate(resp=Response('HTTP_203'), tags=['test', 'demo']) def with_code_header(): """ Esto no le des pelota que soy yo boludeando con pydantic """ return jsonify(language=request.context.headers.Lang), 203, {'X': 233} @app.route('/build', methods=['POST']) def create_build(): if not request.is_json:
def before_handler(req, resp, err, _): if err: resp.headers["X-Error"] = "Validation Error" def after_handler(req, resp, err, _): resp.headers["X-Validation"] = "Pass" def api_after_handler(req, resp, err, _): resp.headers["X-API"] = "OK" api = FlaskPydanticSpec( "flask", before=before_handler, after=after_handler, title="Test API" ) app = Flask(__name__) @app.route("/ping") @api.validate(headers=Headers, tags=["test", "health"]) def ping(): """summary description""" return jsonify(msg="pong") @app.route("/api/user", methods=["GET"]) @api.validate( query=QueryParams,
def test_register(name, app): api = FlaskPydanticSpec(name) api.register(app)
def test_spectree_init(): spec = FlaskPydanticSpec(path="docs") conf = Config() assert spec.config.TITLE == conf.TITLE assert spec.config.PATH == "docs"
"name": "lone", "description": "a lone api" }], ) spec = api.spec assert spec["info"]["title"] == name assert spec["info"]["description"] == "api level description" assert spec["paths"] == {} assert spec["tags"] == [] api = FlaskPydanticSpec( "flask", tags=[{ "name": "lone", "description": "a lone api" }], validation_error_code=400, ) api_strict = FlaskPydanticSpec("flask", mode="strict") api_greedy = FlaskPydanticSpec("flask", mode="greedy") api_customize_backend = FlaskPydanticSpec(backend=FlaskBackend) def create_app(): app = Flask(__name__) @app.route("/foo") @api.validate() def foo(): pass