Code Duplication    Length = 20-22 lines in 2 locations

main.py 2 locations

@@ 543-564 (lines=22) @@
540
        evc.sync()
541
        return JSONResponse("Operation successful", status_code=201)
542
543
    @rest("/v2/evc/metadata/{key}", methods=["DELETE"])
544
    @validate_openapi(spec)
545
    def bulk_delete_metadata(self, request: Request) -> JSONResponse:
546
        """Delete metada from a bulk of EVCs"""
547
        data = get_json_or_400(request, self.controller.loop)
548
        key = request.path_params["key"]
549
        circuit_ids = data.pop("circuit_ids")
550
        self.mongo_controller.update_evcs_metadata(
551
            circuit_ids, {key: ""}, "del"
552
        )
553
554
        fail_evcs = []
555
        for _id in circuit_ids:
556
            try:
557
                evc = self.circuits[_id]
558
                evc.remove_metadata(key)
559
            except KeyError:
560
                fail_evcs.append(_id)
561
562
        if fail_evcs:
563
            raise HTTPException(404, detail=fail_evcs)
564
        return JSONResponse("Operation successful")
565
566
    @rest("/v2/evc/{circuit_id}/metadata/{key}", methods=["DELETE"])
567
    def delete_metadata(self, request: Request) -> JSONResponse:
@@ 502-521 (lines=20) @@
499
                detail=f"circuit_id {circuit_id} not found."
500
            ) from error
501
502
    @rest("/v2/evc/metadata", methods=["POST"])
503
    @validate_openapi(spec)
504
    def bulk_add_metadata(self, request: Request) -> JSONResponse:
505
        """Add metadata to a bulk of EVCs."""
506
        data = get_json_or_400(request, self.controller.loop)
507
        circuit_ids = data.pop("circuit_ids")
508
509
        self.mongo_controller.update_evcs_metadata(circuit_ids, data, "add")
510
511
        fail_evcs = []
512
        for _id in circuit_ids:
513
            try:
514
                evc = self.circuits[_id]
515
                evc.extend_metadata(data)
516
            except KeyError:
517
                fail_evcs.append(_id)
518
519
        if fail_evcs:
520
            raise HTTPException(404, detail=fail_evcs)
521
        return JSONResponse("Operation successful", status_code=201)
522
523
    @rest("/v2/evc/{circuit_id}/metadata", methods=["POST"])
524
    @validate_openapi(spec)