Skip to content

Commit 570b238

Browse files
feat: add pagination to project_links endpoint
1 parent 9c2cbb4 commit 570b238

3 files changed

Lines changed: 51 additions & 8 deletions

File tree

components/renku_data_services/data_connectors/api.spec.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,42 @@ paths:
289289
description: the ID of the data connector
290290
get:
291291
summary: Get all links from a given data connector to projects
292+
parameters:
293+
- in: query
294+
description: query parameters
295+
name: params
296+
style: form
297+
explode: true
298+
schema:
299+
$ref: "#/components/schemas/PaginationRequest"
292300
responses:
293301
"200":
294302
description: List of data connector to project links
295303
content:
296304
application/json:
297305
schema:
298306
$ref: "#/components/schemas/DataConnectorToProjectLinksList"
307+
headers:
308+
page:
309+
description: The index of the current page (starting at 1).
310+
required: true
311+
schema:
312+
type: integer
313+
per-page:
314+
description: The number of items per page.
315+
required: true
316+
schema:
317+
type: integer
318+
total:
319+
description: The total number of items.
320+
required: true
321+
schema:
322+
type: integer
323+
total-pages:
324+
description: The total number of pages.
325+
required: true
326+
schema:
327+
type: integer
299328
default:
300329
$ref: "#/components/responses/Error"
301330
tags:

components/renku_data_services/data_connectors/blueprints.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,16 +312,20 @@ def get_all_project_links(self) -> BlueprintFactoryResponse:
312312
"""List all links from a given data connector to projects."""
313313

314314
@authenticate(self.authenticator)
315+
@paginate
315316
async def _get_all_project_links(
316317
_: Request,
317318
user: base_models.APIUser,
318319
data_connector_id: ULID,
319-
) -> JSONResponse:
320-
links = await self.data_connector_repo.get_links_from(user=user, data_connector_id=data_connector_id)
321-
return validated_json(
322-
apispec.DataConnectorToProjectLinksList,
323-
[self._dump_data_connector_to_project_link(link) for link in links],
320+
pagination: PaginationRequest,
321+
) -> tuple[list[dict[str, Any]], int]:
322+
links, total_num = await self.data_connector_repo.get_links_from(
323+
user=user, data_connector_id=data_connector_id, pagination=pagination
324324
)
325+
return [
326+
validate_and_dump(apispec.DataConnectorToProjectLink, self._dump_data_connector_to_project_link(link))
327+
for link in links
328+
], total_num
325329

326330
return "/data_connectors/<data_connector_id:ulid>/project_links", ["GET"], _get_all_project_links
327331

components/renku_data_services/data_connectors/db.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,8 @@ async def get_data_connector_permissions(
612612
return permissions
613613

614614
async def get_links_from(
615-
self, user: base_models.APIUser, data_connector_id: ULID
616-
) -> list[models.DataConnectorToProjectLink]:
615+
self, user: base_models.APIUser, data_connector_id: ULID, pagination: PaginationRequest
616+
) -> tuple[list[models.DataConnectorToProjectLink], int]:
617617
"""Get links from a given data connector."""
618618
authorized = await self.authz.has_permission(user, ResourceType.data_connector, data_connector_id, Scope.READ)
619619
if not authorized:
@@ -628,10 +628,20 @@ async def get_links_from(
628628
select(schemas.DataConnectorToProjectLinkORM)
629629
.where(schemas.DataConnectorToProjectLinkORM.data_connector_id == data_connector_id)
630630
.where(schemas.DataConnectorToProjectLinkORM.project_id.in_(project_ids))
631+
.limit(pagination.per_page)
632+
.offset(pagination.offset)
633+
.order_by(schemas.DataConnectorToProjectLinkORM.id.desc())
634+
)
635+
stmt_count = (
636+
select(func.count())
637+
.select_from(schemas.DataConnectorToProjectLinkORM)
638+
.where(schemas.DataConnectorToProjectLinkORM.data_connector_id == data_connector_id)
639+
.where(schemas.DataConnectorToProjectLinkORM.project_id.in_(project_ids))
631640
)
632641
result = await session.scalars(stmt)
633642
links_orm = result.all()
634-
return [link.dump() for link in links_orm]
643+
total_elements = await session.scalar(stmt_count) or 0
644+
return [link.dump() for link in links_orm], total_elements
635645

636646
async def get_links_to(
637647
self, user: base_models.APIUser, project_id: ULID

0 commit comments

Comments
 (0)