From d957206a3b788961b21c8b650fe3217e2c64cbf5 Mon Sep 17 00:00:00 2001 From: woorea Date: Thu, 21 Feb 2013 22:28:20 +0100 Subject: [PATCH 1/5] added flask for v1 api --- billingstack/api/v1/__init__.py | 0 billingstack/api/v1/app.py | 46 +++++++++++++++++++++ billingstack/api/v1/resources.py | 70 ++++++++++++++++++++++++++++++++ bin/billingstack-api-v1 | 34 ++++++++++++++++ tools/pip-requires | 2 + 5 files changed, 152 insertions(+) create mode 100644 billingstack/api/v1/__init__.py create mode 100644 billingstack/api/v1/app.py create mode 100644 billingstack/api/v1/resources.py create mode 100644 bin/billingstack-api-v1 diff --git a/billingstack/api/v1/__init__.py b/billingstack/api/v1/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/billingstack/api/v1/app.py b/billingstack/api/v1/app.py new file mode 100644 index 0000000..bb53811 --- /dev/null +++ b/billingstack/api/v1/app.py @@ -0,0 +1,46 @@ +# -*- encoding: utf-8 -*- +# +# Copyright © 2013 Woorea Solutions, S.L +# +# Author: Luis Gervaso +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +"""Set up the API server application instance +""" + +import flask + +from billingstack.openstack.common import cfg + +from billingstack import storage +from billingstack.api.v1 import resources + +def make_app(conf, attach_storage=True): + app = flask.Flask('billingstack.api') + app.register_blueprint(resources.blueprint, url_prefix='/v1') + + @app.before_request + def attach_config(): + flask.request.cfg = conf + + if attach_storage: + @app.before_request + def attach_storage(): + storage_engine = storage.get_engine(conf['service:api'].storage_driver) + flask.request.storage_engine = storage_engine + flask.request.storage_conn = storage_engine.get_connection() + + return app + +# For documentation +app = make_app(cfg.CONF, attach_storage=False) \ No newline at end of file diff --git a/billingstack/api/v1/resources.py b/billingstack/api/v1/resources.py new file mode 100644 index 0000000..03238f7 --- /dev/null +++ b/billingstack/api/v1/resources.py @@ -0,0 +1,70 @@ +# -*- encoding: utf-8 -*- +# +# Copyright © 2013 Woorea Solutions, S.L +# +# Author: Luis Gervaso +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +import datetime + +import flask + +from billingstack.openstack.common import log +from billingstack.openstack.common import timeutils + +from billingstack import storage + + +LOG = log.getLogger(__name__) + + +blueprint = flask.Blueprint('v1', __name__, + template_folder='templates', + static_folder='static') + + +def request_wants_html(): + best = flask.request.accept_mimetypes \ + .best_match(['application/json', 'text/html']) + return best == 'text/html' and \ + flask.request.accept_mimetypes[best] > \ + flask.request.accept_mimetypes['application/json'] + + +def _get_metaquery(args): + return dict((k, v) + for (k, v) in args.iteritems() + if k.startswith('metadata.')) + +## APIs for working with meters. + + +@blueprint.route('/merchants') +def merchants_list(): + """Return a list of merchants. + """ + rq = flask.request + merchants = rq.storage_conn.merchants_list() + return flask.jsonify(meters=list(merchants)) + + +@blueprint.route('/merchants/') +def show_merchant(merchant_id): + """Return a merchant by ID + + :param merchant_id: The ID of the resource. + """ + rq = flask.request + merchant = rq.storage_conn.merchants_get() + return flask.jsonify(merchant) + diff --git a/bin/billingstack-api-v1 b/bin/billingstack-api-v1 new file mode 100644 index 0000000..c762524 --- /dev/null +++ b/bin/billingstack-api-v1 @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +# +# Copyright © 2013 Woorea Solutions, S.L +# +# Author: Luis Gervaso +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +"""Set up the development API server. +""" +import sys + +from billingstack.openstack.common import cfg + +from billingstack.api.v1 import app +from billingstack import service + + +if __name__ == '__main__': + # Parse config file and command line options, + # then configure logging. + service.prepare_service(sys.argv) + root = app.make_app(cfg.CONF) + root.run(host='0.0.0.0', port=cfg.CONF['service:api'].api_port) \ No newline at end of file diff --git a/tools/pip-requires b/tools/pip-requires index da7a866..0cb023b 100644 --- a/tools/pip-requires +++ b/tools/pip-requires @@ -8,3 +8,5 @@ anyjson>=0.2.4 pycountry iso8601 cliff +Flask==0.9 +http://tarballs.openstack.org/oslo-config/oslo-config-2013.1b3.tar.gz#egg=oslo-config \ No newline at end of file From 7aec5a4acb3bdbfde1f3624b35b3bf40453985ab Mon Sep 17 00:00:00 2001 From: woorea Date: Thu, 21 Feb 2013 22:41:40 +0100 Subject: [PATCH 2/5] added all http methods for merchants resource --- billingstack/api/v1/resources.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/billingstack/api/v1/resources.py b/billingstack/api/v1/resources.py index 03238f7..be6302e 100644 --- a/billingstack/api/v1/resources.py +++ b/billingstack/api/v1/resources.py @@ -54,17 +54,43 @@ def merchants_list(): """Return a list of merchants. """ rq = flask.request - merchants = rq.storage_conn.merchants_list() + merchants = rq.storage_conn.merchant_list() return flask.jsonify(meters=list(merchants)) +@blueprint.route('/merchants') +def merchants_create(data): + """Return a list of merchants. + """ + rq = flask.request + merchant = rq.storage_conn.merchant_add(data) + return flask.jsonify(merchant) + @blueprint.route('/merchants/') -def show_merchant(merchant_id): +def merchants_show(merchant_id): """Return a merchant by ID :param merchant_id: The ID of the resource. """ rq = flask.request - merchant = rq.storage_conn.merchants_get() + merchant = rq.storage_conn.merchants_get(merchant_id) + return flask.jsonify(merchant) + +@blueprint.route('/merchants/') +def merchants_update(merchant_id, data): + """Update a a merchant. + """ + rq = flask.request + merchant = rq.storage_conn.merchant_update(merchant_id, data) return flask.jsonify(merchant) + +@blueprint.route('/merchants/') +def merchants_delete(merchant_id): + """Deletes a merchant by ID + + :param merchant_id: The ID of the merchant. + """ + rq = flask.request + rq.storage_conn.merchant_delete(merchant_id) + \ No newline at end of file From d861d38c42aa6b337ef50e062cd7e8cfc202eee5 Mon Sep 17 00:00:00 2001 From: woorea Date: Thu, 21 Feb 2013 22:48:31 +0100 Subject: [PATCH 3/5] fixed requests json --- billingstack/api/v1/resources.py | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/billingstack/api/v1/resources.py b/billingstack/api/v1/resources.py index be6302e..ea25a50 100644 --- a/billingstack/api/v1/resources.py +++ b/billingstack/api/v1/resources.py @@ -15,15 +15,10 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -import datetime import flask from billingstack.openstack.common import log -from billingstack.openstack.common import timeutils - -from billingstack import storage - LOG = log.getLogger(__name__) @@ -40,33 +35,26 @@ def request_wants_html(): flask.request.accept_mimetypes[best] > \ flask.request.accept_mimetypes['application/json'] - -def _get_metaquery(args): - return dict((k, v) - for (k, v) in args.iteritems() - if k.startswith('metadata.')) - ## APIs for working with meters. - -@blueprint.route('/merchants') +@blueprint.route('/merchants', methods=('GET')) def merchants_list(): """Return a list of merchants. """ rq = flask.request merchants = rq.storage_conn.merchant_list() - return flask.jsonify(meters=list(merchants)) + return flask.jsonify(list(merchants)) -@blueprint.route('/merchants') -def merchants_create(data): +@blueprint.route('/merchants', methods=('POST')) +def merchants_create(): """Return a list of merchants. """ rq = flask.request - merchant = rq.storage_conn.merchant_add(data) + merchant = rq.storage_conn.merchant_add(rq.json) return flask.jsonify(merchant) -@blueprint.route('/merchants/') +@blueprint.route('/merchants/', methods=('GET')) def merchants_show(merchant_id): """Return a merchant by ID @@ -76,16 +64,16 @@ def merchants_show(merchant_id): merchant = rq.storage_conn.merchants_get(merchant_id) return flask.jsonify(merchant) -@blueprint.route('/merchants/') -def merchants_update(merchant_id, data): +@blueprint.route('/merchants/', methods=('PUT')) +def merchants_update(merchant_id): """Update a a merchant. """ rq = flask.request - merchant = rq.storage_conn.merchant_update(merchant_id, data) + merchant = rq.storage_conn.merchant_update(merchant_id, rq.json) return flask.jsonify(merchant) -@blueprint.route('/merchants/') +@blueprint.route('/merchants/', methods=('DELETE')) def merchants_delete(merchant_id): """Deletes a merchant by ID From fc8f6ccc79afe827f702533e14204ca3cc88c812 Mon Sep 17 00:00:00 2001 From: woorea Date: Thu, 21 Feb 2013 23:04:03 +0100 Subject: [PATCH 4/5] fix the method names in order to be as the storage api --- billingstack/api/v1/resources.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/billingstack/api/v1/resources.py b/billingstack/api/v1/resources.py index ea25a50..322d605 100644 --- a/billingstack/api/v1/resources.py +++ b/billingstack/api/v1/resources.py @@ -38,7 +38,7 @@ def request_wants_html(): ## APIs for working with meters. @blueprint.route('/merchants', methods=('GET')) -def merchants_list(): +def merchant_list(): """Return a list of merchants. """ rq = flask.request @@ -46,7 +46,7 @@ def merchants_list(): return flask.jsonify(list(merchants)) @blueprint.route('/merchants', methods=('POST')) -def merchants_create(): +def merchant_create(): """Return a list of merchants. """ rq = flask.request @@ -55,7 +55,7 @@ def merchants_create(): @blueprint.route('/merchants/', methods=('GET')) -def merchants_show(merchant_id): +def merchant_show(merchant_id): """Return a merchant by ID :param merchant_id: The ID of the resource. @@ -65,7 +65,7 @@ def merchants_show(merchant_id): return flask.jsonify(merchant) @blueprint.route('/merchants/', methods=('PUT')) -def merchants_update(merchant_id): +def merchant_update(merchant_id): """Update a a merchant. """ rq = flask.request @@ -74,7 +74,7 @@ def merchants_update(merchant_id): @blueprint.route('/merchants/', methods=('DELETE')) -def merchants_delete(merchant_id): +def merchant_delete(merchant_id): """Deletes a merchant by ID :param merchant_id: The ID of the merchant. From 3803797de266b3c0ed5c42445a5723005cb40305 Mon Sep 17 00:00:00 2001 From: woorea Date: Thu, 21 Feb 2013 23:29:51 +0100 Subject: [PATCH 5/5] added more resources --- billingstack/api/v1/resources.py | 618 ++++++++++++++++++++++++++++++- 1 file changed, 616 insertions(+), 2 deletions(-) diff --git a/billingstack/api/v1/resources.py b/billingstack/api/v1/resources.py index 322d605..f321146 100644 --- a/billingstack/api/v1/resources.py +++ b/billingstack/api/v1/resources.py @@ -34,8 +34,151 @@ def request_wants_html(): return best == 'text/html' and \ flask.request.accept_mimetypes[best] > \ flask.request.accept_mimetypes['application/json'] + +## APIs for working with languages. -## APIs for working with meters. +@blueprint.route('/languages', methods=('GET')) +def language_list(): + """Return a list of languages. + """ + rq = flask.request + languages = rq.storage_conn.language_list() + return flask.jsonify(list(languages)) + +@blueprint.route('/languages', methods=('POST')) +def language_create(): + """Return a list of languages. + """ + rq = flask.request + language = rq.storage_conn.language_add(rq.json) + return flask.jsonify(language) + + +@blueprint.route('/languages/', methods=('GET')) +def language_show(language_id): + """Return a language by ID + + :param language_id: The ID of the resource. + """ + rq = flask.request + language = rq.storage_conn.languages_get(language_id) + return flask.jsonify(language) + +@blueprint.route('/languages/', methods=('PUT')) +def language_update(language_id): + """Update a a language. + """ + rq = flask.request + language = rq.storage_conn.language_update(language_id, rq.json) + return flask.jsonify(language) + + +@blueprint.route('/languages/', methods=('DELETE')) +def language_delete(language_id): + """Deletes a language by ID + + :param language_id: The ID of the language. + """ + rq = flask.request + rq.storage_conn.language_delete(language_id) + + +## APIs for working with currencys. + +@blueprint.route('/currencys', methods=('GET')) +def currency_list(): + """Return a list of currencys. + """ + rq = flask.request + currencys = rq.storage_conn.currency_list() + return flask.jsonify(list(currencys)) + +@blueprint.route('/currencys', methods=('POST')) +def currency_create(): + """Return a list of currencys. + """ + rq = flask.request + currency = rq.storage_conn.currency_add(rq.json) + return flask.jsonify(currency) + + +@blueprint.route('/currencys/', methods=('GET')) +def currency_show(currency_id): + """Return a currency by ID + + :param currency_id: The ID of the resource. + """ + rq = flask.request + currency = rq.storage_conn.currencys_get(currency_id) + return flask.jsonify(currency) + +@blueprint.route('/currencys/', methods=('PUT')) +def currency_update(currency_id): + """Update a a currency. + """ + rq = flask.request + currency = rq.storage_conn.currency_update(currency_id, rq.json) + return flask.jsonify(currency) + + +@blueprint.route('/currencys/', methods=('DELETE')) +def currency_delete(currency_id): + """Deletes a currency by ID + + :param currency_id: The ID of the currency. + """ + rq = flask.request + rq.storage_conn.currency_delete(currency_id) + + +## APIs for working with payment gateway providers. + +@blueprint.route('/payment-gateway-providers', methods=('GET')) +def payment_gateway_provider_list(): + """Return a list of payment_gateway_providers. + """ + rq = flask.request + payment_gateway_providers = rq.storage_conn.payment_gateway_provider_list() + return flask.jsonify(list(payment_gateway_providers)) + +@blueprint.route('/payment-gateway-providers', methods=('POST')) +def payment_gateway_provider_create(): + """Return a list of payment_gateway_providers. + """ + rq = flask.request + payment_gateway_provider = rq.storage_conn.payment_gateway_provider_add(rq.json) + return flask.jsonify(payment_gateway_provider) + + +@blueprint.route('/payment-gateway-providers/', methods=('GET')) +def payment_gateway_provider_show(payment_gateway_provider_id): + """Return a payment_gateway_provider by ID + + :param payment_gateway_provider_id: The ID of the resource. + """ + rq = flask.request + payment_gateway_provider = rq.storage_conn.payment_gateway_providers_get(payment_gateway_provider_id) + return flask.jsonify(payment_gateway_provider) + +@blueprint.route('/payment-gateway-providers/', methods=('PUT')) +def payment_gateway_provider_update(payment_gateway_provider_id): + """Update a a payment_gateway_provider. + """ + rq = flask.request + payment_gateway_provider = rq.storage_conn.payment_gateway_provider_update(payment_gateway_provider_id, rq.json) + return flask.jsonify(payment_gateway_provider) + + +@blueprint.route('/payment-gateway-providers/', methods=('DELETE')) +def payment_gateway_provider_delete(payment_gateway_provider_id): + """Deletes a payment_gateway_provider by ID + + :param payment_gateway_provider_id: The ID of the payment_gateway_provider. + """ + rq = flask.request + rq.storage_conn.payment_gateway_provider_delete(payment_gateway_provider_id) + +## APIs for working with merchants. @blueprint.route('/merchants', methods=('GET')) def merchant_list(): @@ -81,4 +224,475 @@ def merchant_delete(merchant_id): """ rq = flask.request rq.storage_conn.merchant_delete(merchant_id) - \ No newline at end of file + +## APIs for working with merchant users. + +@blueprint.route('/merchants//users', methods=('GET')) +def user_list(merchant_id): + """Return a list of a merchant users. + """ + rq = flask.request + users = rq.storage_conn.user_list() + return flask.jsonify(list(users)) + +@blueprint.route('/merchants//users', methods=('POST')) +def user_create(merchant_id): + """Return a list of merchant users. + """ + rq = flask.request + user = rq.storage_conn.user_add(rq.json) + return flask.jsonify(user) + + +@blueprint.route('/merchants//users/', methods=('GET')) +def user_show(merchant_id, user_id): + """Return a merchant user by ID + + :param user_id: The ID of the resource. + """ + rq = flask.request + user = rq.storage_conn.users_get(user_id) + return flask.jsonify(user) + +@blueprint.route('/merchants//users/', methods=('PUT')) +def user_update(merchant_id, user_id): + """Update a merchant user. + """ + rq = flask.request + user = rq.storage_conn.user_update(user_id, rq.json) + return flask.jsonify(user) + + +@blueprint.route('/merchants//users/', methods=('DELETE')) +def user_delete(merchant_id, user_id): + """Deletes a merchant user by ID + + :param user_id: The ID of the user. + """ + rq = flask.request + rq.storage_conn.user_delete(user_id) + +## APIs for working with merchant products. + +@blueprint.route('/merchants//products', methods=('GET')) +def product_list(merchant_id): + """Return a list of a merchant products. + """ + rq = flask.request + products = rq.storage_conn.product_list() + return flask.jsonify(list(products)) + +@blueprint.route('/merchants//products', methods=('POST')) +def product_create(merchant_id): + """Return a list of merchant products. + """ + rq = flask.request + product = rq.storage_conn.product_add(rq.json) + return flask.jsonify(product) + + +@blueprint.route('/merchants//products/', methods=('GET')) +def product_show(merchant_id, product_id): + """Return a merchant product by ID + + :param product_id: The ID of the resource. + """ + rq = flask.request + product = rq.storage_conn.products_get(product_id) + return flask.jsonify(product) + +@blueprint.route('/merchants//products/', methods=('PUT')) +def product_update(merchant_id, product_id): + """Update a merchant product. + """ + rq = flask.request + product = rq.storage_conn.product_update(product_id, rq.json) + return flask.jsonify(product) + + +@blueprint.route('/merchants//products/', methods=('DELETE')) +def product_delete(merchant_id, product_id): + """Deletes a merchant product by ID + + :param product_id: The ID of the product. + """ + rq = flask.request + rq.storage_conn.product_delete(product_id) + +## APIs for working with merchant plans. + +@blueprint.route('/merchants//plans', methods=('GET')) +def plan_list(merchant_id): + """Return a list of a merchant plans. + """ + rq = flask.request + plans = rq.storage_conn.plan_list() + return flask.jsonify(list(plans)) + +@blueprint.route('/merchants//plans', methods=('POST')) +def plan_create(merchant_id): + """Return a list of merchant plans. + """ + rq = flask.request + plan = rq.storage_conn.plan_add(rq.json) + return flask.jsonify(plan) + + +@blueprint.route('/merchants//plans/', methods=('GET')) +def plan_show(merchant_id, plan_id): + """Return a merchant plan by ID + + :param plan_id: The ID of the resource. + """ + rq = flask.request + plan = rq.storage_conn.plans_get(plan_id) + return flask.jsonify(plan) + +@blueprint.route('/merchants//plans/', methods=('PUT')) +def plan_update(merchant_id, plan_id): + """Update a merchant plan. + """ + rq = flask.request + plan = rq.storage_conn.plan_update(plan_id, rq.json) + return flask.jsonify(plan) + + +@blueprint.route('/merchants//plans/', methods=('DELETE')) +def plan_delete(merchant_id, plan_id): + """Deletes a merchant plan by ID + + :param plan_id: The ID of the plan. + """ + rq = flask.request + rq.storage_conn.plan_delete(plan_id) + + +## APIs for working with merchant plan items. + +@blueprint.route('/merchants//plan//items', methods=('GET')) +def plan_item_list(merchant_id): + """Return a list of a merchant plan_items. + """ + rq = flask.request + plan_items = rq.storage_conn.plan_item_list() + return flask.jsonify(list(plan_items)) + +@blueprint.route('/merchants//plan//items', methods=('POST')) +def plan_item_create(merchant_id): + """Return a list of merchant plan_items. + """ + rq = flask.request + plan_item = rq.storage_conn.plan_item_add(rq.json) + return flask.jsonify(plan_item) + + +@blueprint.route('/merchants//plan//items/', methods=('GET')) +def plan_item_show(merchant_id, plan_item_id): + """Return a merchant plan_item by ID + + :param plan_item_id: The ID of the resource. + """ + rq = flask.request + plan_item = rq.storage_conn.plan_items_get(plan_item_id) + return flask.jsonify(plan_item) + +@blueprint.route('/merchants//plan//items/', methods=('PUT')) +def plan_item_update(merchant_id, plan_item_id): + """Update a merchant plan_item. + """ + rq = flask.request + plan_item = rq.storage_conn.plan_item_update(plan_item_id, rq.json) + return flask.jsonify(plan_item) + + +@blueprint.route('/merchants//plan//items/', methods=('DELETE')) +def plan_item_delete(merchant_id, plan_item_id): + """Deletes a merchant plan_item by ID + + :param plan_item_id: The ID of the plan_item. + """ + rq = flask.request + rq.storage_conn.plan_item_delete(plan_item_id) + +## APIs for working with merchant subscriptions. + +@blueprint.route('/merchants//subscriptions', methods=('GET')) +def subscription_list(merchant_id): + """Return a list of a merchant subscriptions. + """ + rq = flask.request + subscriptions = rq.storage_conn.subscription_list() + return flask.jsonify(list(subscriptions)) + +@blueprint.route('/merchants//subscriptions', methods=('POST')) +def subscription_create(merchant_id): + """Return a list of merchant subscriptions. + """ + rq = flask.request + subscription = rq.storage_conn.subscription_add(rq.json) + return flask.jsonify(subscription) + + +@blueprint.route('/merchants//subscriptions/', methods=('GET')) +def subscription_show(merchant_id, subscription_id): + """Return a merchant subscription by ID + + :param subscription_id: The ID of the resource. + """ + rq = flask.request + subscription = rq.storage_conn.subscriptions_get(subscription_id) + return flask.jsonify(subscription) + +@blueprint.route('/merchants//subscriptions/', methods=('PUT')) +def subscription_update(merchant_id, subscription_id): + """Update a merchant subscription. + """ + rq = flask.request + subscription = rq.storage_conn.subscription_update(subscription_id, rq.json) + return flask.jsonify(subscription) + + +@blueprint.route('/merchants//subscriptions/', methods=('DELETE')) +def subscription_delete(merchant_id, subscription_id): + """Deletes a merchant subscription by ID + + :param subscription_id: The ID of the subscription. + """ + rq = flask.request + rq.storage_conn.subscription_delete(subscription_id) + +## APIs for working with merchant invoices. + +@blueprint.route('/merchants//invoices', methods=('GET')) +def invoice_list(merchant_id): + """Return a list of a merchant invoices. + """ + rq = flask.request + invoices = rq.storage_conn.invoice_list() + return flask.jsonify(list(invoices)) + +@blueprint.route('/merchants//invoices', methods=('POST')) +def invoice_create(merchant_id): + """Return a list of merchant invoices. + """ + rq = flask.request + invoice = rq.storage_conn.invoice_add(rq.json) + return flask.jsonify(invoice) + + +@blueprint.route('/merchants//invoices/', methods=('GET')) +def invoice_show(merchant_id, invoice_id): + """Return a merchant invoice by ID + + :param invoice_id: The ID of the resource. + """ + rq = flask.request + invoice = rq.storage_conn.invoices_get(invoice_id) + return flask.jsonify(invoice) + +@blueprint.route('/merchants//invoices/', methods=('PUT')) +def invoice_update(merchant_id, invoice_id): + """Update a merchant invoice. + """ + rq = flask.request + invoice = rq.storage_conn.invoice_update(invoice_id, rq.json) + return flask.jsonify(invoice) + + +@blueprint.route('/merchants//invoices/', methods=('DELETE')) +def invoice_delete(merchant_id, invoice_id): + """Deletes a merchant invoice by ID + + :param invoice_id: The ID of the invoice. + """ + rq = flask.request + rq.storage_conn.invoice_delete(invoice_id) + +## APIs for working with merchant payment gateways. + +@blueprint.route('/merchants//payment-gateways', methods=('GET')) +def payment_gateway_list(merchant_id): + """Return a list of a merchant payment_gateways. + """ + rq = flask.request + payment_gateways = rq.storage_conn.payment_gateway_list() + return flask.jsonify(list(payment_gateways)) + +@blueprint.route('/merchants//payment-gateways', methods=('POST')) +def payment_gateway_create(merchant_id): + """Return a list of merchant payment_gateways. + """ + rq = flask.request + payment_gateway = rq.storage_conn.payment_gateway_add(rq.json) + return flask.jsonify(payment_gateway) + + +@blueprint.route('/merchants//payment-gateways/', methods=('GET')) +def payment_gateway_show(merchant_id, payment_gateway_id): + """Return a merchant payment_gateway by ID + + :param payment_gateway_id: The ID of the resource. + """ + rq = flask.request + payment_gateway = rq.storage_conn.payment_gateways_get(payment_gateway_id) + return flask.jsonify(payment_gateway) + +@blueprint.route('/merchants//payment-gateways/', methods=('PUT')) +def payment_gateway_update(merchant_id, payment_gateway_id): + """Update a merchant payment_gateway. + """ + rq = flask.request + payment_gateway = rq.storage_conn.payment_gateway_update(payment_gateway_id, rq.json) + return flask.jsonify(payment_gateway) + + +@blueprint.route('/merchants//payment-gateways/', methods=('DELETE')) +def payment_gateway_delete(merchant_id, payment_gateway_id): + """Deletes a merchant payment_gateway by ID + + :param payment_gateway_id: The ID of the payment_gateway. + """ + rq = flask.request + rq.storage_conn.payment_gateway_delete(payment_gateway_id) + + +## APIs for working with merchant customers. + +@blueprint.route('/merchants//customers', methods=('GET')) +def customer_list(merchant_id): + """Return a list of a merchant customers. + """ + rq = flask.request + customers = rq.storage_conn.customer_list() + return flask.jsonify(list(customers)) + +@blueprint.route('/merchants//customers', methods=('POST')) +def customer_create(merchant_id): + """Return a list of merchant customers. + """ + rq = flask.request + customer = rq.storage_conn.customer_add(rq.json) + return flask.jsonify(customer) + + +@blueprint.route('/merchants//customers/', methods=('GET')) +def customer_show(merchant_id, customer_id): + """Return a merchant customer by ID + + :param customer_id: The ID of the resource. + """ + rq = flask.request + customer = rq.storage_conn.customers_get(customer_id) + return flask.jsonify(customer) + +@blueprint.route('/merchants//customers/', methods=('PUT')) +def customer_update(merchant_id, customer_id): + """Update a merchant customer. + """ + rq = flask.request + customer = rq.storage_conn.customer_update(customer_id, rq.json) + return flask.jsonify(customer) + + +@blueprint.route('/merchants//customers/', methods=('DELETE')) +def customer_delete(merchant_id, customer_id): + """Deletes a merchant customer by ID + + :param customer_id: The ID of the customer. + """ + rq = flask.request + rq.storage_conn.customer_delete(customer_id) + +## APIs for working with customers users. + +@blueprint.route('/merchants//customers//users', methods=('GET')) +def customer_user_list(merchant_id): + """Return a list of a merchant users. + """ + rq = flask.request + users = rq.storage_conn.user_list() + return flask.jsonify(list(users)) + +@blueprint.route('/merchants//customers//users', methods=('POST')) +def customer_user_create(merchant_id): + """Return a list of merchant users. + """ + rq = flask.request + user = rq.storage_conn.user_add(rq.json) + return flask.jsonify(user) + + +@blueprint.route('/merchants//customers//users/', methods=('GET')) +def customer_user_show(merchant_id, user_id): + """Return a merchant user by ID + + :param user_id: The ID of the resource. + """ + rq = flask.request + user = rq.storage_conn.users_get(user_id) + return flask.jsonify(user) + +@blueprint.route('/merchants//customers//users/', methods=('PUT')) +def customer_user_update(merchant_id, user_id): + """Update a merchant user. + """ + rq = flask.request + user = rq.storage_conn.user_update(user_id, rq.json) + return flask.jsonify(user) + + +@blueprint.route('/merchants//customers//users/', methods=('DELETE')) +def customer_user_delete(merchant_id, user_id): + """Deletes a merchant user by ID + + :param user_id: The ID of the user. + """ + rq = flask.request + rq.storage_conn.user_delete(user_id) + +## APIs for working with customers payment_methods. + +@blueprint.route('/merchants//customers//payment-methods', methods=('GET')) +def customer_payment_method_list(merchant_id): + """Return a list of a merchant payment_methods. + """ + rq = flask.request + payment_methods = rq.storage_conn.payment_method_list() + return flask.jsonify(list(payment_methods)) + +@blueprint.route('/merchants//customers//payment-methods', methods=('POST')) +def customer_payment_method_create(merchant_id): + """Return a list of merchant payment_methods. + """ + rq = flask.request + payment_method = rq.storage_conn.payment_method_add(rq.json) + return flask.jsonify(payment_method) + + +@blueprint.route('/merchants//customers//payment-methods/', methods=('GET')) +def customer_payment_method_show(merchant_id, payment_method_id): + """Return a merchant payment_method by ID + + :param payment_method_id: The ID of the resource. + """ + rq = flask.request + payment_method = rq.storage_conn.payment_methods_get(payment_method_id) + return flask.jsonify(payment_method) + +@blueprint.route('/merchants//customers//payment-methods/', methods=('PUT')) +def customer_payment_method_update(merchant_id, payment_method_id): + """Update a merchant payment_method. + """ + rq = flask.request + payment_method = rq.storage_conn.payment_method_update(payment_method_id, rq.json) + return flask.jsonify(payment_method) + + +@blueprint.route('/merchants//customers//payment-methods/', methods=('DELETE')) +def customer_payment_method_delete(merchant_id, payment_method_id): + """Deletes a merchant payment_method by ID + + :param payment_method_id: The ID of the payment_method. + """ + rq = flask.request + rq.storage_conn.payment_method_delete(payment_method_id) \ No newline at end of file