# -*- coding: utf-8 -*-
# @Author  : [martinatseequent](https://github.com/martinatseequent)
# @Time    : 2021/6/22 9:32

import json
from http import HTTPStatus

from flask import make_response
from flask_openapi3 import OpenAPI, APIBlueprint
from flask_openapi3.models import Info
from pydantic import BaseModel, Field

app = OpenAPI(__name__, info=Info(title="Hello API", version="1.0.0"), )

bp = APIBlueprint("Hello BP", __name__)


class PathName(BaseModel):
    name: str = Field(..., description="The name")


class Message(BaseModel):
    message: str = Field(..., description="The message")


@bp.get("/hello/<string:name>", responses={"200": Message})
def hello(path: PathName):
    message = {"message": f"""Hello {path.name}!"""}

    response = make_response(json.dumps(message), HTTPStatus.OK)
    # response = make_response("sss", HTTPStatus.OK)
Example #2
0
# -*- coding: utf-8 -*-
# @Author  : llc
# @Time    : 2021/6/6 14:05

from typing import Optional

from pydantic import BaseModel, Field

from flask_openapi3 import APIBlueprint, OpenAPI
from flask_openapi3.models import Tag, Info
from flask_openapi3.models.security import HTTPBearer

info = Info(title='book API', version='1.0.0')
securitySchemes = {"jwt": HTTPBearer(bearerFormat="JWT")}

app = OpenAPI(__name__, info=info, securitySchemes=securitySchemes)

tag = Tag(name='book', description="Some Book")
security = [{"jwt": []}]


class Unauthorized(BaseModel):
    code: int = Field(-1, description="Status Code")
    message: str = Field("Unauthorized!", description="Exception Information")


api = APIBlueprint(
    '/book',
    __name__,
    url_prefix='/api',
    abp_tags=[tag],
Example #3
0
# -*- coding: utf-8 -*-
# @Author  : llc
# @Time    : 2021/6/21 11:23
from flask_openapi3 import OpenAPI, OAuthConfig
from flask_openapi3.models import Info
from flask_openapi3.models.security import OAuth2, OAuthFlows, OAuthFlowImplicit

info = Info(title='oauth API', version='1.0.0')

oauth_config = OAuthConfig(clientId="xxx", clientSecret="xxx")

oauth2 = OAuth2(flows=OAuthFlows(implicit=OAuthFlowImplicit(
    authorizationUrl="https://example.com/api/oauth/dialog",
    scopes={
        "write:pets": "modify pets in your account",
        "read:pets": "read your pets"
    })))
securitySchemes = {"oauth2": oauth2}

app = OpenAPI(__name__,
              info=info,
              oauth_config=oauth_config,
              securitySchemes=securitySchemes)

security = [{"oauth2": ["write:pets", "read:pets"]}]


@app.get("/", security=security)
def oauth():
    return "oauth"