Skip to content

Commit f6a7139

Browse files
committed
client: healthcheck in _load
1 parent 48eba6e commit f6a7139

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

mpcontribs-client/mpcontribs/client/__init__.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import plotly.io as pio
1111
import itertools
1212
import functools
13+
import requests
1314

15+
from requests.exceptions import RequestException
1416
from bravado_core.param import Param
1517
from bson.objectid import ObjectId
1618
from typing import Union, Type, List
@@ -75,6 +77,9 @@
7577
SUPPORTED_FILETYPES = (Gz, Jpeg, Png, Gif, Tiff)
7678
SUPPORTED_MIMES = [t().mime for t in SUPPORTED_FILETYPES]
7779
DEFAULT_DOWNLOAD_DIR = Path.home() / "mpcontribs-downloads"
80+
EMPTY_SPEC_DICT = {
81+
"swagger": "2.0", "paths": {}, "info": {"title": "Swagger", "version": "0.0"},
82+
}
7883

7984
j2h = Json2Html()
8085
pd.options.plotting.backend = "plotly"
@@ -380,13 +385,21 @@ def _load(protocol, host, headers_json, project):
380385
spec_dict = ujson.loads(apispec.read_bytes())
381386
print(f"Specs for {origin_url} re-loaded from {apispec}.")
382387
else:
383-
loader = Loader(http_client)
384-
spec_dict = loader.load_spec(origin_url)
388+
try:
389+
if requests.options(f"{url}/healthcheck").status_code == 200:
390+
loader = Loader(http_client)
391+
spec_dict = loader.load_spec(origin_url)
385392

386-
with apispec.open("w") as f:
387-
ujson.dump(spec_dict, f)
393+
with apispec.open("w") as f:
394+
ujson.dump(spec_dict, f)
388395

389-
print(f"Specs for {origin_url} saved as {apispec}.")
396+
print(f"Specs for {origin_url} saved as {apispec}.")
397+
else:
398+
spec_dict = EMPTY_SPEC_DICT
399+
print(f"Specs not loaded: Healthcheck for {url} failed!")
400+
except RequestException:
401+
spec_dict = EMPTY_SPEC_DICT
402+
print(f"Specs not loaded: Could not connect to {url}!")
390403

391404
spec_dict["host"] = host
392405
spec_dict["schemes"] = [protocol]
@@ -401,9 +414,11 @@ def _load(protocol, host, headers_json, project):
401414
for key in set(bravado_config._fields).intersection(set(config)):
402415
del config[key]
403416
config["bravado"] = bravado_config
404-
405417
swagger_spec = Spec.from_dict(spec_dict, origin_url, http_client, config)
406418

419+
if not spec_dict["paths"]:
420+
return swagger_spec
421+
407422
# expand regex-based query parameters for `data` columns
408423
session = get_session()
409424
query = {"name": project} if project else {}
@@ -416,7 +431,7 @@ def _load(protocol, host, headers_json, project):
416431
session.close()
417432

418433
if not resp or not resp["data"]:
419-
return swagger_spec # skip in tests
434+
return swagger_spec
420435

421436
if project and not resp["data"]:
422437
raise ValueError(f"{project} doesn't exist, or access denied!")

0 commit comments

Comments
 (0)