sklik rozsireni na typove metody
This commit is contained in:
@@ -133,6 +133,58 @@ class SklikClient:
|
||||
full_args = [self._user_struct()] + list(args or [])
|
||||
return await self._call(method, full_args)
|
||||
|
||||
async def fetch_list(
|
||||
self,
|
||||
entity: str,
|
||||
restriction_filter: dict[str, Any],
|
||||
display_columns: list[str] | None = None,
|
||||
) -> dict:
|
||||
"""Page through ``{entity}.list`` and collect every row.
|
||||
|
||||
``{entity}.list`` takes ``(restrictionFilter, displayOptions)`` and
|
||||
returns the rows under a key named after the entity (``campaigns``,
|
||||
``groups``, ``ads``). Paging is offset/limit, same as readReport.
|
||||
"""
|
||||
rows: list[Any] = []
|
||||
offset = 0
|
||||
pages = 0
|
||||
total: int | None = None
|
||||
|
||||
while True:
|
||||
display: dict[str, Any] = {
|
||||
"offset": offset,
|
||||
"limit": config.SKLIK_LIST_PAGE_LIMIT,
|
||||
}
|
||||
if display_columns:
|
||||
display["displayColumns"] = display_columns
|
||||
|
||||
page = await self.call(f"{entity}.list", [restriction_filter, display])
|
||||
batch = page.get(entity) or []
|
||||
rows.extend(batch)
|
||||
if total is None and isinstance(page.get("totalCount"), int):
|
||||
total = page["totalCount"]
|
||||
|
||||
pages += 1
|
||||
offset += config.SKLIK_LIST_PAGE_LIMIT
|
||||
if len(batch) < config.SKLIK_LIST_PAGE_LIMIT:
|
||||
break
|
||||
if pages >= config.SKLIK_LIST_MAX_PAGES:
|
||||
logger.warning(
|
||||
"Sklik %s.list hit the %d-page safety cap (collected %d "
|
||||
"rows); result is truncated.",
|
||||
entity,
|
||||
config.SKLIK_LIST_MAX_PAGES,
|
||||
len(rows),
|
||||
)
|
||||
break
|
||||
|
||||
return {
|
||||
"totalCount": total if total is not None else len(rows),
|
||||
"returnedCount": len(rows),
|
||||
"truncated": pages >= config.SKLIK_LIST_MAX_PAGES,
|
||||
entity: rows,
|
||||
}
|
||||
|
||||
async def fetch_report(
|
||||
self, entity: str, report_args: list[Any]
|
||||
) -> dict:
|
||||
|
||||
Reference in New Issue
Block a user