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