Skip to content

Commit

Permalink
Refactoring to ensure that the new MPRester supports autocompletion of
Browse files Browse the repository at this point in the history
methods.
  • Loading branch information
Shyue Ping Ong committed Jul 25, 2022
1 parent 4f2424b commit 81f5020
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions pymatgen/ext/matproj.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 81f5020

Please sign in to comment.