Code Duplication    Length = 40-40 lines in 2 locations

main.py 2 locations

@@ 459-498 (lines=40) @@
456
        evc.sync()
457
        return jsonify("Operation successful"), 201
458
459
    @rest("v2/evc/metadata/<key>", methods=["DELETE"])
460
    def delete_bulk_metadata(self, key):
461
        """Delete metada from a bulk of EVCs"""
462
        try:
463
            metadata = request.get_json()
464
            content_type = request.content_type
465
        except BadRequest as error:
466
            result = "The request body is not a well-formed JSON."
467
            raise BadRequest(result) from error
468
        if content_type is None:
469
            result = "The request body is empty."
470
            raise BadRequest(result)
471
        if metadata is None:
472
            if content_type != "application/json":
473
                result = (
474
                    "The content type must be application/json "
475
                    f"(received {content_type})."
476
                )
477
            else:
478
                result = "Request is empty."
479
            raise UnsupportedMediaType(result)
480
481
        circuit_ids = metadata.pop("circuit_ids", None)
482
        if circuit_ids is None:
483
            raise BadRequest("The key 'circuit_ids' is missing.")
484
        payload = {f"metadata.{key}": ""}
485
        self.mongo_controller.update_bulk_evc(circuit_ids, {"$unset": payload})
486
487
        fail_evcs = set()
488
        for _id in circuit_ids:
489
            try:
490
                evc = self.circuits[_id]
491
                evc.remove_metadata(key)
492
                evc.sync()
493
            except KeyError:
494
                fail_evcs.add(_id)
495
496
        if fail_evcs:
497
            raise NotFound(f"Circuits {str(fail_evcs)} were not found")
498
        return jsonify("Operation successful"), 200
499
500
    @rest("v2/evc/<circuit_id>/metadata/<key>", methods=["DELETE"])
501
    def delete_metadata(self, circuit_id, key):
@@ 387-426 (lines=40) @@
384
        except KeyError as error:
385
            raise NotFound(f"circuit_id {circuit_id} not found.") from error
386
387
    @rest("v2/evc/metadata", methods=["POST"])
388
    def add_bulk_metadata(self):
389
        """Add metadata to a bulk of EVCs."""
390
        try:
391
            metadata = request.get_json()
392
            content_type = request.content_type
393
        except BadRequest as error:
394
            result = "The request body is not a well-formed JSON."
395
            raise BadRequest(result) from error
396
        if content_type is None:
397
            result = "The request body is empty."
398
            raise BadRequest(result)
399
        if metadata is None:
400
            if content_type != "application/json":
401
                result = (
402
                    "The content type must be application/json "
403
                    f"(received {content_type})."
404
                )
405
            else:
406
                result = "Metadata is empty."
407
            raise UnsupportedMediaType(result)
408
409
        circuit_ids = metadata.pop("circuit_ids", None)
410
        if circuit_ids is None:
411
            raise BadRequest("The key 'circuit_ids' is missing.")
412
        payload = {f"metadata.{k}": v for k, v in metadata.items()}
413
        self.mongo_controller.update_bulk_evc(circuit_ids, {"$set": payload})
414
415
        fail_evcs = set()
416
        for _id in circuit_ids:
417
            try:
418
                evc = self.circuits[_id]
419
                evc.extend_metadata(metadata)
420
                evc.sync()
421
            except KeyError:
422
                fail_evcs.add(_id)
423
424
        if fail_evcs:
425
            raise NotFound(f"Circuits {str(fail_evcs)} were not found")
426
        return jsonify("Operation successful"), 201
427
428
    @rest("v2/evc/<circuit_id>/metadata", methods=["POST"])
429
    def add_metadata(self, circuit_id):