Test Failed
Push — master ( e380d0...f5671d )
by W
02:58
created

st2stream/st2stream/controllers/v1/stream.py (1 issue)

1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import six
17
18
from st2common import log as logging
19
from st2common.router import Response
20
from st2common.util.jsonify import json_encode
21
from st2common.stream.listener import get_listener
22
23
__all__ = [
24
    'StreamController'
25
]
26
27
LOG = logging.getLogger(__name__)
28
29
DEFAULT_EVENTS_WHITELIST = [
30
    'st2.announcement__*',
31
32
    'st2.execution__create',
33
    'st2.execution__update',
34
    'st2.execution__delete',
35
36
    'st2.liveaction__create',
37
    'st2.liveaction__update',
38
    'st2.liveaction__delete',
39
]
40
41
42
def format(gen):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in format.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
43
    message = '''event: %s\ndata: %s\n\n'''
44
45
    for pack in gen:
46
        if not pack:
47
            # Note: gunicorn wsgi handler expect bytes, not unicode
48
            yield six.binary_type(b'\n')
49
        else:
50
            (event, body) = pack
51
            # Note: gunicorn wsgi handler expect bytes, not unicode
52
            yield six.binary_type((message % (event, json_encode(body,
53
                                                                 indent=None))).encode('utf-8'))
54
55
56
class StreamController(object):
57
    def get_all(self, events=None, action_refs=None, execution_ids=None, requester_user=None):
58
        events = events if events else DEFAULT_EVENTS_WHITELIST
59
        action_refs = action_refs if action_refs else None
60
        execution_ids = execution_ids if execution_ids else None
61
62
        def make_response():
63
            listener = get_listener(name='stream')
64
            app_iter = format(listener.generator(events=events, action_refs=action_refs,
65
                                                 execution_ids=execution_ids))
66
            res = Response(content_type='text/event-stream', app_iter=app_iter)
67
            return res
68
69
        stream = make_response()
70
71
        return stream
72
73
74
stream_controller = StreamController()
75