Skip to content

Commit

Permalink
[QI2-1135] Made default_host configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
NischalQuTech committed Nov 7, 2024
1 parent a8fa11f commit fa6646d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions quantuminspire/cli/command_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,20 @@ def login(
typer.echo(f"Using member ID {settings.auths[host].team_member_id}")


@app.command("set-default-host")
def set_default_host(
host: str = typer.Argument(
help="The URL of the platform to which to connect"
),
) -> None:
"""Set default_host for interacting with Quantum Inspire.
"""
settings = Settings()
settings.default_host = Url(host)
settings.write_settings_to_file()
typer.echo(f"Default host set to {host}")


@app.command("logout")
def logout(
host: str = typer.Argument(
Expand Down
5 changes: 5 additions & 0 deletions quantuminspire/util/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,16 @@ def store_tokens(self, host: Url, tokens: TokenInfo) -> None:
self.auths[host].tokens = tokens
member_id = self.get_team_member_id(host=host, access_token=tokens.access_token)
self.auths[host].team_member_id = member_id
self.default_host = host
self.write_settings_to_file()

def write_settings_to_file(self) -> None:
assert isinstance(self.model_config["json_file"], PathLike)
Path(self.model_config["json_file"]).write_text(
self.model_dump_json(indent=2), encoding=self.model_config.get("env_file_encoding")
)


@staticmethod
async def _fetch_team_member_id(host: str, access_token: str) -> int:
config = Configuration(host=host, access_token=access_token)
Expand Down
9 changes: 9 additions & 0 deletions tests/cli/test_command_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,12 @@ def test_login(mocker: MockerFixture, mocked_config_file: MagicMock) -> None:
device_session.poll_for_tokens.assert_called_once()
store_tokens.assert_called_once()
assert "Login successful!" in result.stdout


def test_set_default_host(mocker: MockerFixture, mocked_config_file: MagicMock) -> None:
host = "https://example.com"
write_settings_to_file = mocker.patch("quantuminspire.cli.command_list.Settings.write_settings_to_file")
result = runner.invoke(app, ["set-default-host", host])
assert result.exit_code == 0, repr(result.exception)
write_settings_to_file.assert_called_once()
assert f"Default host set to {host}" in result.stdout

0 comments on commit fa6646d

Please sign in to comment.