Completed
Pull Request — master (#2334)
by Edward
06:02
created

st2api.controllers.exp.ResourceController._get_all()   F

Complexity

Conditions 17

Size

Total Lines 82

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 82
rs 2.0647
cc 17

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

Complex classes like st2api.controllers.exp.ResourceController._get_all() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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
from pecan import (abort, expose, request, rest)
17
from six.moves import http_client
18
19
# from st2api.controllers import resource
20
from st2common import log as logging
21
22
LOG = logging.getLogger(__name__)
23
24
25
class ActionAliasController(rest.RestController):
26
27
    @expose()
28
    def post(self, action_alias):
29
        self._redirect(request)
30
31
    @expose()
32
    def put(self, action_alias_ref_or_id, action_alias):
33
        self._redirect(request)
34
35
    @expose()
36
    def delete(self, action_alias_ref_or_id):
37
        self._redirect(request)
38
39
    def _redirect(self, request_instance):
40
        redirect_url = request.path_url.replace('/exp/', '/v1/')
41
        LOG.debug('Redirecting to: %s', redirect_url)
42
        # In theory, we could redirect using pecan's redirect method but there is
43
        # a bug that doesn't display the name of the moved location in the body.
44
        # redirect_path = request.path.replace('/exp/', '/v1')
45
        # redirect(location=redirect_path, internal=False, request=request,
46
        #          code=http_client.MOVED_PERMANENTLY)
47
        msg = 'The resource has been permanently moved to %s.' % redirect_url
48
        abort(status_code=http_client.MOVED_PERMANENTLY,
49
              headers={'Location': redirect_url},
50
              detail=msg,
51
              comment=msg)
52