Skip to content

Commit

Permalink
Rename query_by_task_id to get_document_by_id
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhorton committed Mar 26, 2021
1 parent 5e1d8f7 commit 566f9b4
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/mp_api/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ def query(
criteria=criteria, fields=fields, monty_decode=monty_decode, suburl=suburl
).get("data")

def query_by_task_id(
def get_document_by_id(
self,
task_id,
document_id,
fields: Optional[List[str]] = None,
monty_decode: bool = True,
version: Optional[str] = None,
Expand All @@ -267,7 +267,7 @@ def query_by_task_id(
Query the endpoint for a single document.
Arguments:
task_id: the unique key, typically a task_id
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
monty_decode: Decode the data using monty into python objects
version: For supported endpoints, specify a specific database
Expand All @@ -277,6 +277,10 @@ def query_by_task_id(
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 fields is None:
criteria = {"all_fields": True, "limit": 1} # type: dict
else:
Expand All @@ -289,19 +293,23 @@ def query_by_task_id(
fields = (fields,)

results = self.query(
criteria=criteria, fields=fields, monty_decode=monty_decode, suburl=task_id
criteria=criteria, fields=fields, monty_decode=monty_decode, suburl=document_id
)

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

def query_by_task_id(self, *args, **kwargs):
print("query_by_task_id has been renamed to get_document_by_id to be more general")
return self.get_document_by_id(*args, **kwargs)

def count(self, criteria: Optional[Dict] = None) -> Union[int, str]:
"""
Return a count of total documents.
Expand Down Expand Up @@ -335,7 +343,6 @@ def __str__(self):
return (
f"{self.__class__.__name__} connected to {self.endpoint}\n\n"
f"Available fields: {', '.join(self.available_fields)}\n\n"
f"Available documents: {self.count()}"
)


Expand Down

0 comments on commit 566f9b4

Please sign in to comment.