Code Duplication    Length = 44-45 lines in 2 locations

main.py 2 locations

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