From 64dad7f7c739716f747d6b1e0c648e411c96b030 Mon Sep 17 00:00:00 2001 From: IgorKalaidjan Date: Thu, 15 Nov 2018 13:12:20 +0200 Subject: [PATCH] Create custom endpoint for aggregate --- bigchaindb/tatau_backend/query.py | 8 +++++ bigchaindb/web/routes.py | 5 +++ bigchaindb/web/views/aggregate.py | 59 +++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 bigchaindb/tatau_backend/query.py create mode 100644 bigchaindb/web/views/aggregate.py diff --git a/bigchaindb/tatau_backend/query.py b/bigchaindb/tatau_backend/query.py new file mode 100644 index 00000000..cbc40a4a --- /dev/null +++ b/bigchaindb/tatau_backend/query.py @@ -0,0 +1,8 @@ +"""Tatau Query implementation for MongoDB""" + + +def aggregate(conn, pipeline, table='transactions'): + cursor = conn.run( + conn.collection(table).aggregate(pipeline)) + + return (obj for obj in cursor) \ No newline at end of file diff --git a/bigchaindb/web/routes.py b/bigchaindb/web/routes.py index 94d6e63a..a69d8e5d 100644 --- a/bigchaindb/web/routes.py +++ b/bigchaindb/web/routes.py @@ -12,6 +12,7 @@ from bigchaindb.web.views import ( transactions as tx, outputs, validators, + aggregate, ) @@ -40,8 +41,12 @@ ROUTES_API_V1 = [ r('validators/', validators.ValidatorsApi), ] +ROUTES_API_TATAU = [ + r('aggregate/', aggregate.AggregateListApi), +] API_SECTIONS = [ (None, [r('/', info.RootIndex)]), ('/api/v1/', ROUTES_API_V1), + ('/api/tatau/', ROUTES_API_TATAU), ] diff --git a/bigchaindb/web/views/aggregate.py b/bigchaindb/web/views/aggregate.py new file mode 100644 index 00000000..295ceb94 --- /dev/null +++ b/bigchaindb/web/views/aggregate.py @@ -0,0 +1,59 @@ +# Tatau + +"""This module provides the blueprint for some basic API endpoints. + +For more information please refer to the documentation: http://bigchaindb.com/http-api +""" +import logging +import types +import json +from bson import json_util + +from flask_restful import Resource +from flask import request, current_app + +from bigchaindb.backend.exceptions import OperationError +from bigchaindb.web.views.base import make_error +from bigchaindb.tatau_backend import query + + +logger = logging.getLogger(__name__) + + +class AggregateListApi(Resource): + + def post(self): + """API endpoint to aggregate. + + Args: + pipeline (str): Request of aggregate in body. + + Return: + A list of aggregate the query. + """ + table = request.args.get('table') + if not table: + table = 'transactions' + # return make_error(400, 'table cannot be empty') + try: + pipeline = request.get_json(force=True) + + except Exception as e: + return make_error(400, 'Json required') + + pool = current_app.config['bigchain_pool'] + + with pool() as bigchain: + def aggregate(obj, pipeline, table): + return query.aggregate(obj.connection, pipeline, table) + + bigchain.aggregate = types.MethodType(aggregate, bigchain) + data = bigchain.aggregate(pipeline, table) + + try: + return json.loads(json_util.dumps(data)) + except OperationError as e: + return make_error( + 400, + '({}): {}'.format(type(e).__name__, e) + )