Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate get_data_by_id #882

Merged
merged 34 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a913c47
Remove get_data_by_id
Jan 31, 2024
8f3d95d
Add search method for DOI rester
Jan 31, 2024
719722a
Big endpoint updates
Jan 31, 2024
0d38c1a
Remove charge density rester
Jan 31, 2024
57ac593
Fix materials structure method
Jan 31, 2024
0082646
Remove charge density references
Jan 31, 2024
449a81e
More charge density fixes
Jan 31, 2024
3610a56
Add type ignore
Jan 31, 2024
41827d4
Fix task_id method
Jan 31, 2024
b940687
Fix references method
Jan 31, 2024
f98c3be
Fix phonon methods
Jan 31, 2024
c520641
Fix phonon and task methods
Jan 31, 2024
cccfdbe
Fix remaining type issues in MPRester
Jan 31, 2024
343bb5c
Remove get_bv refs in es methods
Jan 31, 2024
8a6a7ba
Fix user settings method
Jan 31, 2024
32bd0e7
Fix get_by ref in molecules
Jan 31, 2024
1199f97
Remove remaining references to get_data_by_id
Jan 31, 2024
200722c
Deprecate get_data_by_id
Feb 7, 2024
c18a153
Linting
Feb 13, 2024
01cf5c4
Remove charge density rester tests
Feb 13, 2024
796b5bb
Remove generic get method tests
Feb 13, 2024
e7d8e25
Remove warning expectation
Feb 13, 2024
1d79579
Linting
Feb 13, 2024
48e2f3a
Fix method reference in test
Feb 13, 2024
c387ac3
More method typo fixes
Feb 13, 2024
ec01962
More materials typos
Feb 13, 2024
70bd644
Linting
Feb 13, 2024
180a27d
Fix boto3 warning
Feb 14, 2024
138dfc5
Fix generic search tests
Feb 14, 2024
75aa683
Linting
Feb 14, 2024
e79f4de
Update task_ids arg
Feb 14, 2024
f5e80ea
Fix remaining search methods
Feb 14, 2024
9642727
Fix thermo and tasks
Feb 14, 2024
893027c
Linting
Feb 14, 2024
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
110 changes: 34 additions & 76 deletions mp_api/client/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ def __init__(
self._s3_resource = None

self.document_model = (
api_sanitize(self.document_model) if self.document_model is not None else None # type: ignore
api_sanitize(self.document_model)
if self.document_model is not None
else None # type: ignore
)

@property
Expand Down Expand Up @@ -237,7 +239,9 @@ def _post_resource(
if isinstance(data["data"], dict):
data["data"] = self.document_model.parse_obj(data["data"]) # type: ignore
elif isinstance(data["data"], list):
data["data"] = [self.document_model.parse_obj(d) for d in data["data"]] # type: ignore
data["data"] = [
self.document_model.parse_obj(d) for d in data["data"]
] # type: ignore

return data

Expand Down Expand Up @@ -307,7 +311,9 @@ def _patch_resource(
if isinstance(data["data"], dict):
data["data"] = self.document_model.parse_obj(data["data"]) # type: ignore
elif isinstance(data["data"], list):
data["data"] = [self.document_model.parse_obj(d) for d in data["data"]] # type: ignore
data["data"] = [
self.document_model.parse_obj(d) for d in data["data"]
] # type: ignore

return data

Expand Down Expand Up @@ -967,79 +973,6 @@ def _query_resource_data(
num_chunks=1,
).get("data")

def get_data_by_id(
self,
document_id: str,
fields: list[str] | None = None,
) -> T:
"""Query the endpoint for a single document.
Arguments:
document_id: the unique key for this kind of document, typically a task_id
fields: list of fields to return, by default will return all fields
Returns:
A single document.
"""
if document_id is None:
raise ValueError(
"Please supply a specific ID. You can use the query method to find "
"ids of interest."
)

if self.primary_key in ["material_id", "task_id"]:
validate_ids([document_id])

if fields is None:
criteria = {"_all_fields": True, "_limit": 1} # type: dict
else:
criteria = {"_limit": 1}

if isinstance(fields, str): # pragma: no cover
fields = (fields,)

results = [] # type: List

try:
results = self._query_resource_data(criteria=criteria, fields=fields, suburl=document_id) # type: ignore
except MPRestError:
if self.primary_key == "material_id":
# see if the material_id has changed, perhaps a task_id was supplied
# this should likely be re-thought
from mp_api.client.routes.materials.materials import MaterialsRester

with MaterialsRester(
api_key=self.api_key,
endpoint=self.base_endpoint,
use_document_model=False,
monty_decode=False,
session=self.session,
headers=self.headers,
) as mpr:
docs = mpr.search(task_ids=[document_id], fields=["material_id"])

if len(docs) > 0:
new_document_id = docs[0].get("material_id", None)

if new_document_id is not None:
warnings.warn(
f"Document primary key has changed from {document_id} to {new_document_id}, "
f"returning data for {new_document_id} in {self.suffix} route. "
)

results = self._query_resource_data(
criteria=criteria, fields=fields, suburl=new_document_id # type: ignore
)

if not results:
raise MPRestError(f"No result for record {document_id}.")
elif len(results) > 1: # pragma: no cover
raise ValueError(
f"Multiple records for {document_id}, this shouldn't happen. Please report as a bug."
)
else:
return results[0]

def _search(
self,
num_chunks: int | None = None,
Expand Down Expand Up @@ -1078,6 +1011,31 @@ def _search(
num_chunks=num_chunks,
)

def get_data_by_id(
self,
document_id: str,
fields: list[str] | None = None,
) -> T | dict:
warnings.warn(
"get_data_by_id is deprecated and will be removed soon. Please use the search method instead.",
DeprecationWarning,
stacklevel=2,
)

if self.primary_key in ["material_id", "task_id"]:
validate_ids([document_id])

if isinstance(fields, str): # pragma: no cover
fields = (fields,) # type: ignore

return self._search( # type: ignore
**{self.primary_key + "s": document_id},
num_chunks=1,
chunk_size=1,
all_fields=fields is None,
fields=fields,
)

def _get_all_documents(
self,
query_params,
Expand Down
Loading
Loading