Skip to content

Commit 77eacb8

Browse files
committed
add cli
1 parent e797cc5 commit 77eacb8

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-3
lines changed

implementation/betterapis/__init__.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import connexion
2-
import logging
32
from connexion.resolver import RestyResolver
43
from flask_sqlalchemy import SQLAlchemy
54

6-
logging.basicConfig(level=logging.DEBUG)
5+
# Uncomment to see import-time debug logging:
6+
#import logging
7+
#logging.basicConfig(level=logging.DEBUG)
78

89
api = connexion.FlaskApp(__name__, specification_dir='specs/')
9-
api.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
10+
api.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/betterapis.db'
11+
api.app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
1012
db = SQLAlchemy(api.app)
1113
api.add_api('betterapis.yaml', resolver=RestyResolver('betterapis.controllers'))
1214

implementation/betterapis/cli.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import click
2+
import logging
3+
from . import api, db
4+
5+
6+
@click.group()
7+
@click.option('-l', '--log-level', default='DEBUG', type=click.Choice(
8+
['DEBUG', 'INFO', 'WARN', 'ERROR']))
9+
def betterapis_cli(log_level):
10+
level = getattr(logging, log_level)
11+
logging.basicConfig(level=level)
12+
13+
@betterapis_cli.command()
14+
@click.option('-p', '--port', default=8080, type=click.IntRange(1,65535))
15+
@click.option('--debug/--no-debug', default=True)
16+
def run(port, debug):
17+
api.run(port=port, debug=debug)
18+
click.echo('Goodbye!')
19+
20+
@betterapis_cli.command(name='reset-db')
21+
def reset_db():
22+
click.confirm('This will ERASE the application databsase. Are you sure?',
23+
abort=True)
24+
click.echo('Dropping all tables...')
25+
db.drop_all()
26+
click.echo('Creating all tables...')
27+
db.create_all()
28+
click.echo('Database reset!')
29+
30+
@betterapis_cli.command(name='create-tables')
31+
def create_tables():
32+
click.echo('Creating all tables...')
33+
db.create_all()
34+
click.echo('Tables created.')
35+
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from flask import jsonify, url_for
2+
3+
from betterapis import api
4+
5+
6+
def get():
7+
# Get all of the mapped 'search' endpoints and list them as links
8+
links = {rule.rule.lstrip('/'): url_for(rule.endpoint, _external=True) \
9+
for rule in api.app.url_map.iter_rules() \
10+
if rule.endpoint.endswith('_search')}
11+
return jsonify(links=links)

implementation/betterapis/specs/betterapis.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ produces:
2222
- application/json
2323

2424
paths:
25+
/:
26+
get:
27+
summary: Get a list of endpoints
28+
description: Lists all API endpoints with links
29+
responses:
30+
200:
31+
description: Returns a list of endpoints.
2532
/talks:
2633
get:
2734
summary: Get a list of talks

implementation/setup.py

+5
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@
1010
'connexion',
1111
'flask_sqlalchemy'
1212
],
13+
entry_points={
14+
'console_scripts':[
15+
'betterapis=betterapis.cli:betterapis_cli',
16+
],
17+
}
1318
)

0 commit comments

Comments
 (0)