Skip to content

Commit b01005f

Browse files
committed
Move token server check to validation
1 parent a8c9cf6 commit b01005f

File tree

2 files changed

+29
-35
lines changed

2 files changed

+29
-35
lines changed

simvue/config/parameters.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import pydantic
44
import typing
55
import pathlib
6+
import http
67

78
import simvue.models as sv_models
89
from simvue.utilities import get_expiry
10+
from simvue.version import __version__
11+
from simvue.api import get
912

1013
CONFIG_FILE_NAMES: list[str] = ["simvue.toml", ".simvue.toml"]
1114

@@ -27,12 +30,34 @@ def url_to_str(cls, v: typing.Any) -> str:
2730
return f"{v}"
2831

2932
@pydantic.field_validator("token")
30-
def check_token(cls, v: pydantic.SecretStr) -> str:
31-
if not (expiry := get_expiry(v.get_secret_value())):
33+
def check_token(cls, v: typing.Any) -> str:
34+
if not (expiry := get_expiry(v)):
3235
raise AssertionError("Failed to parse Simvue token - invalid token form")
3336
if time.time() - expiry > 0:
3437
raise AssertionError("Simvue token has expired")
35-
return v.get_secret_value()
38+
return v
39+
40+
@pydantic.model_validator(mode="after")
41+
def check_valid_server(cls, values: dict) -> bool:
42+
url = values["url"]
43+
token = values["token"]
44+
headers: dict[str, str] = {
45+
"Authorization": f"Bearer {token}",
46+
"User-Agent": f"Simvue Python client {__version__}",
47+
}
48+
try:
49+
response = get(f"{url}/api/version", headers)
50+
51+
if response.status_code != http.HTTPStatus.OK or not response.json().get(
52+
"version"
53+
):
54+
raise AssertionError
55+
56+
if response.status_code == http.HTTPStatus.UNAUTHORIZED:
57+
raise AssertionError("Unauthorised token")
58+
59+
except Exception as err:
60+
raise AssertionError(f"Exception retrieving server version: {str(err)}")
3661

3762

3863
class DefaultRunSpecifications(pydantic.BaseModel):

simvue/factory/proxy/remote.py

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import logging
22
import typing
33
import http
4-
import time
54

65
if typing.TYPE_CHECKING:
76
from simvue.config import SimvueConfiguration
87

98
from simvue.api import get, post, put
109
from simvue.factory.proxy.base import SimvueBaseClass
11-
from simvue.utilities import prepare_for_api, skip_if_failed, get_expiry
10+
from simvue.utilities import prepare_for_api, skip_if_failed
1211
from simvue.version import __version__
1312

1413
logger = logging.getLogger(__name__)
@@ -483,36 +482,6 @@ def send_heartbeat(self) -> typing.Optional[dict[str, typing.Any]]:
483482
self._error(f"Got status code {response.status_code} when sending heartbeat")
484483
return None
485484

486-
@skip_if_failed("_aborted", "_suppress_errors", False)
487-
def check_token(self) -> bool:
488-
"""
489-
Check token
490-
"""
491-
if not (expiry := get_expiry(self._token)):
492-
self._error("Failed to parse user token")
493-
return False
494-
495-
if time.time() - expiry > 0:
496-
self._error("Token has expired")
497-
return False
498-
499-
try:
500-
response = get(f"{self._url}/api/version", self._headers)
501-
502-
if response.status_code != http.HTTPStatus.OK or not response.json().get(
503-
"version"
504-
):
505-
raise AssertionError
506-
507-
if response.status_code == http.HTTPStatus.UNAUTHORIZED:
508-
self._error("Unauthorised token")
509-
return False
510-
511-
except Exception as err:
512-
self._error(f"Exception retrieving server version: {str(err)}")
513-
return False
514-
return True
515-
516485
@skip_if_failed("_aborted", "_suppress_errors", False)
517486
def get_abort_status(self) -> bool:
518487
logger.debug("Retrieving alert status")

0 commit comments

Comments
 (0)