Skip to content

Commit

Permalink
Add method to generate openapi spec file
Browse files Browse the repository at this point in the history
  • Loading branch information
strongbugman committed Jun 6, 2022
1 parent d501fe2 commit ba8b39c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
8 changes: 8 additions & 0 deletions apiman/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,14 @@ def parse(self, func: typing.Callable) -> typing.Dict[str, typing.Any]:
else:
return specification

def generate_specification_file(self, filename: str):
with open(filename, "w") as f:
f.writelines(
json.dumps(self.specification)
if filename.endswith("json")
else yaml.safe_dump(self.specification)
)

@staticmethod
def load_file(file_path: str) -> typing.Dict:
with open(file_path) as f:
Expand Down
14 changes: 11 additions & 3 deletions examples/_bottle.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import pytest
from bottle import app, request, route, run
from webtest import TestApp
Expand Down Expand Up @@ -101,9 +102,6 @@ def validate(path):

def test_app():
client = TestApp(app[0])
spec = apiman.load_specification(app[0])
apiman.validate_specification()
assert client.get(apiman.config["specification_url"]).json == spec
assert client.get(apiman.config["swagger_url"]).status_code == 200
assert client.get(apiman.config["redoc_url"]).status_code == 200
# --
Expand Down Expand Up @@ -163,6 +161,16 @@ def test_app():
== 200
)

spec = apiman.load_specification(app[0])
apiman.validate_specification()
apiman.generate_specification_file("test_bottle.yaml")
apiman.generate_specification_file("test_bottle.json")
assert os.path.exists("test_bottle.yaml")
assert os.path.exists("test_bottle.json")
os.remove("test_bottle.yaml")
os.remove("test_bottle.json")
assert client.get(apiman.config["specification_url"]).json == spec


if __name__ == "__main__":
run(host="localhost", port=8080, debug=True)

0 comments on commit ba8b39c

Please sign in to comment.