Skip to content
1 change: 1 addition & 0 deletions changelog/325.removed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Removed `client.branch.diff_data()` from both the async and sync clients. The method relied on a `GET /api/diff/data` REST endpoint that does not exist in Infrahub, so every call returned a 404. Use `client.get_diff_tree()` to retrieve the full diff of a branch against its base branch, or `client.get_diff_summary()` for the list of changed nodes; both use the `DiffTree` GraphQL query.
33 changes: 29 additions & 4 deletions docs/docs/python-sdk/guides/branches.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,40 @@ The Python SDK provides multiple methods to manage the branches in an Infrahub i
</TabItem>
</Tabs>

## Generating a diff for a branch
## Getting the diff for a branch

Use `get_diff_tree` to retrieve the full diff of a branch compared to its base branch, including summary counts and the list of changed nodes. It returns `None` if no diff exists for the branch.

<Tabs groupId="async-sync">
<TabItem value="Async" default>

```python
from infrahub_sdk import InfrahubClient
client = InfrahubClient()
diff = await client.get_diff_tree(branch="new-branch")
```

</TabItem>
<TabItem value="Sync" default>

```python
from infrahub_sdk import InfrahubClientSync
client = InfrahubClientSync()
diff = client.get_diff_tree(branch="new-branch")
```

</TabItem>
</Tabs>

If you only need the list of changed nodes, `get_diff_summary` returns them without the diff metadata.

<Tabs groupId="async-sync">
<TabItem value="Async" default>

```python
from infrahub_sdk import InfrahubClient
client = await InfrahubClient()
diff = await client.branch.diff_data(branch_name="new-branch")
client = InfrahubClient()
node_diffs = await client.get_diff_summary(branch="new-branch")
```

</TabItem>
Expand All @@ -125,7 +150,7 @@ The Python SDK provides multiple methods to manage the branches in an Infrahub i
```python
from infrahub_sdk import InfrahubClientSync
client = InfrahubClientSync()
diff = client.branch.diff_data(branch_name="new-branch")
node_diffs = client.get_diff_summary(branch="new-branch")
```

</TabItem>
Expand Down
65 changes: 3 additions & 62 deletions infrahub_sdk/branch.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from __future__ import annotations

from enum import Enum
from typing import TYPE_CHECKING, Any, Literal, overload
from urllib.parse import urlencode
from typing import TYPE_CHECKING, Literal, overload

from pydantic import BaseModel

from .exceptions import BranchNotFoundError
from .graphql import Mutation, Query
from .utils import decode_json

if TYPE_CHECKING:
from .client import InfrahubClient, InfrahubClientSync
Expand Down Expand Up @@ -61,30 +59,7 @@ class BranchData(BaseModel):
QUERY_ONE_BRANCH_DATA = {"Branch": {**BRANCH_DATA, **BRANCH_DATA_FILTER}}


class InfraHubBranchManagerBase:
@classmethod
def generate_diff_data_url(
cls,
client: InfrahubClient | InfrahubClientSync,
branch_name: str,
branch_only: bool = True,
time_from: str | None = None,
time_to: str | None = None,
) -> str:
"""Generate the URL for the diff_data function."""
url = f"{client.address}/api/diff/data"
url_params = {}
url_params["branch"] = branch_name
url_params["branch_only"] = str(branch_only).lower()
if time_from:
url_params["time_from"] = time_from
if time_to:
url_params["time_to"] = time_to

return url + urlencode(url_params)


class InfrahubBranchManager(InfraHubBranchManagerBase):
class InfrahubBranchManager:
def __init__(self, client: InfrahubClient) -> None:
self.client = client

Expand Down Expand Up @@ -206,25 +181,8 @@ async def get(self, branch_name: str) -> BranchData:
raise BranchNotFoundError(identifier=branch_name)
return BranchData(**data["Branch"][0])

async def diff_data(
self,
branch_name: str,
branch_only: bool = True,
time_from: str | None = None,
time_to: str | None = None,
) -> dict[Any, Any]:
url = self.generate_diff_data_url(
client=self.client,
branch_name=branch_name,
branch_only=branch_only,
time_from=time_from,
time_to=time_to,
)
response = await self.client._get(url=url, headers=self.client.headers)
return decode_json(response=response)


class InfrahubBranchManagerSync(InfraHubBranchManagerBase):
class InfrahubBranchManagerSync:
def __init__(self, client: InfrahubClientSync) -> None:
self.client = client

Expand Down Expand Up @@ -300,23 +258,6 @@ def delete(self, branch_name: str) -> bool:
response = self.client.execute_graphql(query=query.render(), tracker="mutation-branch-delete")
return response["BranchDelete"]["ok"]

def diff_data(
self,
branch_name: str,
branch_only: bool = True,
time_from: str | None = None,
time_to: str | None = None,
) -> dict[Any, Any]:
url = self.generate_diff_data_url(
client=self.client,
branch_name=branch_name,
branch_only=branch_only,
time_from=time_from,
time_to=time_to,
)
response = self.client._get(url=url, headers=self.client.headers)
return decode_json(response=response)

def merge(self, branch_name: str) -> bool:
input_data = {
"data": {
Expand Down