def _switch_config(api: "PgBackupApi", barman_server: str,
                   barman_model: Optional[str], reset: Optional[bool]) -> int:
    """Switch configuration of Barman server through ``pg-backup-api``.

    .. note::
        If requests to ``pg-backup-api`` fail recurrently or we face HTTP
        errors, then exit with :attr:`ExitCode.HTTP_ERROR`.

    :param api: a :class:`PgBackupApi` instance to handle communication with
        the API.
    :param barman_server: name of the Barman server which config is to be
        switched.
    :param barman_model: name of the Barman model to be applied to the server,
        if any.
    :param reset: ``True`` if you would like to unapply the currently active
        model for the server, if any.

    :returns: the return code to be used when exiting the ``patroni_barman``
        application. Refer to :class:`ExitCode`.
    """
    operation_id = None

    try:
        operation_id = api.create_config_switch_operation(
            barman_server,
            barman_model,
            reset,
        )
    except RetriesExceeded as exc:
        logging.error("An issue was faced while trying to create a config "
                      "switch operation: %r", exc)
        return ExitCode.HTTP_ERROR

    logging.info("Created the config switch operation with ID %s",
                 operation_id)

    status = None

    while True:
        try:
            status = api.get_operation_status(barman_server, operation_id)
        except RetriesExceeded:
            logging.error("Maximum number of retries exceeded, exiting.")
            return ExitCode.HTTP_ERROR

        if status != OperationStatus.IN_PROGRESS:
            break

        logging.info("Config switch operation %s is still in progress",
                     operation_id)
        time.sleep(5)

    if status == OperationStatus.DONE:
        logging.info("Config switch operation finished successfully.")
        return ExitCode.CONFIG_SWITCH_DONE
    else:
        logging.error("Config switch operation failed.")
        return ExitCode.CONFIG_SWITCH_FAILED
Switch configuration of Barman server through ``pg-backup-api``.

    .. note::
        If requests to ``pg-backup-api`` fail recurrently or we face HTTP
        errors, then exit with :attr:`ExitCode.HTTP_ERROR`.

    :param api: a :class:`PgBackupApi` instance to handle communication with
        the API.
    :param barman_server: name of the Barman server which config is to be
        switched.
    :param barman_model: name of the Barman model to be applied to the server,
        if any.
    :param reset: ``True`` if you would like to unapply the currently active
        model for the server, if any.

    :returns: the return code to be used when exiting the ``patroni_barman``
        application. Refer to :class:`ExitCode`.
    
patroni/scripts/barman/config_switch.py