def on_post(self, req, res):
        helpers.validate_authorization(req, self._default_authorized_roles)

        stream_definition = helpers.read_json_msg_body(req)

        self._validate_stream_definition(stream_definition)

        tenant_id = helpers.get_tenant_id(req)
        name = get_query_stream_definition_name(stream_definition)
        description = get_query_stream_definition_description(
            stream_definition)
        select = stream_definition['select']
        for s in select:
            if 'traits' in s:
                s['traits']['_tenant_id'] = tenant_id
            else:
                s['traits'] = {'_tenant_id': tenant_id}

        group_by = stream_definition['group_by']
        fire_criteria = stream_definition['fire_criteria']
        expiration = stream_definition['expiration']
        fire_actions = get_query_stream_definition_fire_actions(
            stream_definition)
        expire_actions = get_query_stream_definition_expire_actions(
            stream_definition)

        result = self._stream_definition_create(tenant_id, name, description,
                                                select, group_by,
                                                fire_criteria, expiration,
                                                fire_actions, expire_actions)

        helpers.add_links_to_resource(result, req.uri)
        res.body = helpers.dumpit_utf8(result)
        res.status = falcon.HTTP_201
    def _stream_definition_list(self, tenant_id, name, req_uri,
                                offset, limit):

        stream_definition_rows = (
            self._stream_definitions_repo.get_stream_definitions(
                tenant_id, name, offset, limit))
        result = []
        for stream_definition_row in stream_definition_rows:
            sd = self._build_stream_definition_show_result(
                stream_definition_row)
            helpers.add_links_to_resource(sd, req_uri)
            result.append(sd)

        result = helpers.paginate(result, req_uri)

        return result
    def on_get(self, req, res, event_id=None):
        helpers.validate_authorization(req, self._default_authorized_roles)
        tenant_id = helpers.get_tenant_id(req)

        if event_id:
            helpers.validate_authorization(req, self._default_authorized_roles)
            tenant_id = helpers.get_tenant_id(req)
            result = self._list_event(tenant_id, event_id)
            helpers.add_links_to_resource(result[0], re.sub("/" + event_id, "", req.uri))
            res.body = helpers.dumpit_utf8(result)
            res.status = falcon.HTTP_200
        else:
            offset = helpers.normalize_offset(helpers.get_query_param(req, "offset"))
            limit = helpers.get_query_param(req, "limit")

            result = self._list_events(tenant_id, req.uri, offset, limit)
            res.body = helpers.dumpit_utf8(result)
            res.status = falcon.HTTP_200
 def on_get(self, req, res, transform_id=None):
     if transform_id:
         helpers.validate_authorization(req, self._default_authorized_roles)
         tenant_id = helpers.get_tenant_id(req)
         result = self._list_transform(tenant_id, transform_id, req.uri)
         helpers.add_links_to_resource(
             result, re.sub('/' + transform_id, '', req.uri))
         res.body = json.dumps(result, cls=MyEncoder)
         res.status = falcon.HTTP_200
     else:
         helpers.validate_authorization(req, self._default_authorized_roles)
         tenant_id = helpers.get_tenant_id(req)
         limit = helpers.get_query_param(req, 'limit')
         offset = helpers.normalize_offset(helpers.get_query_param(
             req,
             'offset'))
         result = self._list_transforms(tenant_id, limit, offset, req.uri)
         res.body = json.dumps(result, cls=MyEncoder)
         res.status = falcon.HTTP_200
    def on_patch(self, req, res, stream_id):
        helpers.validate_authorization(req, self._default_authorized_roles)

        stream_definition = helpers.read_json_msg_body(req)

        tenant_id = helpers.get_tenant_id(req)

        name = get_query_stream_definition_name(stream_definition, return_none=True)
        description = get_query_stream_definition_description(
            stream_definition, return_none=True)
        select = get_query_stream_definition_select(stream_definition, return_none=True)
        if select:
            for s in select:
                if 'traits' in s:
                    s['traits']['_tenant_id'] = tenant_id
                else:
                    s['traits'] = {'_tenant_id': tenant_id}

        group_by = get_query_stream_definition_group_by(stream_definition, return_none=True)
        fire_criteria = get_query_stream_definition_fire_criteria(stream_definition, return_none=True)
        expiration = get_query_stream_definition_expiration(stream_definition, return_none=True)
        fire_actions = get_query_stream_definition_fire_actions(
            stream_definition, return_none=True)
        expire_actions = get_query_stream_definition_expire_actions(
            stream_definition, return_none=True)

        result = self._stream_definition_patch(tenant_id,
                                               stream_id,
                                               name,
                                               description,
                                               select,
                                               group_by,
                                               fire_criteria,
                                               expiration,
                                               fire_actions,
                                               expire_actions)

        helpers.add_links_to_resource(result, req.uri)
        res.body = helpers.dumpit_utf8(result)
        res.status = falcon.HTTP_201
    def on_get(self, req, res, stream_id=None):
        if stream_id:
            helpers.validate_authorization(req, self._default_authorized_roles)
            tenant_id = helpers.get_tenant_id(req)

            result = self._stream_definition_show(tenant_id, stream_id)

            helpers.add_links_to_resource(
                result, re.sub('/' + stream_id, '', req.uri))
            res.body = helpers.dumpit_utf8(result)
            res.status = falcon.HTTP_200
        else:
            helpers.validate_authorization(req, self._default_authorized_roles)
            tenant_id = helpers.get_tenant_id(req)
            name = helpers.get_query_name(req)
            offset = helpers.normalize_offset(
                helpers.get_query_param(req, 'offset'))
            limit = helpers.get_query_param(req, 'limit')
            result = self._stream_definition_list(tenant_id, name,
                                                  req.uri, offset, limit)

            res.body = helpers.dumpit_utf8(result)
            res.status = falcon.HTTP_200