Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Close #10 Apply review suggestions
  • Loading branch information
EliuX committed Mar 12, 2020
commit 85589cfcb5087151c85e355cc421537a2619df43
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ a link to the swagger.json with the definition of the api.

## CLI

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

```angular2
```
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.

```angular2
```
python cli.py gen_swagger_json -f ~/Downloads/swagger.json
```
28 changes: 21 additions & 7 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,48 @@
import os

from flask import json
from time_tracker_api import create_app
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', help='Path of the swagger file. By default swagger.json')
@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')
def gen_postman_collection(filename='timetracker-api-postman-collection.json'):
@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)
parsed_json = postman_collection_json_data.replace("http://localhost", '{{timetracker_api_host}}')
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, filename):
def save_data(data: str, filename: str) -> None:
""" Save text content to a file """
if filename:
try:
Expand Down