From 53ec416325274cc5abac57b2027bda557bfa4cb4 Mon Sep 17 00:00:00 2001 From: Bearchitek Date: Tue, 13 May 2025 12:21:34 +0200 Subject: [PATCH 1/4] add partial_match to the count function --- infrahub_sdk/client.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/infrahub_sdk/client.py b/infrahub_sdk/client.py index 4f6aa8a..0269fbe 100644 --- a/infrahub_sdk/client.py +++ b/infrahub_sdk/client.py @@ -562,6 +562,7 @@ async def count( at: Timestamp | None = None, branch: str | None = None, timeout: int | None = None, + partial_match: bool = False, **kwargs: Any, ) -> int: """Return the number of nodes of a given kind.""" @@ -572,8 +573,13 @@ async def count( if at: at = Timestamp(at) - response = await self.execute_graphql( - query=Query(query={schema.kind: {"count": None, "@filters": filters}}).render(), + data = {"count": None, "@filters": filters} + + if partial_match: + data["@filters"]["partial_match"] = True + + response = self.execute_graphql( + query=Query(query={schema.kind: data}).render(), branch_name=branch, at=at, timeout=timeout, @@ -801,7 +807,7 @@ async def process_batch() -> tuple[list[InfrahubNode], list[InfrahubNode]]: nodes = [] related_nodes = [] batch_process = await self.create_batch() - count = await self.count(kind=schema.kind, **filters) + count = await self.count(kind=schema.kind, partial_match=partial_match, **filters) total_pages = (count + pagination_size - 1) // pagination_size for page_number in range(1, total_pages + 1): @@ -1683,6 +1689,7 @@ def count( at: Timestamp | None = None, branch: str | None = None, timeout: int | None = None, + partial_match: bool = False, **kwargs: Any, ) -> int: """Return the number of nodes of a given kind.""" @@ -1693,8 +1700,12 @@ def count( if at: at = Timestamp(at) + data = {"count": None, "@filters": filters} + if partial_match: + data["@filters"]["partial_match"] = True + response = self.execute_graphql( - query=Query(query={schema.kind: {"count": None, "@filters": filters}}).render(), + query=Query(query={schema.kind: data}).render(), branch_name=branch, at=at, timeout=timeout, @@ -1957,7 +1968,7 @@ def process_batch() -> tuple[list[InfrahubNodeSync], list[InfrahubNodeSync]]: related_nodes = [] batch_process = self.create_batch() - count = self.count(kind=schema.kind, **filters) + count = self.count(kind=schema.kind, partial_match=partial_match, **filters) total_pages = (count + pagination_size - 1) // pagination_size for page_number in range(1, total_pages + 1): From a6360b0aced54a1ba8eb56d258bffb559000945e Mon Sep 17 00:00:00 2001 From: Bearchitek Date: Tue, 13 May 2025 12:34:20 +0200 Subject: [PATCH 2/4] typo --- infrahub_sdk/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/infrahub_sdk/client.py b/infrahub_sdk/client.py index 0269fbe..541820f 100644 --- a/infrahub_sdk/client.py +++ b/infrahub_sdk/client.py @@ -578,7 +578,7 @@ async def count( if partial_match: data["@filters"]["partial_match"] = True - response = self.execute_graphql( + response = await self.execute_graphql( query=Query(query={schema.kind: data}).render(), branch_name=branch, at=at, From d868e9c599cc3546596a44a8232b5602c6630c3c Mon Sep 17 00:00:00 2001 From: Bearchitek Date: Mon, 19 May 2025 09:30:06 +0200 Subject: [PATCH 3/4] refactor for mypy --- infrahub_sdk/client.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/infrahub_sdk/client.py b/infrahub_sdk/client.py index 541820f..5cfe6c8 100644 --- a/infrahub_sdk/client.py +++ b/infrahub_sdk/client.py @@ -566,17 +566,20 @@ async def count( **kwargs: Any, ) -> int: """Return the number of nodes of a given kind.""" - filters = kwargs - schema = await self.schema.get(kind=kind, branch=branch) + filters: dict[str, Any] = dict(kwargs) + + if partial_match: + filters["partial_match"] = True + schema = await self.schema.get(kind=kind, branch=branch) branch = branch or self.default_branch if at: at = Timestamp(at) - data = {"count": None, "@filters": filters} - - if partial_match: - data["@filters"]["partial_match"] = True + data: dict[str, Any] = { + "count": None, + "@filters": filters, + } response = await self.execute_graphql( query=Query(query={schema.kind: data}).render(), @@ -1693,16 +1696,20 @@ def count( **kwargs: Any, ) -> int: """Return the number of nodes of a given kind.""" - filters = kwargs - schema = self.schema.get(kind=kind, branch=branch) + filters: dict[str, Any] = dict(kwargs) + + if partial_match: + filters["partial_match"] = True + schema = self.schema.get(kind=kind, branch=branch) branch = branch or self.default_branch if at: at = Timestamp(at) - data = {"count": None, "@filters": filters} - if partial_match: - data["@filters"]["partial_match"] = True + data: dict[str, Any] = { + "count": None, + "@filters": filters, + } response = self.execute_graphql( query=Query(query={schema.kind: data}).render(), @@ -1712,6 +1719,7 @@ def count( ) return int(response.get(schema.kind, {}).get("count", 0)) + @overload def all( self, From d3f8ae9e6be60070b44324ef4664f406675bfbfb Mon Sep 17 00:00:00 2001 From: Bearchitek Date: Mon, 19 May 2025 09:44:35 +0200 Subject: [PATCH 4/4] linter --- infrahub_sdk/client.py | 1 - 1 file changed, 1 deletion(-) diff --git a/infrahub_sdk/client.py b/infrahub_sdk/client.py index 5cfe6c8..bfea914 100644 --- a/infrahub_sdk/client.py +++ b/infrahub_sdk/client.py @@ -1719,7 +1719,6 @@ def count( ) return int(response.get(schema.kind, {}).get("count", 0)) - @overload def all( self,