diff --git a/pymatgen/ext/matproj.py b/pymatgen/ext/matproj.py index e58e8e94e03..80e1aa1f1c1 100644 --- a/pymatgen/ext/matproj.py +++ b/pymatgen/ext/matproj.py @@ -552,7 +552,7 @@ def get_entries( props.append("structure") if not isinstance(chemsys_formula_id_criteria, dict): - criteria = MPRester.parse_criteria(chemsys_formula_id_criteria) + criteria = _MPResterLegacy.parse_criteria(chemsys_formula_id_criteria) else: criteria = chemsys_formula_id_criteria data = self.query(criteria, props) @@ -1897,6 +1897,27 @@ def get_initial_structures_by_material_id( return structures +def is_new_api(api_key) -> bool: + """ + Checks whether API key belongs to new or legacy API. + + Returns: + True if the API key is a valid one for the new Materials Project API. + """ + session = requests.Session() + session.headers = {"x-api-key": api_key} + try: + # This is a crude way to check the API key validity. But so be it for now. + response = session.get("https://api.materialsproject.org/materials/mp-262?_fields=formula_pretty") + if response.status_code == 200: + return True + else: + return False + except Exception: + # Default to legacy behavior on any errors. + return False + + class MPRester: """ A class to conveniently interface with the Materials Project REST @@ -1937,39 +1958,19 @@ def __init__(self, *args, **kwargs): self.session = requests.Session() self.session.headers = {"x-api-key": self.api_key} - if self.is_new_api(): - self.mpr_mapped = _MPResterNew(*args, **kwargs) + if is_new_api(self.api_key): + self._mpr_mapped = _MPResterNew(*args, **kwargs) else: - self.mpr_mapped = _MPResterLegacy(*args, **kwargs) + self._mpr_mapped = _MPResterLegacy(*args, **kwargs) + for d in dir(self._mpr_mapped): + if not d.startswith("__"): + setattr(self, d, getattr(self._mpr_mapped, d)) def __enter__(self): - return self.mpr_mapped + return self._mpr_mapped def __exit__(self, exc_type, exc_val, exc_tb): - self.mpr_mapped.__exit__(exc_type, exc_val, exc_tb) - - def is_new_api(self) -> bool: - """ - Checks whether API key belongs to new or legacy API. - - Returns: - True if the API key is a valid one for the new Materials Project API. - """ - session = requests.Session() - session.headers = {"x-api-key": self.api_key} - try: - # This is a crude way to check the API key validity. But so be it for now. - response = session.get("https://api.materialsproject.org/materials/mp-262?_fields=formula_pretty") - if response.status_code == 200: - return True - else: - return False - except Exception: - # Default to legacy behavior on any errors. - return False - - def __getattr__(self, name): - return getattr(self.mpr_mapped, name) + self._mpr_mapped.__exit__(exc_type, exc_val, exc_tb) class MPRestError(Exception):