Conditions | 8 |
Total Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
29 | def __call__(self, environ, start_response): |
||
30 | request = Request(environ) |
||
31 | |||
32 | def custom_start_response(status, headers, exc_info=None): |
||
33 | headers = ResponseHeaders(headers) |
||
34 | |||
35 | origin = request.headers.get('Origin') |
||
36 | origins = set(cfg.CONF.api.allow_origin) |
||
37 | |||
38 | # Build a list of the default allowed origins |
||
39 | public_api_url = cfg.CONF.auth.api_url |
||
40 | |||
41 | # Default gulp development server WebUI URL |
||
42 | origins.add('http://127.0.0.1:3000') |
||
43 | |||
44 | # By default WebUI simple http server listens on 8080 |
||
45 | origins.add('http://localhost:8080') |
||
46 | origins.add('http://127.0.0.1:8080') |
||
47 | |||
48 | if public_api_url: |
||
49 | # Public API URL |
||
50 | origins.add(public_api_url) |
||
51 | |||
52 | if origin: |
||
53 | if '*' in origins: |
||
54 | origin_allowed = '*' |
||
55 | else: |
||
56 | # See http://www.w3.org/TR/cors/#access-control-allow-origin-response-header |
||
57 | origin_allowed = origin if origin in origins else 'null' |
||
58 | else: |
||
59 | origin_allowed = list(origins)[0] |
||
60 | |||
61 | methods_allowed = ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'] |
||
62 | request_headers_allowed = ['Content-Type', 'Authorization', HEADER_ATTRIBUTE_NAME, |
||
63 | HEADER_API_KEY_ATTRIBUTE_NAME, REQUEST_ID_HEADER] |
||
64 | response_headers_allowed = ['Content-Type', 'X-Limit', 'X-Total-Count', |
||
65 | REQUEST_ID_HEADER] |
||
66 | |||
67 | headers['Access-Control-Allow-Origin'] = origin_allowed |
||
68 | headers['Access-Control-Allow-Methods'] = ','.join(methods_allowed) |
||
69 | headers['Access-Control-Allow-Headers'] = ','.join(request_headers_allowed) |
||
70 | headers['Access-Control-Expose-Headers'] = ','.join(response_headers_allowed) |
||
71 | |||
72 | return start_response(status, headers._items, exc_info) |
||
|
|||
73 | |||
74 | try: |
||
75 | return self.app(environ, custom_start_response) |
||
76 | except NotFoundException: |
||
77 | if request.method != 'options': |
||
78 | raise |
||
79 | |||
80 | return Response()(environ, custom_start_response) |
||
81 |
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: