-
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
Merged
EliuX
merged 2 commits into
master
from
feature/create-cli-generate-file-with-api-defÂ#10
Mar 12, 2020
The head ref may contain hidden characters: "feature/create-cli-generate-file-with-api-def\u00C2#10"
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| print("****************") | ||
| 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() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is it necessary to have these prints before the import section ?
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.
Yes, because I wanted to know whether the CLI file is actually running before any error inside it happens.