Skip to content

Commit f5afa19

Browse files
committed
feat: add fields methods to QuerySet
1 parent add9d18 commit f5afa19

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

tableauserverclient/server/endpoint/endpoint.py

+39
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
TypeVar,
1515
Union,
1616
)
17+
from typing_extensions import Self
1718

1819
from tableauserverclient.models.pagination_item import PaginationItem
1920
from tableauserverclient.server.request_options import RequestOptions
@@ -353,3 +354,41 @@ def paginate(self, **kwargs) -> QuerySet[T]:
353354
@abc.abstractmethod
354355
def get(self, request_options: Optional[RequestOptions] = None) -> tuple[list[T], PaginationItem]:
355356
raise NotImplementedError(f".get has not been implemented for {self.__class__.__qualname__}")
357+
358+
def fields(self: Self, *fields: str) -> QuerySet:
359+
"""
360+
Add fields to the request options. If no fields are provided, the
361+
default fields will be used. If fields are provided, the default fields
362+
will be used in addition to the provided fields.
363+
364+
Parameters
365+
----------
366+
fields : str
367+
The fields to include in the request options.
368+
369+
Returns
370+
-------
371+
QuerySet
372+
"""
373+
queryset = QuerySet(self)
374+
queryset.request_options.fields |= set(fields) | set(("_default_",))
375+
return queryset
376+
377+
def only_fields(self: Self, *fields: str) -> QuerySet:
378+
"""
379+
Add fields to the request options. If no fields are provided, the
380+
default fields will be used. If fields are provided, the default fields
381+
will be replaced by the provided fields.
382+
383+
Parameters
384+
----------
385+
fields : str
386+
The fields to include in the request options.
387+
388+
Returns
389+
-------
390+
QuerySet
391+
"""
392+
queryset = QuerySet(self)
393+
queryset.request_options.fields |= set(fields)
394+
return queryset

tableauserverclient/server/query.py

+36
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,42 @@ def paginate(self: Self, **kwargs) -> Self:
208208
self.request_options.pagesize = kwargs["page_size"]
209209
return self
210210

211+
def fields(self: Self, *fields: str) -> Self:
212+
"""
213+
Add fields to the request options. If no fields are provided, the
214+
default fields will be used. If fields are provided, the default fields
215+
will be used in addition to the provided fields.
216+
217+
Parameters
218+
----------
219+
fields : str
220+
The fields to include in the request options.
221+
222+
Returns
223+
-------
224+
QuerySet
225+
"""
226+
self.request_options.fields |= set(fields) | set(("_default_"))
227+
return self
228+
229+
def only_fields(self: Self, *fields: str) -> Self:
230+
"""
231+
Add fields to the request options. If no fields are provided, the
232+
default fields will be used. If fields are provided, the default fields
233+
will be replaced by the provided fields.
234+
235+
Parameters
236+
----------
237+
fields : str
238+
The fields to include in the request options.
239+
240+
Returns
241+
-------
242+
QuerySet
243+
"""
244+
self.request_options.fields |= set(fields)
245+
return self
246+
211247
@staticmethod
212248
def _parse_shorthand_filter(key: str) -> tuple[str, str]:
213249
tokens = key.split("__", 1)

test/test_request_option.py

+10
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,13 @@ def test_language_export(self) -> None:
368368

369369
resp = self.server.users.get_request(url, request_object=opts)
370370
self.assertTrue(re.search("language=en-us", resp.request.query))
371+
372+
def test_queryset_fields(self) -> None:
373+
loop = self.server.users.fields("id")
374+
assert "id" in loop.request_options.fields
375+
assert "_default_" in loop.request_options.fields
376+
377+
def test_queryset_only_fields(self) -> None:
378+
loop = self.server.users.only_fields("id")
379+
assert "id" in loop.request_options.fields
380+
assert "_default_" not in loop.request_options.fields

0 commit comments

Comments
 (0)