Skip to content

Commit

Permalink
Add from_yaml from_dict
Browse files Browse the repository at this point in the history
  • Loading branch information
strongbugman committed Dec 25, 2021
1 parent 102821e commit 2e9e25a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 43 deletions.
31 changes: 26 additions & 5 deletions apiman/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
class OpenApi:
HTTP_METHODS = {"get", "post", "put", "patch", "delete"}
SPECIFICATION_FILE = "__spec_file__"
SPECIFICATION_YAML = "__spec_yaml__"
SPECIFICATION_DICT = "__spec_dict__"
STATIC_DIR = f"{getattr(apiman, '__path__')[0]}/static/"

def __init__(
Expand Down Expand Up @@ -192,17 +194,36 @@ def decorator(func: typing.Callable) -> typing.Callable:

return decorator

def from_yaml(self, content: str) -> typing.Callable:
def decorator(func: typing.Callable) -> typing.Callable:
setattr(func, self.SPECIFICATION_YAML, content)
return func

return decorator

def from_dict(self, content: typing.Dict) -> typing.Callable:
def decorator(func: typing.Callable) -> typing.Callable:
setattr(func, self.SPECIFICATION_DICT, content)
return func

return decorator

def parse(self, func: typing.Callable) -> typing.Dict[str, typing.Any]:
# doc-string
specification = {}
if func.__doc__:
specification = yaml.safe_load(func.__doc__.split("---")[-1])
if not isinstance(specification, dict):
specification = {}
# file
if not specification and hasattr(func, self.SPECIFICATION_FILE):
specification = self.load_file(getattr(func, self.SPECIFICATION_FILE))
return specification
if specification:
return specification
elif hasattr(func, self.SPECIFICATION_YAML):
return yaml.safe_load(getattr(func, self.SPECIFICATION_YAML))
elif hasattr(func, self.SPECIFICATION_DICT):
return getattr(func, self.SPECIFICATION_DICT)
elif hasattr(func, self.SPECIFICATION_FILE):
return self.load_file(getattr(func, self.SPECIFICATION_FILE))
else:
return specification

@staticmethod
def load_file(file_path: str) -> typing.Dict:
Expand Down
90 changes: 53 additions & 37 deletions examples/dogstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,52 +31,66 @@


# define routes and schema(in doc string)
@openapi.from_dict(
{
"get": {
"parameters": [
{
"in": "path",
"name": "id",
"required": True,
"schema": {"type": "integer"},
},
{
"in": "query",
"name": "test_param1",
"required": True,
"schema": {"type": "string"},
},
{
"in": "header",
"name": "Test-Param2",
"required": True,
"schema": {"type": "string"},
},
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"properties": {
"age": {"type": "integer"},
"id": {
"description": "global unique",
"type": "integer",
},
"name": {"type": "string"},
},
"required": ["id", "name", "age"],
"type": "object",
}
}
},
"description": "OK",
},
"404": {"description": "Not found"},
},
"summary": "Get single dog",
"tags": ["dog"],
}
}
)
class DogView(MethodView):
"""
Declare multi method
---
get:
summary: Get single dog
tags:
- dog
parameters:
- name: id
in: path
required: true
schema:
type: integer
- name: test_param1
in: query
required: true
schema:
type: string
- name: Test-Param2
in: header
required: true
schema:
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Dog'
"404":
description: Not found
"""

def get(self, id):
"""
Normal annotation will be ignored
"""
openapi.validate_request(request)
return jsonify(DOGS[id])

def delete(self, id):
@openapi.from_yaml(
"""
Declare single method
---
summary: Delete single dog
tags:
- dog
Expand All @@ -92,6 +106,8 @@ def delete(self, id):
"404":
description: Not found
"""
)
def delete(self, id):
DOGS.pop(id)
return Response(status=204)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "apiman"
version = "0.3.1"
version = "0.3.2"
description = "Integrate api manual for your web project"
license = "BSD"
readme = "README.md"
Expand Down

0 comments on commit 2e9e25a

Please sign in to comment.