Skip to content

Commit

Permalink
Add sorting and projecting to private resters (#826)
Browse files Browse the repository at this point in the history
* Add sorting and projecting to private resters

* Linting
  • Loading branch information
munrojm committed Jul 7, 2023
1 parent c32d071 commit 1989f03
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
35 changes: 31 additions & 4 deletions mp_api/client/routes/_messages.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import List
from typing import List, Optional

from emmet.core._messages import MessagesDoc, MessageType

Expand Down Expand Up @@ -39,16 +39,43 @@ def set_message(

return self._post_resource(body=d).get("data")

def get_messages(self, last_updated: datetime): # pragma: no cover
def get_messages(
self,
last_updated: datetime,
sort_fields: Optional[List[str]] = None,
num_chunks: Optional[int] = None,
chunk_size: int = 1000,
all_fields: bool = True,
fields: Optional[List[str]] = None,
): # pragma: no cover
"""Get user settings.
Args:
last_updated: Datetime to use to query for newer messages
last_updated (datetime): Datetime to use to query for newer messages
sort_fields (List[str]): Fields used to sort results. Prefix with '-' to sort in descending order.
num_chunks (int): Maximum number of chunks of data to yield. None will yield all possible.
chunk_size (int): Number of data entries per chunk.
all_fields (bool): Whether to return all fields in the document. Defaults to True.
fields (List[str]): List of fields to project.
Returns:
Dictionary with messages data
Raises:
MPRestError.
"""
return self._search(last_updated=last_updated)
query_params = {}

if sort_fields:
query_params.update(
{"_sort_fields": ",".join([s.strip() for s in sort_fields])}
)

return self._search(
last_updated=last_updated,
num_chunks=num_chunks,
chunk_size=chunk_size,
all_fields=all_fields,
fields=fields,
**query_params
)
5 changes: 3 additions & 2 deletions mp_api/client/routes/_user_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ def patch_user_time_settings(self, consumer_id, time): # pragma: no cover
params={"consumer_id": consumer_id},
).get("data")

def get_user_settings(self, consumer_id): # pragma: no cover
def get_user_settings(self, consumer_id, fields): # pragma: no cover
"""Get user settings.
Args:
consumer_id: Consumer ID for the user
fields: List of fields to project
Returns:
Dictionary with consumer_id and settings.
Raises:
MPRestError.
"""
return self.get_data_by_id(consumer_id)
return self.get_data_by_id(consumer_id, fields)

0 comments on commit 1989f03

Please sign in to comment.