Skip to content

Commit 98c4980

Browse files
authored
feat: speed up the packages api (#396)
The Connect Packages API (v1/packages) supports uncapped page size. By increasing the page size from the default of 500 to 1,000,000, there is a significant speed up when fetching all packages. This is due to a reduction in the number of round trips to the Connect server. The tradeoff is a larger payload. Since the Python requests package does not limit the HTTP GET request-response body size, we are unconcerned with the larger payload.
1 parent a41b81d commit 98c4980

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

src/posit/connect/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,9 @@ def oauth(self) -> OAuth:
349349
@property
350350
@requires(version="2024.11.0")
351351
def packages(self) -> Packages:
352-
return _PaginatedResourceSequence(self._ctx, "v1/packages", uid="name")
352+
return _PaginatedResourceSequence(
353+
self._ctx, "v1/packages", uid="name", page_size=1_000_000
354+
)
353355

354356
@property
355357
def system(self) -> System:

src/posit/connect/paginator.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,14 @@ class Paginator:
4343
"""
4444

4545
def __init__(
46-
self,
47-
ctx: Context,
48-
path: str,
49-
params: dict | None = None,
46+
self, ctx: Context, path: str, params: dict | None = None, page_size: int | None = None
5047
) -> None:
5148
if params is None:
5249
params = {}
5350
self._ctx = ctx
5451
self._path = path
5552
self._params = params
53+
self._page_size = page_size or _MAX_PAGE_SIZE
5654

5755
def fetch_results(self) -> List[dict]:
5856
"""
@@ -109,7 +107,7 @@ def fetch_page(self, page_number: int) -> Page:
109107
params = {
110108
**self._params,
111109
"page_number": page_number,
112-
"page_size": _MAX_PAGE_SIZE,
110+
"page_size": self._page_size,
113111
}
114112
response = self._ctx.client.get(self._path, params=params)
115113
return Page(**response.json())

src/posit/connect/resources.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,12 @@ def find_by(self, **conditions) -> Any | None:
177177

178178

179179
class _PaginatedResourceSequence(_ResourceSequence):
180+
def __init__(self, ctx, path: str, *, uid: str = "guid", page_size: int | None = None):
181+
super().__init__(ctx, path, uid=uid)
182+
self._page_size = page_size
183+
180184
def fetch(self, **conditions):
181-
paginator = Paginator(self._ctx, self._path, dict(**conditions))
185+
paginator = Paginator(self._ctx, self._path, dict(**conditions), page_size=self._page_size)
182186
for page in paginator.fetch_pages():
183187
resources = []
184188
results = page.results

0 commit comments

Comments
 (0)