Skip to content

Commit d957206

Browse files
committed
added flask for v1 api
1 parent 09ba14c commit d957206

5 files changed

Lines changed: 152 additions & 0 deletions

File tree

billingstack/api/v1/__init__.py

Whitespace-only changes.

billingstack/api/v1/app.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# -*- encoding: utf-8 -*-
2+
#
3+
# Copyright © 2013 Woorea Solutions, S.L
4+
#
5+
# Author: Luis Gervaso <luis@woorea.es>
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
8+
# not use this file except in compliance with the License. You may obtain
9+
# a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
"""Set up the API server application instance
19+
"""
20+
21+
import flask
22+
23+
from billingstack.openstack.common import cfg
24+
25+
from billingstack import storage
26+
from billingstack.api.v1 import resources
27+
28+
def make_app(conf, attach_storage=True):
29+
app = flask.Flask('billingstack.api')
30+
app.register_blueprint(resources.blueprint, url_prefix='/v1')
31+
32+
@app.before_request
33+
def attach_config():
34+
flask.request.cfg = conf
35+
36+
if attach_storage:
37+
@app.before_request
38+
def attach_storage():
39+
storage_engine = storage.get_engine(conf['service:api'].storage_driver)
40+
flask.request.storage_engine = storage_engine
41+
flask.request.storage_conn = storage_engine.get_connection()
42+
43+
return app
44+
45+
# For documentation
46+
app = make_app(cfg.CONF, attach_storage=False)

billingstack/api/v1/resources.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# -*- encoding: utf-8 -*-
2+
#
3+
# Copyright © 2013 Woorea Solutions, S.L
4+
#
5+
# Author: Luis Gervaso <luis@woorea.es>
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
8+
# not use this file except in compliance with the License. You may obtain
9+
# a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
import datetime
19+
20+
import flask
21+
22+
from billingstack.openstack.common import log
23+
from billingstack.openstack.common import timeutils
24+
25+
from billingstack import storage
26+
27+
28+
LOG = log.getLogger(__name__)
29+
30+
31+
blueprint = flask.Blueprint('v1', __name__,
32+
template_folder='templates',
33+
static_folder='static')
34+
35+
36+
def request_wants_html():
37+
best = flask.request.accept_mimetypes \
38+
.best_match(['application/json', 'text/html'])
39+
return best == 'text/html' and \
40+
flask.request.accept_mimetypes[best] > \
41+
flask.request.accept_mimetypes['application/json']
42+
43+
44+
def _get_metaquery(args):
45+
return dict((k, v)
46+
for (k, v) in args.iteritems()
47+
if k.startswith('metadata.'))
48+
49+
## APIs for working with meters.
50+
51+
52+
@blueprint.route('/merchants')
53+
def merchants_list():
54+
"""Return a list of merchants.
55+
"""
56+
rq = flask.request
57+
merchants = rq.storage_conn.merchants_list()
58+
return flask.jsonify(meters=list(merchants))
59+
60+
61+
@blueprint.route('/merchants/<merchant_id>')
62+
def show_merchant(merchant_id):
63+
"""Return a merchant by ID
64+
65+
:param merchant_id: The ID of the resource.
66+
"""
67+
rq = flask.request
68+
merchant = rq.storage_conn.merchants_get()
69+
return flask.jsonify(merchant)
70+

bin/billingstack-api-v1

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
#
4+
# Copyright © 2013 Woorea Solutions, S.L
5+
#
6+
# Author: Luis Gervaso <luis@woorea.es>
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
9+
# not use this file except in compliance with the License. You may obtain
10+
# a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17+
# License for the specific language governing permissions and limitations
18+
# under the License.
19+
"""Set up the development API server.
20+
"""
21+
import sys
22+
23+
from billingstack.openstack.common import cfg
24+
25+
from billingstack.api.v1 import app
26+
from billingstack import service
27+
28+
29+
if __name__ == '__main__':
30+
# Parse config file and command line options,
31+
# then configure logging.
32+
service.prepare_service(sys.argv)
33+
root = app.make_app(cfg.CONF)
34+
root.run(host='0.0.0.0', port=cfg.CONF['service:api'].api_port)

tools/pip-requires

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ anyjson>=0.2.4
88
pycountry
99
iso8601
1010
cliff
11+
Flask==0.9
12+
http://tarballs.openstack.org/oslo-config/oslo-config-2013.1b3.tar.gz#egg=oslo-config

0 commit comments

Comments
 (0)