Skip to content

Commit 64b3db1

Browse files
committed
Adding a check that API URL provided in login is valid (and show the API version to the user).
1 parent ba4216a commit 64b3db1

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

src/recodex_cli/utils/client_factory.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import typer
22
from recodex import client_factory
33
from recodex.client import Client
4+
from recodex.generated.swagger_client import ApiClient
5+
from recodex.generated.swagger_client.configuration import Configuration
46

57
from .cmd_utils import execute_with_verbosity
68
from .login_info import LoginInfo
@@ -24,6 +26,29 @@ def login(login_info: LoginInfo, verbose=False):
2426
print("Reusing API URL from session file.")
2527
login_info.api_url = user_context.api_url
2628

29+
# check the API URL is right
30+
try:
31+
# perform a HTTP GET request to the API URL to check if it is correct and reachable
32+
config = Configuration()
33+
config.host = login_info.api_url
34+
unauth_client = ApiClient(config)
35+
data, code, _ = unauth_client.call_api(
36+
'/v1/', 'GET', _return_http_data_only=False, response_type="object", _request_timeout=10)
37+
if code != 200:
38+
raise Exception(f"Unexpected response code {code} when connecting to the API URL {login_info.api_url}.")
39+
if (not data or type(data) is not dict or "project" not in data or data["project"] != "ReCodEx API" or
40+
"version" not in data):
41+
if verbose:
42+
print("Response:", end="")
43+
print(data)
44+
raise Exception(f"Unexpected response data when connecting to the API URL {login_info.api_url}.")
45+
46+
print(f'{login_info.api_url} refers to valid ReCodEx API version {data["version"]}')
47+
48+
except Exception as e:
49+
print(f"Could not connect to the provided API URL {login_info.api_url}.")
50+
raise e
51+
2752
# prompt the API URL and API token and overwrite any existing session file
2853
if login_info.use_token_prompt:
2954
if login_info.api_url is None:

0 commit comments

Comments
 (0)