Code Duplication    Length = 46-47 lines in 2 locations

main.py 2 locations

@@ 61-107 (lines=47) @@
58
        If you have some cleanup procedure, insert it here.
59
        """
60
61
    @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
@@ 113-158 (lines=46) @@
110
111
        return JSONResponse({}, status_code=201)
112
113
    @rest("v1/evc/disable", methods=["POST"])
114
    async def disable_telemetry(self, request: Request) -> JSONResponse:
115
        """REST to disable/remove INT flows for an EVC_ID
116
117
        If a list of evc_ids is empty, it'll disable on all INT EVCs.
118
        """
119
        try:
120
            content = await aget_json_or_400(request)
121
            evc_ids = content["evc_ids"]
122
            force = content.get("force", False)
123
            if not isinstance(force, bool):
124
                raise TypeError(f"'force' wrong type: {type(force)} expected bool")
125
        except (TypeError, KeyError):
126
            raise HTTPException(400, detail=f"Invalid payload: {content}")
127
128
        try:
129
            evcs = (
130
                await api.get_evcs()
131
                if len(evc_ids) != 1
132
                else await api.get_evc(evc_ids[0])
133
            )
134
        except RetryError as exc:
135
            exc_error = str(exc.last_attempt.exception())
136
            log.error(exc_error)
137
            raise HTTPException(503, detail=exc_error)
138
139
        if evc_ids:
140
            evcs = {evc_id: evcs.get(evc_id, {}) for evc_id in evc_ids}
141
        else:
142
            evcs = {k: v for k, v in evcs.items() if utils.has_int_enabled(v)}
143
            if not evcs:
144
                # There's no INT EVCs to get disabled.
145
                return JSONResponse({})
146
147
        try:
148
            await self.int_manager.disable_int(evcs, force)
149
        except EVCNotFound as exc:
150
            raise HTTPException(404, detail=str(exc))
151
        except EVCHasNoINT as exc:
152
            raise HTTPException(400, detail=str(exc))
153
        except RetryError as exc:
154
            exc_error = str(exc.last_attempt.exception())
155
            log.error(exc_error)
156
            raise HTTPException(503, detail=exc_error)
157
        except UnrecoverableError as exc:
158
            exc_error = str(exc)
159
            log.error(exc_error)
160
            raise HTTPException(500, detail=exc_error)
161