Completed
Pull Request — master (#2754)
by Manas
05:23
created

BaseRestControllerMixin._parse_query_params()   A

Complexity

Conditions 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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
from pecan.rest import RestController
18
from six.moves.urllib import parse as urlparse  # pylint: disable=import-error
19
20
from st2api.controllers.controller_transforms import transform_to_bool
21
22
__all__ = [
23
    'BaseRestControllerMixin'
24
]
25
26
27
SHOW_SECRETS_QUERY_PARAM = 'show_secrets'
28
29
30
class BaseRestControllerMixin(RestController):
31
    """
32
    Base REST controller class which contains various utility functions.
33
    """
34
35
    def _parse_query_params(self, request):
36
        """
37
        Parse query string for the provided request.
38
39
        :rtype: ``dict``
40
        """
41
        query_string = request.query_string
42
        query_params = dict(urlparse.parse_qsl(query_string))
43
44
        return query_params
45
46
    def _get_query_param_value(self, request, param_name, param_type, default_value=None):
47
        """
48
        Return a value for the provided query param and optionally cast it for boolean types.
49
50
        If the requested query parameter is not provided, default value is returned instead.
51
52
        :param request: Request object.
53
54
        :param param_name: Name of the param to retrieve the value for.
55
        :type param_name: ``str``
56
57
        :param param_type: Type of the query param (e.g. "bool").
58
        :type param_type: ``str``
59
60
        :param default_value: Value to return if query param is not provided.
61
        :type default_value: ``object``
62
        """
63
        query_params = self._parse_query_params(request=request)
64
        value = query_params.get(param_name, default_value)
65
66
        if param_type == 'bool' and isinstance(value, six.string_types):
67
            value = transform_to_bool(value)
68
69
        return value
70
71
    def _get_mask_secrets(self, request):
72
        """
73
        Return a value for mask_secrets which can be used in masking secret properties
74
        to be retruned by any API. The default value is as per the config however admin
75
        users have the ability to override by passing in a special query parameter
76
        show_secrets.
77
78
        :param request: Request object.
79
80
        :rtype: ``bool``
81
        """
82
        mask_secrets = cfg.CONF.api.mask_secrets
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'cfg'
Loading history...
83
        show_secrets_param = self._get_query_param_value(request=request,
84
                                                   param_name=SHOW_SECRETS_QUERY_PARAM,
85
                                                   param_type='bool',
86
                                                   default_value=False)
87
88
        if show_secrets and request_user_is_admin(request=request):
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'show_secrets'
Loading history...
Comprehensibility Best Practice introduced by
Undefined variable 'request_user_is_admin'
Loading history...
89
            mask_secrets = False
90
91
        return mask_secrets
92