Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ __pycache__/

# Files generated for development
.env
timetracker-api-postman-collection.json
swagger.json
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,21 @@ automatically [pip](https://pip.pypa.io/en/stable/) as well.
```

- Open `http://127.0.0.1:5000/` in a browser. You will find in the presented UI
a link to the swagger.json with the definition of the api.
a link to the swagger.json with the definition of the api.


## CLI

There are available commands, aware of the API, that can be very helpful to you. You
can check them out by running

```
python cli.py
```

If you want to run an specific command, e.g. `gen_swagger_json`, specify it as a param
as well as its correspondent options.

```
python cli.py gen_swagger_json -f ~/Downloads/swagger.json
```
66 changes: 66 additions & 0 deletions cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python3

print("****************")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to have these prints before the import section ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because I wanted to know whether the CLI file is actually running before any error inside it happens.

print("TimeTracker CLI")
print("****************")

import os

from flask import json
from flask_script import Manager

from time_tracker_api import create_app
from time_tracker_api.api import api

app = create_app()
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__":
cli_manager.run()
18 changes: 16 additions & 2 deletions requirements/prod.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# requirements/prod.txt

# For production releases

#Required by Flask
Flask==1.1.1
flask-restplus==0.13.0
flake8==3.7.9
WSGIserver==1.3
Werkzeug==0.16.1
gunicorn==20.0.4
Jinja2==2.11.1

#WSGI server
gunicorn==20.0.4

#Swagger support for Restful API
flask-restplus==0.13.0

#CLI support
Flask-Script==2.0.6
2 changes: 1 addition & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
from time_tracker_api import create_app

app = create_app()
print("BPM Projects API server was created")
print("TimeTracker API server was created")