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