Conditions | 19 |
Total Lines | 106 |
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:
Complex classes like jsexpose() 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 |
||
199 | def jsexpose(arg_types=None, body_cls=None, status_code=None, content_type='application/json', |
||
200 | method=None): |
||
201 | """ |
||
202 | :param arg_types: A list of types for the function arguments (e.g. [str, str, int, bool]). |
||
203 | :type arg_types: ``list`` |
||
204 | |||
205 | :param body_cls: Request body class. If provided, this class will be used to create an instance |
||
206 | out of the request body. |
||
207 | :type body_cls: :class:`object` |
||
208 | |||
209 | :param status_code: Response status code. |
||
210 | :type status_code: ``int`` |
||
211 | |||
212 | :param content_type: Response content type. |
||
213 | :type content_type: ``str`` |
||
214 | """ |
||
215 | pecan_json_decorate = pecan.expose( |
||
216 | content_type=content_type, |
||
217 | generic=False) |
||
218 | |||
219 | def decorate(f): |
||
220 | @functools.wraps(f) |
||
221 | def callfunction(*args, **kwargs): |
||
222 | function_name = f.__name__ |
||
223 | args = list(args) |
||
224 | more = [args.pop(0)] |
||
225 | |||
226 | def cast_value(value_type, value): |
||
227 | if value_type == bool: |
||
228 | def cast_func(value): |
||
229 | return value.lower() in ['1', 'true'] |
||
230 | else: |
||
231 | cast_func = value_type |
||
232 | |||
233 | result = cast_func(value) |
||
234 | return result |
||
235 | |||
236 | if body_cls: |
||
237 | if pecan.request.body: |
||
238 | data = pecan.request.json |
||
239 | |||
240 | obj = body_cls(**data) |
||
241 | try: |
||
242 | obj = obj.validate() |
||
243 | except (jsonschema.ValidationError, ValueError) as e: |
||
244 | raise exc.HTTPBadRequest(detail=e.message, |
||
245 | comment=traceback.format_exc()) |
||
246 | except Exception as e: |
||
247 | raise exc.HTTPInternalServerError(detail=e.message, |
||
248 | comment=traceback.format_exc()) |
||
249 | |||
250 | # Set default pack if one is not provided for resource create |
||
251 | if function_name == 'post' and not hasattr(obj, 'pack'): |
||
252 | extra = { |
||
253 | 'resource_api': obj, |
||
254 | 'default_pack_name': DEFAULT_PACK_NAME |
||
255 | } |
||
256 | LOG.debug('Pack not provided in the body, setting a default pack name', |
||
257 | extra=extra) |
||
258 | setattr(obj, 'pack', DEFAULT_PACK_NAME) |
||
259 | else: |
||
260 | obj = None |
||
261 | |||
262 | more.append(obj) |
||
263 | |||
264 | if arg_types: |
||
265 | # Cast and transform arguments based on the provided arg_types specification |
||
266 | result_args, result_kwargs = get_controller_args_for_types(func=f, |
||
267 | arg_types=arg_types, |
||
268 | args=args, |
||
269 | kwargs=kwargs) |
||
270 | more = more + result_args |
||
271 | kwargs.update(result_kwargs) |
||
272 | |||
273 | args = tuple(more) + tuple(args) |
||
274 | |||
275 | noop_codes = [http_client.NOT_IMPLEMENTED, |
||
276 | http_client.METHOD_NOT_ALLOWED, |
||
277 | http_client.FORBIDDEN] |
||
278 | |||
279 | if status_code and status_code in noop_codes: |
||
280 | pecan.response.status = status_code |
||
281 | return json_encode(None) |
||
282 | |||
283 | try: |
||
284 | result = f(*args, **kwargs) |
||
285 | except TypeError as e: |
||
286 | e = get_exception_for_type_error(func=f, exc=e) |
||
287 | raise e |
||
288 | |||
289 | if status_code: |
||
290 | pecan.response.status = status_code |
||
291 | if content_type == 'application/json': |
||
292 | if is_debugging_enabled(): |
||
293 | indent = 4 |
||
294 | else: |
||
295 | indent = None |
||
296 | return json_encode(result, indent=indent) |
||
297 | else: |
||
298 | return result |
||
299 | |||
300 | pecan_json_decorate(callfunction) |
||
301 | |||
302 | return callfunction |
||
303 | |||
304 | return decorate |
||
305 |