-
Notifications
You must be signed in to change notification settings - Fork 0
Add CLI commands to generate API definition #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The head ref may contain hidden characters: "feature/create-cli-generate-file-with-api-def\u00C2#10"
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,3 +20,5 @@ __pycache__/ | |
|
|
||
| # Files generated for development | ||
| .env | ||
| timetracker-api-postman-collection.json | ||
| swagger.json | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| 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 | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| print("****************") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it necessary to have these prints before the import section ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}}') | ||
|
||
| save_data(parsed_json, filename) | ||
|
|
||
|
|
||
| def save_data(data, filename): | ||
|
||
| """ 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() | ||
| 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 |
There was a problem hiding this comment.
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 was a problem hiding this comment.
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.