2323from simvue .version import __version__
2424from simvue .api .request import (
2525 get as sv_get ,
26+ get_paginated ,
2627 post as sv_post ,
2728 put as sv_put ,
2829 delete as sv_delete ,
@@ -347,7 +348,7 @@ def new(cls, **_) -> Self:
347348 @classmethod
348349 def ids (
349350 cls , count : int | None = None , offset : int | None = None , ** kwargs
350- ) -> list [str ]:
351+ ) -> typing . Generator [str , None , None ]:
351352 """Retrieve a list of all object identifiers.
352353
353354 Parameters
@@ -357,17 +358,23 @@ def ids(
357358 offset : int | None, optional
358359 set start index for objects list
359360
360- Returns
361+ Yields
361362 -------
362- list[ str]
363+ str
363364 identifiers for all objects of this type.
364365 """
365366 _class_instance = cls (_read_only = True , _local = True )
366- if (_data := cls ._get_all_objects (count , offset , ** kwargs ).get ("data" )) is None :
367- raise RuntimeError (
368- f"Expected key 'data' for retrieval of { _class_instance .__class__ .__name__ .lower ()} s"
369- )
370- return [_entry ["id" ] for _entry in _data ]
367+ _count : int = 0
368+ for response in cls ._get_all_objects (offset ):
369+ if (_data := response .get ("data" )) is None :
370+ raise RuntimeError (
371+ f"Expected key 'data' for retrieval of { _class_instance .__class__ .__name__ .lower ()} s"
372+ )
373+ for entry in _data :
374+ yield entry ["id" ]
375+ _count += 1
376+ if count and _count > count :
377+ return
371378
372379 @classmethod
373380 @pydantic .validate_call
@@ -396,23 +403,19 @@ def get(
396403 Generator[tuple[str, SimvueObject | None], None, None]
397404 """
398405 _class_instance = cls (_read_only = True , _local = True )
399- if (
400- _data := cls ._get_all_objects (
401- count = count ,
402- offset = offset ,
403- ** kwargs ,
404- ).get ("data" )
405- ) is None :
406- raise RuntimeError (
407- f"Expected key 'data' for retrieval of { _class_instance .__class__ .__name__ .lower ()} s"
408- )
409-
410- for _entry in _data :
411- if not (_id := _entry .pop ("id" , None )):
406+ _count : int = 0
407+ for _response in cls ._get_all_objects (offset , ** kwargs ):
408+ if count and _count > count :
409+ return
410+ if (_data := _response .get ("data" )) is None :
412411 raise RuntimeError (
413- f"Expected key 'id ' for { _class_instance .__class__ .__name__ .lower ()} "
412+ f"Expected key 'data ' for retrieval of { _class_instance .__class__ .__name__ .lower ()} s "
414413 )
415- yield _id , cls (_read_only = True , identifier = _id , _local = True , ** _entry )
414+
415+ for entry in _data :
416+ _id = entry ["id" ]
417+ yield _id , cls (_read_only = True , identifier = _id , _local = True , ** entry )
418+ _count += 1
416419
417420 @classmethod
418421 def count (cls , ** kwargs ) -> int :
@@ -424,42 +427,34 @@ def count(cls, **kwargs) -> int:
424427 total from server database for current user.
425428 """
426429 _class_instance = cls (_read_only = True )
427- if (
428- _count := cls ._get_all_objects (count = None , offset = None , ** kwargs ).get (
429- "count"
430- )
431- ) is None :
432- raise RuntimeError (
433- f"Expected key 'count' for retrieval of { _class_instance .__class__ .__name__ .lower ()} s"
434- )
435- return _count
430+ _count_total : int = 0
431+ for _data in cls ._get_all_objects (** kwargs ):
432+ if not (_count := _data .get ("count" )):
433+ raise RuntimeError (
434+ f"Expected key 'count' for retrieval of { _class_instance .__class__ .__name__ .lower ()} s"
435+ )
436+ _count_total += _count
437+ return _count_total
436438
437439 @classmethod
438440 def _get_all_objects (
439- cls ,
440- count : int | None ,
441- offset : int | None ,
442- ** kwargs ,
443- ) -> dict [str , typing .Any ]:
441+ cls , offset : int | None , ** kwargs
442+ ) -> typing .Generator [dict , None , None ]:
444443 _class_instance = cls (_read_only = True )
445444 _url = f"{ _class_instance ._base_url } "
446- _params : dict [str , int | str ] = {"start" : offset , "count" : count }
447-
448- _response = sv_get (
449- _url ,
450- headers = _class_instance ._headers ,
451- params = _params | kwargs ,
452- )
453445
454446 _label = _class_instance .__class__ .__name__ .lower ()
455447 if _label .endswith ("s" ):
456448 _label = _label [:- 1 ]
457449
458- return get_json_from_response (
459- response = _response ,
460- expected_status = [http .HTTPStatus .OK ],
461- scenario = f"Retrieval of { _label } s" ,
462- )
450+ for response in get_paginated (
451+ _url , headers = _class_instance ._headers , offset = offset , ** kwargs
452+ ):
453+ yield get_json_from_response (
454+ response = response ,
455+ expected_status = [http .HTTPStatus .OK ],
456+ scenario = f"Retrieval of { _label } s" ,
457+ ) # type: ignore
463458
464459 def read_only (self , is_read_only : bool ) -> None :
465460 """Set whether this object is in read only state.
0 commit comments