Code Duplication    Length = 44-45 lines in 2 locations

main.py 2 locations

@@ 88-132 (lines=45) @@
85
86
        return await self.install_int_flows(switches_flows)
87
88
    @rest("v1/evc/enable", methods=["POST"])
89
    async def enable_telemetry(self, request: Request) -> JSONResponse:
90
        """REST to enable INT flows on EVCs.
91
92
        If a list of evc_ids is empty, it'll enable on non-INT EVCs.
93
        """
94
95
        try:
96
            content = await aget_json_or_400(request)
97
            evc_ids = content["evc_ids"]
98
            force = bool(content.get("force", False))
99
        except (TypeError, KeyError):
100
            raise HTTPException(400, detail=f"Invalid payload: {content}")
101
102
        try:
103
            evcs = await get_evcs() if len(evc_ids) != 1 else await get_evc(evc_ids[0])
104
        except RetryError as exc:
105
            exc_error = str(exc.last_attempt.exception())
106
            log.error(exc_error)
107
            raise HTTPException(503, detail=exc_error)
108
109
        if evc_ids:
110
            evcs = {evc_id: evcs.get(evc_id, {}) for evc_id in evc_ids}
111
        else:
112
            evcs = {k: v for k, v in evcs.items() if not utils.has_int_enabled(v)}
113
            if not evcs:
114
                # There's no non-INT EVCs to get enabled.
115
                return JSONResponse({})
116
117
        try:
118
            await self.int_manager.enable_int(evcs, force)
119
        except (EVCNotFound, FlowsNotFound, ProxyPortNotFound) as exc:
120
            raise HTTPException(404, detail=str(exc))
121
        except (EVCHasINT, ProxyPortStatusNotUP) as exc:
122
            raise HTTPException(400, detail=str(exc))
123
        except RetryError as exc:
124
            exc_error = str(exc.last_attempt.exception())
125
            log.error(exc_error)
126
            raise HTTPException(503, detail=exc_error)
127
        except UnrecoverableError as exc:
128
            exc_error = str(exc)
129
            log.error(exc_error)
130
            raise HTTPException(500, detail=exc_error)
131
132
        return JSONResponse({}, status_code=201)
133
134
    @rest("v1/evc/disable", methods=["POST"])
135
    async def disable_telemetry(self, request: Request) -> JSONResponse:
@@ 134-177 (lines=44) @@
131
132
        return JSONResponse({}, status_code=201)
133
134
    @rest("v1/evc/disable", methods=["POST"])
135
    async def disable_telemetry(self, request: Request) -> JSONResponse:
136
        """REST to disable/remove INT flows for an EVC_ID
137
138
        If a list of evc_ids is empty, it'll disable on all INT EVCs.
139
        """
140
        try:
141
            content = await aget_json_or_400(request)
142
            evc_ids = content["evc_ids"]
143
            force = bool(content.get("force", False))
144
        except (TypeError, KeyError):
145
            raise HTTPException(400, detail=f"Invalid payload: {content}")
146
147
        try:
148
            evcs = await get_evcs() if len(evc_ids) != 1 else await get_evc(evc_ids[0])
149
        except RetryError as exc:
150
            exc_error = str(exc.last_attempt.exception())
151
            log.error(exc_error)
152
            raise HTTPException(503, detail=exc_error)
153
154
        if evc_ids:
155
            evcs = {evc_id: evcs.get(evc_id, {}) for evc_id in evc_ids}
156
        else:
157
            evcs = {k: v for k, v in evcs.items() if utils.has_int_enabled(v)}
158
            if not evcs:
159
                # There's no INT EVCs to get disabled.
160
                return JSONResponse({})
161
162
        try:
163
            await self.int_manager.disable_int(evcs, force)
164
        except EVCNotFound as exc:
165
            raise HTTPException(404, detail=str(exc))
166
        except EVCHasNoINT as exc:
167
            raise HTTPException(400, detail=str(exc))
168
        except RetryError as exc:
169
            exc_error = str(exc.last_attempt.exception())
170
            log.error(exc_error)
171
            raise HTTPException(503, detail=exc_error)
172
        except UnrecoverableError as exc:
173
            exc_error = str(exc)
174
            log.error(exc_error)
175
            raise HTTPException(500, detail=exc_error)
176
177
        return JSONResponse({})
178
179
    @rest("v1/evc")
180
    def get_evcs(self, _request: Request) -> JSONResponse: