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
Next Next commit
Fixes #10 Add CLI commands to generate API definition
  • Loading branch information
EliuX committed Mar 11, 2020
commit 5f521dd3eef70426152ee2df45bb8d8329b452dc
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 verify useful for you. You
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you meant

There are available commands that can be very useful for you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

LOL, yes, thanks for the catch.

can check them out by running

```angular2
Copy link
Contributor

Choose a reason for hiding this comment

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

👀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

LoL, thanks, I will fix it.

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
Copy link
Contributor

Choose a reason for hiding this comment

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

👀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep

python cli.py gen_swagger_json -f ~/Downloads/swagger.json
```
52 changes: 52 additions & 0 deletions cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/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 time_tracker_api import create_app
from flask_script import Manager
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')
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'):
""" 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}}')
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be great if we can send http://localhost as a parameter to the cli.py file. I think by default it can be localhost.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By default it is actually localhost:5000 for development and localhost:8000 when it is using a wsgi server, like gunicorn. The idea behind setting a postman env variable is that you can set that env variable to whatever you want based on the environment you will be working at.
Nonetheless, I will create a param, so you can override the default param which by default will be localhost and everyone is happy :)

save_data(parsed_json, filename)


def save_data(data, filename):
Copy link
Contributor

@Angeluz-07 Angeluz-07 Mar 12, 2020

Choose a reason for hiding this comment

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

Could we have type hints here? Not only for parameters but for return also i.e def function(..) -> None.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, sure.

""" 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")