Skip to content
Open
Changes from all commits
Commits
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
24 changes: 20 additions & 4 deletions src/resdk/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ def _populate_resource(self, data: dict) -> BaseResource:
resolwe=self.resolwe, **data, initial_data_source=DataSource.SERVER
)

def _fetch(self):
"""Make request to the server and populate cache."""
def _fetch_data(self):
"""Fetch data from the server."""
if self._cache is not None:
# Already fetched.
return
Expand All @@ -248,12 +248,17 @@ def _fetch(self):

# Extract data from paginated response
if isinstance(items, dict) and "results" in items:
self._count = items["count"]
count = items["count"]
items = items["results"]
# Store count when list of objects is received without limit.
if isinstance(items, list) and self._limit is None:
self._count = len(items)
count = len(items)
return items, count

def _fetch(self):
"""Make request to the server and populate cache."""
items, count = self._fetch_data()
self._count = count
self._cache = [self._populate_resource(data) for data in items]

def clear_cache(self):
Expand All @@ -276,6 +281,17 @@ def count(self) -> int:
remaining = self._count - self._offset
return max(0, min(self._limit, remaining))

def values(self, fields: list[str]) -> list[dict]:
"""Return only values for the given fields.

Used to reduce the data transferred from the server.

TODO: Should we support pagination? How do we support it?
"""
field_query = self.filter(fields=fields)
items, _count = field_query._fetch_data()
return items

def get(self, *args, **kwargs):
"""Get object that matches given parameters.

Expand Down
Loading