Skip to content

Commit 6294d82

Browse files
authored
Merge pull request #15 from ioet/feature/create-cli-generate-file-with-api-defÂ#10
Add CLI commands to generate API definition
2 parents 5153909 + 85589cf commit 6294d82

File tree

5 files changed

+103
-4
lines changed

5 files changed

+103
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ __pycache__/
2020

2121
# Files generated for development
2222
.env
23+
timetracker-api-postman-collection.json
24+
swagger.json

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,21 @@ automatically [pip](https://pip.pypa.io/en/stable/) as well.
4747
```
4848
4949
- Open `http://127.0.0.1:5000/` in a browser. You will find in the presented UI
50-
a link to the swagger.json with the definition of the api.
50+
a link to the swagger.json with the definition of the api.
51+
52+
53+
## CLI
54+
55+
There are available commands, aware of the API, that can be very helpful to you. You
56+
can check them out by running
57+
58+
```
59+
python cli.py
60+
```
61+
62+
If you want to run an specific command, e.g. `gen_swagger_json`, specify it as a param
63+
as well as its correspondent options.
64+
65+
```
66+
python cli.py gen_swagger_json -f ~/Downloads/swagger.json
67+
```

cli.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python3
2+
3+
print("****************")
4+
print("TimeTracker CLI")
5+
print("****************")
6+
7+
import os
8+
9+
from flask import json
10+
from flask_script import Manager
11+
12+
from time_tracker_api import create_app
13+
from time_tracker_api.api import api
14+
15+
app = create_app()
16+
cli_manager = Manager(app)
17+
18+
19+
@cli_manager.command
20+
@cli_manager.option('-f', '--filename',
21+
dest='filename',
22+
help='Path of the swagger file. By default swagger.json')
23+
def gen_swagger_json(filename='swagger.json'):
24+
""" Exports swagger specifications in json format """
25+
schema_json_data = json.dumps(api.__schema__)
26+
save_data(schema_json_data, filename)
27+
28+
29+
@cli_manager.command
30+
@cli_manager.option('-f', '--filename',
31+
dest='filename',
32+
help='Path of the postman collection file.'
33+
'By default it is timetracker-api-postman-collection.json')
34+
@cli_manager.option('-b', '--base-url-placeholder',
35+
dest='base_url_placeholder',
36+
help='Text used as placeholder. E.g. {{timetracker_api_host}}.'
37+
'By default the base url will be http://localhost')
38+
def gen_postman_collection(filename='timetracker-api-postman-collection.json',
39+
base_url_placeholder=None):
40+
""" Generates a Postman collection for the API """
41+
data = api.as_postman(urlvars=False, swagger=True)
42+
postman_collection_json_data = json.dumps(data)
43+
if base_url_placeholder is not None:
44+
parsed_json = postman_collection_json_data.replace("http://localhost", base_url_placeholder)
45+
else:
46+
parsed_json = postman_collection_json_data
47+
48+
save_data(parsed_json, filename)
49+
50+
51+
def save_data(data: str, filename: str) -> None:
52+
""" Save text content to a file """
53+
if filename:
54+
try:
55+
real_path = os.path.expanduser(filename)
56+
with open(real_path, "w") as f:
57+
f.write(data)
58+
print("%s was generated successfully" % real_path)
59+
except OSError as err:
60+
print("Error while generating '%s': %s" % filename, err)
61+
else:
62+
print(data)
63+
64+
65+
if __name__ == "__main__":
66+
cli_manager.run()

requirements/prod.txt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1+
# requirements/prod.txt
2+
3+
# For production releases
4+
5+
#Required by Flask
16
Flask==1.1.1
2-
flask-restplus==0.13.0
37
flake8==3.7.9
8+
WSGIserver==1.3
49
Werkzeug==0.16.1
5-
gunicorn==20.0.4
10+
Jinja2==2.11.1
11+
12+
#WSGI server
13+
gunicorn==20.0.4
14+
15+
#Swagger support for Restful API
16+
flask-restplus==0.13.0
17+
18+
#CLI support
19+
Flask-Script==2.0.6

run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
from time_tracker_api import create_app
66

77
app = create_app()
8-
print("BPM Projects API server was created")
8+
print("TimeTracker API server was created")

0 commit comments

Comments
 (0)