def _complete_import_request(
    import_request: LaunchDarklyImportRequest,
) -> Generator[None, None, None]:
    """
    Wrap code so the import request always ends up completed.

    If no exception raised, assume successful import.

    In case wrapped code needs to expose an error to the user, it should populate
    `import_request.status["error_messages"]` before raising an exception.
    """
    result: LaunchDarklyImportResult = "incomplete"
    try:
        yield
    except Exception as exc:
        if not isinstance(exc, LaunchDarklyRateLimitError):
            result = "failure"
        raise exc
    else:
        if not import_request.status.get("error_messages"):
            result = "success"
    finally:
        import_request.status["result"] = result
        if result in {"success", "failure"}:
            import_request.ld_token = ""
        import_request.completed_at = timezone.now()
        import_request.save()

    Wrap code so the import request always ends up completed.

    If no exception raised, assume successful import.

    In case wrapped code needs to expose an error to the user, it should populate
    `import_request.status["error_messages"]` before raising an exception.
    
api/integrations/launch_darkly/services.py