Skip to content
Open
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
7 changes: 5 additions & 2 deletions mp_api/client/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from tqdm.auto import tqdm
from urllib3.util.retry import Retry

from mp_api.client.core.exceptions import MPRestError
from mp_api.client.core.exceptions import MPRestError, _emit_status_warning
from mp_api.client.core.settings import MAPI_CLIENT_SETTINGS
from mp_api.client.core.utils import (
load_json,
Expand Down Expand Up @@ -222,7 +222,10 @@ def _get_database_version(endpoint):

Returns: database version as a string
"""
return requests.get(url=endpoint + "heartbeat").json()["db_version"]
if (get_resp := requests.get(url=endpoint + "heartbeat")).status_code == 403:
_emit_status_warning()
return
return get_resp.json()["db_version"]

def _post_resource(
self,
Expand Down
12 changes: 12 additions & 0 deletions mp_api/client/core/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
"""Define custom exceptions and warnings for the client."""
from __future__ import annotations

import warnings


class MPRestError(Exception):
"""Raised when the query has problems, e.g., bad query format."""


class MPRestWarning(Warning):
"""Raised when a query is malformed but interpretable."""


def _emit_status_warning() -> None:
"""Emit a warning if client can't hear a heartbeat."""
warnings.warn(
"Cannot listen to heartbeat, check Materials Project "
"status page: https://status.materialsproject.org/",
category=MPRestWarning,
stacklevel=2,
)
37 changes: 25 additions & 12 deletions mp_api/client/mprester.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from requests import Session, get

from mp_api.client.core import BaseRester, MPRestError, MPRestWarning
from mp_api.client.core import BaseRester
from mp_api.client.core._oxygen_evolution import OxygenEvolution
from mp_api.client.core.exceptions import (
MPRestError,
MPRestWarning,
_emit_status_warning,
)
from mp_api.client.core.settings import MAPI_CLIENT_SETTINGS
from mp_api.client.core.utils import (
LazyImport,
Expand Down Expand Up @@ -161,14 +166,15 @@ def __init__(
)

# Check if emmet version of server is compatible
emmet_version = MPRester.get_emmet_version(self.endpoint)

if version.parse(emmet_version.base_version) < version.parse(
MAPI_CLIENT_SETTINGS.MIN_EMMET_VERSION
if (emmet_version := MPRester.get_emmet_version(self.endpoint)) and (
version.parse(emmet_version.base_version)
< version.parse(MAPI_CLIENT_SETTINGS.MIN_EMMET_VERSION)
):
warnings.warn(
"The installed version of the mp-api client may not be compatible with the API server. "
"Please install a previous version if any problems occur."
"Please install a previous version if any problems occur.",
category=MPRestWarning,
stacklevel=2,
)

if notify_db_version:
Expand Down Expand Up @@ -311,7 +317,7 @@ def get_structure_by_material_id(

return structure_data

def get_database_version(self):
def get_database_version(self) -> str | None:
"""The Materials Project database is periodically updated and has a
database version associated with it. When the database is updated,
consolidated data (information about "a material") may and does
Expand All @@ -324,20 +330,27 @@ def get_database_version(self):

Returns: database version as a string
"""
return get(url=self.endpoint + "heartbeat").json()["db_version"]
if (get_resp := get(url=self.endpoint + "heartbeat")).status_code == 403:
_emit_status_warning()
return
return get_resp.json()["db_version"]

@staticmethod
@cache
def get_emmet_version(endpoint):
def get_emmet_version(endpoint) -> str | None:
"""Get the latest version emmet-core and emmet-api used in the
current API service.

Returns: version as a string
"""
response = get(url=endpoint + "heartbeat").json()
get_resp = get(url=endpoint + "heartbeat")

if get_resp.status_code == 403:
_emit_status_warning()
return

error = response.get("error", None)
if error:
response = get_resp.json()
if error := response.get("error", None):
raise MPRestError(error)

return version.parse(response["version"])
Expand Down
2 changes: 1 addition & 1 deletion mp_api/client/routes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from mp_api.client.core.utils import LazyImport

GENERIC_RESTERS = {
k: LazyImport(f"mp_api.client.routes.{k}.{v}")
k: LazyImport(f"mp_api.client.routes._server.{v}")
for k, v in {
"_general_store": "GeneralStoreRester",
"_messages": "MessagesRester",
Expand Down
44 changes: 0 additions & 44 deletions mp_api/client/routes/_general_store.py

This file was deleted.

81 changes: 0 additions & 81 deletions mp_api/client/routes/_messages.py

This file was deleted.

Loading