Conditions | 11 |
Total Lines | 51 |
Code Lines | 39 |
Lines | 47 |
Ratio | 92.16 % |
Tests | 0 |
CRAP Score | 132 |
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 build.main.Main.enable_telemetry() 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 | """Main module of kytos/telemetry Network Application. |
||
61 | View Code Duplication | @rest("v1/evc/enable", methods=["POST"]) |
|
|
|||
62 | async def enable_telemetry(self, request: Request) -> JSONResponse: |
||
63 | """REST to enable INT flows on EVCs. |
||
64 | |||
65 | If a list of evc_ids is empty, it'll enable on non-INT EVCs. |
||
66 | """ |
||
67 | |||
68 | try: |
||
69 | content = await aget_json_or_400(request) |
||
70 | evc_ids = content["evc_ids"] |
||
71 | force = content.get("force", False) |
||
72 | if not isinstance(force, bool): |
||
73 | raise TypeError(f"'force' wrong type: {type(force)} expected bool") |
||
74 | except (TypeError, KeyError): |
||
75 | raise HTTPException(400, detail=f"Invalid payload: {content}") |
||
76 | |||
77 | try: |
||
78 | evcs = ( |
||
79 | await api.get_evcs() |
||
80 | if len(evc_ids) != 1 |
||
81 | else await api.get_evc(evc_ids[0]) |
||
82 | ) |
||
83 | except RetryError as exc: |
||
84 | exc_error = str(exc.last_attempt.exception()) |
||
85 | log.error(exc_error) |
||
86 | raise HTTPException(503, detail=exc_error) |
||
87 | |||
88 | if evc_ids: |
||
89 | evcs = {evc_id: evcs.get(evc_id, {}) for evc_id in evc_ids} |
||
90 | else: |
||
91 | evcs = {k: v for k, v in evcs.items() if not utils.has_int_enabled(v)} |
||
92 | if not evcs: |
||
93 | # There's no non-INT EVCs to get enabled. |
||
94 | return JSONResponse({}) |
||
95 | |||
96 | try: |
||
97 | await self.int_manager.enable_int(evcs, force) |
||
98 | except (EVCNotFound, FlowsNotFound, ProxyPortNotFound) as exc: |
||
99 | raise HTTPException(404, detail=str(exc)) |
||
100 | except (EVCHasINT, ProxyPortStatusNotUP) as exc: |
||
101 | raise HTTPException(400, detail=str(exc)) |
||
102 | except RetryError as exc: |
||
103 | exc_error = str(exc.last_attempt.exception()) |
||
104 | log.error(exc_error) |
||
105 | raise HTTPException(503, detail=exc_error) |
||
106 | except UnrecoverableError as exc: |
||
107 | exc_error = str(exc) |
||
108 | log.error(exc_error) |
||
109 | raise HTTPException(500, detail=exc_error) |
||
110 | |||
111 | return JSONResponse({}, status_code=201) |
||
112 | |||
229 |