Code Duplication    Length = 46-47 lines in 2 locations

main.py 2 locations

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