Skip to content

Commit

Permalink
Moved APISpec to a class method
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Jul 7, 2020
1 parent ae2192a commit 3a82dba
Showing 1 changed file with 22 additions and 47 deletions.
69 changes: 22 additions & 47 deletions src/labthings/apispec/apispec.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from ..utilities import get_docstring, get_summary

from flask.views import http_method_funcs
from werkzeug.routing import Rule
from http import HTTPStatus

Expand Down Expand Up @@ -35,50 +36,24 @@ def rule_to_apispec_path(rule: Rule, view, apispec: APISpec):


def view_to_apispec_operations(view, apispec: APISpec):
"""Generate APISpec `operations` argument from a flask View"""

# Build dictionary of operations (HTTP methods)
ops = {}
for op in ("get", "post", "put", "delete"):
if hasattr(view, op):

ops[op] = {
"description": getattr(view, "description", None)
or get_docstring(view),
"summary": getattr(view, "summary", None) or get_summary(view),
"tags": list(view.get_tags()),
}

# Add arguments schema
if (op in (("post", "put", "delete"))) and hasattr(view, "get_args"):
request_schema = convert_to_schema_or_json(view.get_args(), apispec)
if request_schema:
ops[op]["requestBody"] = {
"content": {"application/json": {"schema": request_schema}}
}

# Add response schema
if hasattr(view, "get_responses"):
ops[op]["responses"] = {}

for code, response in view.get_responses().items():
ops[op]["responses"][code] = {
"description": response.get("description")
or HTTPStatus(code).phrase,
"content": {
# See if response description specifies a content_type
# If not, assume application/json
response.get("content_type", "application/json"): {
"schema": convert_to_schema_or_json(
response.get("schema"), apispec
)
}
if response.get("schema")
else {} # If no schema is defined, don't include one in the APISpec
},
}
else:
# If no explicit responses are known, populate with defaults
ops[op]["responses"] = {200: {HTTPStatus(200).phrase}}

return ops
specs = {}
if hasattr(view, "get_apispec"):
specs = view.get_apispec()

for method, spec in specs.items():
if "responses" in spec:
for response_code, response in spec["responses"].items():
if "schema" in response:
response["schema"] = convert_to_schema_or_json(
response["schema"], apispec
)

if "requestBody" in spec:
for content_type, description in (
spec["requestBody"].get("content", {}).items()
):
description["schema"] = convert_to_schema_or_json(
description["schema"], apispec
)

return specs

0 comments on commit 3a82dba

Please sign in to comment.