GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — develop ( 7a2ea9...8041e8 )
by Plexxi
11:15 queued 05:58
created

RequestIDMiddleware.__call__()   B

Complexity

Conditions 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
c 0
b 0
f 0
dl 0
loc 23
rs 8.7972

1 Method

Rating   Name   Duplication   Size   Complexity  
A RequestIDMiddleware.custom_start_response() 0 8 2
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 uuid
17
18
from webob.headers import ResponseHeaders
19
20
from st2common.constants.api import REQUEST_ID_HEADER
21
from st2common.router import Request
22
23
24
class RequestIDMiddleware(object):
25
    def __init__(self, app):
26
        self.app = app
27
28
    def __call__(self, environ, start_response):
29
        # The middleware adds unique `X-Request-ID` header on the requests that don't have it and
30
        # modifies the responses to have the same exact header as their request. The middleware
31
        # helps us better track relation between request and response in places where it might not
32
        # be immediately obvious (like logs for example). In general, you want to place this header
33
        # as soon as possible to ensure it's present by the time it's needed. Certainly before
34
        # LoggingMiddleware which relies on this header.
35
        request = Request(environ)
36
37
        if not request.headers.get(REQUEST_ID_HEADER, None):
38
            req_id = str(uuid.uuid4())
39
            request.headers[REQUEST_ID_HEADER] = req_id
40
41
        def custom_start_response(status, headers, exc_info=None):
42
            headers = ResponseHeaders(headers)
43
44
            req_id_header = request.headers.get(REQUEST_ID_HEADER, None)
45
            if req_id_header:
46
                headers[REQUEST_ID_HEADER] = req_id_header
47
48
            return start_response(status, headers._items, exc_info)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _items was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
49
50
        return self.app(environ, custom_start_response)
51