import json

from flask import request, Response
from werkzeug.exceptions import BadRequest
from werkzeug.test import EnvironBuilder


class Aggregator(object):

    def __init__(self, app=None, endpoint=None):
        self.url_map = {}
        self.endpoint = endpoint or "/aggregate"
        if app:
            self.init_app(app)

    def init_app(self, app):
        self.app = app
        self.app.add_url_rule(self.endpoint, view_func=self.post, methods=["POST"])

    def get_response(self, route):
        query_string = ""
        if '?' in route:
            route, query_string = route.split('?', 1)

        builder = EnvironBuilder(path=route, query_string=query_string)
        self.app.request_context(builder.get_environ()).push()
        return self.app.dispatch_request()

    def post(self):
        try:
            data = request.data.decode('utf-8')
            routes = json.loads(data)
            if not isinstance(routes, list):
                raise TypeError
        except (ValueError, TypeError) as e:
            raise BadRequest("Can't get requests list.")

        def __generate():
            data = None
            for route in routes:
                yield data + ', ' if data else '{'
                response = self.get_response(route)
                json_response = json.dumps(response)
                data = '"{}": {}'.format(route, json_response)
            yield data + '}'

        return Response(__generate(), mimetype='application/json')

@@ -368,36 +368,8 @@
     
-self.app = app%0A        self.
 app.
@@ -433,16 +433,64 @@
 %5B%22POST%22%5D
+,%0A                         defaults=%7B%22app%22: app%7D
 )%0A%0A    d
@@ -510,16 +510,21 @@
 se(self,
+ app,
  route):
@@ -710,21 +710,16 @@
         
-self.
 app.requ
@@ -775,21 +775,16 @@
  return 
-self.
 app.disp
@@ -816,16 +816,21 @@
 ost(self
+, app
 ):%0A     
@@ -1282,16 +1282,21 @@
 esponse(
+app, 
 route)%0A 

Support multiple Flask apps on the same Aggregator instance