| Conditions | 22 |
| Total Lines | 90 |
| Code Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 core.useractivity.user_logger() 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 | import os |
||
| 209 | def user_logger(func): |
||
| 210 | """ |
||
| 211 | Decorator for logging user activities |
||
| 212 | :param func: the decorated function |
||
| 213 | :return: the decorator |
||
| 214 | """ |
||
| 215 | @wraps(func) |
||
| 216 | def logger(*args, **kwargs): |
||
| 217 | qualified_name = func.__qualname__ |
||
| 218 | class_name = qualified_name.split(".")[0] |
||
| 219 | func_name = qualified_name.split(".")[1] |
||
| 220 | |||
| 221 | if func_name not in ("on_post", "on_put", "on_delete"): |
||
| 222 | # do not log for other HTTP Methods |
||
| 223 | func(*args, **kwargs) |
||
| 224 | return |
||
| 225 | req, resp = args |
||
| 226 | headers = req.headers |
||
| 227 | if headers is not None and 'USER-UUID' in headers.keys(): |
||
| 228 | user_uuid = headers['USER-UUID'] |
||
| 229 | else: |
||
| 230 | # todo: deal with requests with NULL user_uuid |
||
| 231 | print('user_logger: USER-UUID is NULL') |
||
| 232 | # do not log for NULL user_uuid |
||
| 233 | func(*args, **kwargs) |
||
| 234 | return |
||
| 235 | |||
| 236 | if func_name == "on_post": |
||
| 237 | try: |
||
| 238 | file_name = str(uuid.uuid4()) |
||
| 239 | with open(file_name, "wb") as fw: |
||
| 240 | reads = req.stream.read() |
||
| 241 | fw.write(reads) |
||
| 242 | raw_json = reads.decode('utf-8') |
||
| 243 | with open(file_name, "rb") as fr: |
||
| 244 | req.stream = Body(fr) |
||
| 245 | func(*args, **kwargs) |
||
| 246 | write_log(user_uuid=user_uuid, request_method='POST', resource_type=class_name, |
||
| 247 | resource_id=kwargs.get('id_'), request_body=raw_json) |
||
| 248 | os.remove(file_name) |
||
| 249 | except OSError as e: |
||
| 250 | print("Failed to stream request") |
||
| 251 | except UnicodeDecodeError as e: |
||
| 252 | print("Failed to decode request") |
||
| 253 | except Exception as e: |
||
| 254 | if isinstance(e, falcon.HTTPError): |
||
| 255 | raise e |
||
| 256 | else: |
||
| 257 | print('user_logger:' + str(e)) |
||
| 258 | return |
||
| 259 | elif func_name == "on_put": |
||
| 260 | try: |
||
| 261 | file_name = str(uuid.uuid4()) |
||
| 262 | |||
| 263 | with open(file_name, "wb") as fw: |
||
| 264 | reads = req.stream.read() |
||
| 265 | fw.write(reads) |
||
| 266 | raw_json = reads.decode('utf-8') |
||
| 267 | with open(file_name, "rb") as fr: |
||
| 268 | req.stream = Body(fr) |
||
| 269 | func(*args, **kwargs) |
||
| 270 | write_log(user_uuid=user_uuid, request_method='PUT', resource_type=class_name, |
||
| 271 | resource_id=kwargs.get('id_'), request_body=raw_json) |
||
| 272 | os.remove(file_name) |
||
| 273 | except OSError as e: |
||
| 274 | print("Failed to stream request") |
||
| 275 | except UnicodeDecodeError as e: |
||
| 276 | print("Failed to decode request") |
||
| 277 | except Exception as e: |
||
| 278 | if isinstance(e, falcon.HTTPError): |
||
| 279 | raise e |
||
| 280 | else: |
||
| 281 | print('user_logger:' + str(e)) |
||
| 282 | |||
| 283 | return |
||
| 284 | elif func_name == "on_delete": |
||
| 285 | try: |
||
| 286 | func(*args, **kwargs) |
||
| 287 | write_log(user_uuid=user_uuid, request_method="DELETE", resource_type=class_name, |
||
| 288 | resource_id=kwargs.get('id_'), request_body=json.dumps(kwargs)) |
||
| 289 | except (TypeError, ValueError) as e: |
||
| 290 | print("Failed to decode JSON") |
||
| 291 | except Exception as e: |
||
| 292 | if isinstance(e, falcon.HTTPError): |
||
| 293 | raise e |
||
| 294 | else: |
||
| 295 | print('user_logger:' + str(e)) |
||
| 296 | return |
||
| 297 | |||
| 298 | return logger |
||
| 299 |