Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for the version of the api through /status/parameters #119

Merged
merged 8 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions firecrest/AsyncClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ def __init__(
self.polling_sleep_times: list = 250 * [0]
#: Disable all logging from the client.
self.disable_client_logging: bool = False
self._api_version: Version = parse("1.15.0")
self._session = httpx.AsyncClient(verify=self._verify)

#: Seconds between requests in each microservice
Expand Down Expand Up @@ -247,9 +246,36 @@ def __init__(
"/tasks": None,
}

# Try to set the api version by querying the /status/parameters
# endpoint
try:
general_params = self._api_version = self._get_request(
endpoint="/status/parameters",
).json()["out"]["general"]
for g in general_params:
if g["name"] == "FIRECREST_VERSION":
self._api_version = parse(g["value"])
return

# We want the except block to catch this and set the
# version to the default one
raise Exception

except Exception:
self.log(
logging.WARNING,
"Could not get the version of the api from firecREST. "
"The version will be set to 1.15.0, but you can manually "
"set it with the method `set_api_version`."
)
self._api_version = parse("1.15.0")

def set_api_version(self, api_version: str) -> None:
"""Set the version of the api of firecrest. By default it will be assumed that you are
using version 1.13.1 or compatible. The version is parsed by the `packaging` library.
"""Set the version of the api of firecrest manually. By default, the
client will query the api (before the first request), through the
/status endpoint. This information is only available for
version>=1.16.1, so for older deployments the default will be 1.15.0.
The version is parsed by the `packaging` library.
"""
self._api_version = parse(api_version)

Expand Down
31 changes: 28 additions & 3 deletions firecrest/BasicClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,37 @@ def __init__(
self.polling_sleep_times: list = [1, 0.5] + 234 * [0.25]
#: Disable all logging from the client.
self.disable_client_logging: bool = False
self._api_version: Version = parse("1.15.0")
self._session = requests.Session()
# Try to set the api version by querying the /status/parameters
# endpoint
try:
general_params = self._api_version = self._get_request(
endpoint="/status/parameters",
).json()["out"]["general"]
for g in general_params:
if g["name"] == "FIRECREST_VERSION":
self._api_version = parse(g["value"])
return

# We want the except block to catch this and set the
# version to the default one
raise Exception

except Exception:
self.log(
logging.WARNING,
"Could not get the version of the api from firecREST. "
"The version will be set to 1.15.0, but you can manually "
"set it with the method `set_api_version`."
)
self._api_version = parse("1.15.0")

def set_api_version(self, api_version: str) -> None:
"""Set the version of the api of firecrest. By default it will be assumed that you are
using version 1.13.1 or compatible. The version is parsed by the `packaging` library.
"""Set the version of the api of firecrest manually. By default, the
client will query the api (before the first request), through the
/status endpoint. This information is only available for
version>=1.16.1, so for older deployments the default will be 1.15.0.
The version is parsed by the `packaging` library.
"""
self._api_version = parse(api_version)

Expand Down
36 changes: 35 additions & 1 deletion firecrest/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,41 @@ def parameters(
*info
)

console.print(storage_table, utilities_table, overflow="fold")
extra_tables = []

if "compute" in result:
info = [
("Name", "name"),
("Value", "value"),
("Unit", "unit"),
]
if "description" in result["compute"][0]:
info.append(("Description", "description"))

compute_table = create_table(
"Compute parameters",
result["compute"],
*info
)
extra_tables.append(compute_table)

if "general" in result:
info = [
("Name", "name"),
("Value", "value"),
("Unit", "unit"),
]
if "description" in result["general"][0]:
info.append(("Description", "description"))

general_table = create_table(
"General parameters",
result["general"],
*info
)
extra_tables.append(general_table)

console.print(storage_table, utilities_table, *extra_tables, overflow="fold")
except Exception as e:
examine_exeption(e)
raise typer.Exit(code=1)
Expand Down
6 changes: 5 additions & 1 deletion firecrest/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ class Parameter(TypedDict):


class Parameters(TypedDict):
"""A parameters record, from `status/parameters`"""
"""A parameters record, from `status/parameters`. For older versions
of the API `compute` and `system` may not be present.
"""

storage: list[Parameter]
utilities: list[Parameter]
compute: list[Parameter]
general: list[Parameter]


class Service(TypedDict):
Expand Down
Loading