Projet

Général

Profil

0001-utils-allow-specifying-endpoint-parameters-manually-.patch

Valentin Deniaud, 06 avril 2020 18:12

Télécharger (4,12 ko)

Voir les différences:

Subject: [PATCH 1/2] utils: allow specifying endpoint parameters manually
 (#41386)

 passerelle/utils/api.py | 54 ++++++++++++++++++++---------------------
 1 file changed, 27 insertions(+), 27 deletions(-)
passerelle/utils/api.py
43 43
                 parameters=None,
44 44
                 cache_duration=None,
45 45
                 post=None,
46
                 show=True):
46
                 show=True,
47
                 show_undocumented_params=True):
47 48
        self.perm = perm
48 49
        self.methods = methods
49 50
        self.serializer_type = serializer_type
......
61 62
            'patch': long_description_patch or long_description,
62 63
        }
63 64
        self.example_pattern = example_pattern
64
        self.parameters = parameters
65
        self.parameters = parameters or {}
65 66
        self.cache_duration = cache_duration
66 67
        self.post = post
67 68
        if post:
......
71 72
            if post.get('long_description'):
72 73
                self.long_descriptions['post'] = post.get('long_description')
73 74
        self.show = show
75
        self.show_undocumented_params = show_undocumented_params
74 76

  
75 77
    def __call__(self, func):
76 78
        func.endpoint_info = self
......
160 162
                return 'float'
161 163

  
162 164
        params = []
163
        defaults = dict(zip(
164
            reversed(inspect.getargspec(self.func).args),
165
            reversed(inspect.getargspec(self.func).defaults or [])))
166
        for param in inspect.getargspec(self.func).args[2:]:
167
            if param == 'post_data':
168
                continue
165
        available_params = self.parameters
166
        spec = inspect.getargspec(self.func)
167
        defaults = dict(zip(reversed(spec.args), reversed(spec.defaults or [])))
168
        if self.show_undocumented_params:
169
            available_params = {arg: {} for arg in spec.args[2:]
170
                                if arg != 'post_data' and not arg in self.parameters}
171
            available_params.update(self.parameters)
172
        for param, info in available_params.items():
169 173
            param_info = {'name': param}
170
            if self.parameters and param in self.parameters:
171
                info = self.parameters[param]
172
                if info.get('description'):
173
                    param_info['description'] = info['description']
174
                if 'type' in info:
175
                    typ = info['type']
176
                    if typ == 'int':
177
                        param_info['type'] = 'integer'
178
                    elif typ == 'bool':
179
                        param_info['type'] = 'boolean'
180
                    else:
181
                        param_info['type'] = typ
182
                elif 'example_value' in info:
183
                    typ = type_to_str(info['example_value'])
184
                    if typ:
185
                        param_info['type'] = typ
174
            if info.get('description'):
175
                param_info['description'] = info['description']
176
            typ = None
177
            if 'type' in info:
178
                typ = info['type']
179
                if typ == 'int':
180
                    typ = 'integer'
181
                elif typ == 'bool':
182
                    typ = 'boolean'
183
            elif 'example_value' in info:
184
                typ = type_to_str(info['example_value'])
186 185
            if param in defaults:
187 186
                param_info['optional'] = True
188 187
                param_info['default_value'] = defaults[param]
189
                if 'type' not in param_info:
188
                if not typ:
190 189
                    typ = type_to_str(defaults[param])
191
                    if typ:
192
                        param_info['type'] = typ
190
            if typ:
191
                param_info['type'] = typ
193 192
            params.append(param_info)
193
        params.sort(key=lambda x: (x.get('optional', False), x['name']))
194 194
        return params
195
-