Skip to content

Commit 96cf642

Browse files
author
Jason Munro
authoredFeb 14, 2024··
Deprecate get_data_by_id (#882)
* Remove get_data_by_id * Add search method for DOI rester * Big endpoint updates * Remove charge density rester * Fix materials structure method * Remove charge density references * More charge density fixes * Add type ignore * Fix task_id method * Fix references method * Fix phonon methods * Fix phonon and task methods * Fix remaining type issues in MPRester * Remove get_bv refs in es methods * Fix user settings method * Fix get_by ref in molecules * Remove remaining references to get_data_by_id * Deprecate get_data_by_id * Linting * Remove charge density rester tests * Remove generic get method tests * Remove warning expectation * Linting * Fix method reference in test * More method typo fixes * More materials typos * Linting * Fix boto3 warning * Fix generic search tests * Linting * Update task_ids arg * Fix remaining search methods * Fix thermo and tasks * Linting
1 parent 8a84e76 commit 96cf642

39 files changed

+566
-739
lines changed
 

‎mp_api/client/core/client.py

+34-76
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ def __init__(
128128
self._s3_resource = None
129129

130130
self.document_model = (
131-
api_sanitize(self.document_model) if self.document_model is not None else None # type: ignore
131+
api_sanitize(self.document_model)
132+
if self.document_model is not None
133+
else None # type: ignore
132134
)
133135

134136
@property
@@ -237,7 +239,9 @@ def _post_resource(
237239
if isinstance(data["data"], dict):
238240
data["data"] = self.document_model.parse_obj(data["data"]) # type: ignore
239241
elif isinstance(data["data"], list):
240-
data["data"] = [self.document_model.parse_obj(d) for d in data["data"]] # type: ignore
242+
data["data"] = [
243+
self.document_model.parse_obj(d) for d in data["data"]
244+
] # type: ignore
241245

242246
return data
243247

@@ -307,7 +311,9 @@ def _patch_resource(
307311
if isinstance(data["data"], dict):
308312
data["data"] = self.document_model.parse_obj(data["data"]) # type: ignore
309313
elif isinstance(data["data"], list):
310-
data["data"] = [self.document_model.parse_obj(d) for d in data["data"]] # type: ignore
314+
data["data"] = [
315+
self.document_model.parse_obj(d) for d in data["data"]
316+
] # type: ignore
311317

312318
return data
313319

@@ -967,79 +973,6 @@ def _query_resource_data(
967973
num_chunks=1,
968974
).get("data")
969975

970-
def get_data_by_id(
971-
self,
972-
document_id: str,
973-
fields: list[str] | None = None,
974-
) -> T:
975-
"""Query the endpoint for a single document.
976-
977-
Arguments:
978-
document_id: the unique key for this kind of document, typically a task_id
979-
fields: list of fields to return, by default will return all fields
980-
981-
Returns:
982-
A single document.
983-
"""
984-
if document_id is None:
985-
raise ValueError(
986-
"Please supply a specific ID. You can use the query method to find "
987-
"ids of interest."
988-
)
989-
990-
if self.primary_key in ["material_id", "task_id"]:
991-
validate_ids([document_id])
992-
993-
if fields is None:
994-
criteria = {"_all_fields": True, "_limit": 1} # type: dict
995-
else:
996-
criteria = {"_limit": 1}
997-
998-
if isinstance(fields, str): # pragma: no cover
999-
fields = (fields,)
1000-
1001-
results = [] # type: List
1002-
1003-
try:
1004-
results = self._query_resource_data(criteria=criteria, fields=fields, suburl=document_id) # type: ignore
1005-
except MPRestError:
1006-
if self.primary_key == "material_id":
1007-
# see if the material_id has changed, perhaps a task_id was supplied
1008-
# this should likely be re-thought
1009-
from mp_api.client.routes.materials.materials import MaterialsRester
1010-
1011-
with MaterialsRester(
1012-
api_key=self.api_key,
1013-
endpoint=self.base_endpoint,
1014-
use_document_model=False,
1015-
monty_decode=False,
1016-
session=self.session,
1017-
headers=self.headers,
1018-
) as mpr:
1019-
docs = mpr.search(task_ids=[document_id], fields=["material_id"])
1020-
1021-
if len(docs) > 0:
1022-
new_document_id = docs[0].get("material_id", None)
1023-
1024-
if new_document_id is not None:
1025-
warnings.warn(
1026-
f"Document primary key has changed from {document_id} to {new_document_id}, "
1027-
f"returning data for {new_document_id} in {self.suffix} route. "
1028-
)
1029-
1030-
results = self._query_resource_data(
1031-
criteria=criteria, fields=fields, suburl=new_document_id # type: ignore
1032-
)
1033-
1034-
if not results:
1035-
raise MPRestError(f"No result for record {document_id}.")
1036-
elif len(results) > 1: # pragma: no cover
1037-
raise ValueError(
1038-
f"Multiple records for {document_id}, this shouldn't happen. Please report as a bug."
1039-
)
1040-
else:
1041-
return results[0]
1042-
1043976
def _search(
1044977
self,
1045978
num_chunks: int | None = None,
@@ -1078,6 +1011,31 @@ def _search(
10781011
num_chunks=num_chunks,
10791012
)
10801013

1014+
def get_data_by_id(
1015+
self,
1016+
document_id: str,
1017+
fields: list[str] | None = None,
1018+
) -> T | dict:
1019+
warnings.warn(
1020+
"get_data_by_id is deprecated and will be removed soon. Please use the search method instead.",
1021+
DeprecationWarning,
1022+
stacklevel=2,
1023+
)
1024+
1025+
if self.primary_key in ["material_id", "task_id"]:
1026+
validate_ids([document_id])
1027+
1028+
if isinstance(fields, str): # pragma: no cover
1029+
fields = (fields,) # type: ignore
1030+
1031+
return self._search( # type: ignore
1032+
**{self.primary_key + "s": document_id},
1033+
num_chunks=1,
1034+
chunk_size=1,
1035+
all_fields=fields is None,
1036+
fields=fields,
1037+
)
1038+
10811039
def _get_all_documents(
10821040
self,
10831041
query_params,

0 commit comments

Comments
 (0)