Code Duplication    Length = 52-52 lines in 2 locations

main.py 2 locations

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