Code Duplication    Length = 52-52 lines in 2 locations

main.py 2 locations

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