| Conditions | 12 |
| Total Lines | 98 |
| Code Lines | 69 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 1 |
| CRAP Score | 146.6209 |
| 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 handlers.management_handler.ManagementHandler._add_or_update_endpoint() 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 | 1 | import logging |
|
| 45 | 1 | @gen.coroutine |
|
| 46 | def _add_or_update_endpoint(self, action, name, version, request_data): |
||
| 47 | """ |
||
| 48 | Add or update an endpoint |
||
| 49 | """ |
||
| 50 | self.logger.log(logging.DEBUG, f"Adding/updating model {name}...") |
||
| 51 | |||
| 52 | if not isinstance(name, str): |
||
| 53 | msg = "Endpoint name must be a string" |
||
| 54 | self.logger.log(logging.CRITICAL, msg) |
||
| 55 | raise TypeError(msg) |
||
| 56 | |||
| 57 | name_checker = _compile(r"^[a-zA-Z0-9-_\s]+$") |
||
| 58 | if not name_checker.match(name): |
||
| 59 | raise gen.Return( |
||
| 60 | "endpoint name can only contain: a-z, A-Z, 0-9," |
||
| 61 | " underscore, hyphens and spaces." |
||
| 62 | ) |
||
| 63 | |||
| 64 | if self.settings.get("add_or_updating_endpoint"): |
||
| 65 | msg = ( |
||
| 66 | "Another endpoint update is already in progress" |
||
| 67 | ", please wait a while and try again" |
||
| 68 | ) |
||
| 69 | self.logger.log(logging.CRITICAL, msg) |
||
| 70 | raise RuntimeError(msg) |
||
| 71 | |||
| 72 | self.settings["add_or_updating_endpoint"] = random_uuid() |
||
| 73 | try: |
||
| 74 | docstring = None |
||
| 75 | if "docstring" in request_data: |
||
| 76 | docstring = str( |
||
| 77 | bytes(request_data["docstring"], "utf-8").decode("unicode_escape") |
||
| 78 | ) |
||
| 79 | |||
| 80 | description = request_data.get("description", None) |
||
| 81 | endpoint_type = request_data.get("type", None) |
||
| 82 | methods = request_data.get("methods", []) |
||
| 83 | dependencies = request_data.get("dependencies", None) |
||
| 84 | target = request_data.get("target", None) |
||
| 85 | schema = request_data.get("schema", None) |
||
| 86 | src_path = request_data.get("src_path", None) |
||
| 87 | target_path = get_query_object_path( |
||
| 88 | self.settings[SettingsParameters.StateFilePath], name, version |
||
| 89 | ) |
||
| 90 | |||
| 91 | path_checker = _compile(r"^[\\\:a-zA-Z0-9-_~\s/\.\(\)]+$") |
||
| 92 | # copy from staging |
||
| 93 | if src_path: |
||
| 94 | if not isinstance(src_path, str): |
||
| 95 | raise gen.Return("src_path must be a string.") |
||
| 96 | if not path_checker.match(src_path): |
||
| 97 | raise gen.Return(f"Invalid source path for endpoint {name}") |
||
| 98 | |||
| 99 | yield self._copy_po_future(src_path, target_path) |
||
| 100 | elif endpoint_type != "alias": |
||
| 101 | raise gen.Return("src_path is required to add/update an endpoint.") |
||
| 102 | else: |
||
| 103 | # alias special logic: |
||
| 104 | if not target: |
||
| 105 | raise gen.Return("Target is required for alias endpoint.") |
||
| 106 | dependencies = [target] |
||
| 107 | |||
| 108 | # update local config |
||
| 109 | try: |
||
| 110 | if action == "add": |
||
| 111 | self.tabpy_state.add_endpoint( |
||
| 112 | name=name, |
||
| 113 | description=description, |
||
| 114 | docstring=docstring, |
||
| 115 | endpoint_type=endpoint_type, |
||
| 116 | methods=methods, |
||
| 117 | dependencies=dependencies, |
||
| 118 | target=target, |
||
| 119 | schema=schema, |
||
| 120 | ) |
||
| 121 | else: |
||
| 122 | self.tabpy_state.update_endpoint( |
||
| 123 | name=name, |
||
| 124 | description=description, |
||
| 125 | docstring=docstring, |
||
| 126 | endpoint_type=endpoint_type, |
||
| 127 | methods=methods, |
||
| 128 | dependencies=dependencies, |
||
| 129 | target=target, |
||
| 130 | schema=schema, |
||
| 131 | version=version, |
||
| 132 | ) |
||
| 133 | |||
| 134 | except Exception as e: |
||
| 135 | raise gen.Return(f"Error when changing TabPy state: {e}") |
||
| 136 | |||
| 137 | on_state_change( |
||
| 138 | self.settings, self.tabpy_state, self.python_service, self.logger |
||
| 139 | ) |
||
| 140 | |||
| 141 | finally: |
||
| 142 | self.settings["add_or_updating_endpoint"] = None |
||
| 143 | |||
| 151 |