Skip to content

Commit ed6673e

Browse files
authored
IHS-154 Deprecate using raise_for_error = False (#508)
1 parent 49335ca commit ed6673e

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

changelog/493.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deprecate the use of `raise_for_error=False` across several methods, using a try/except pattern is preferred.

infrahub_sdk/client.py

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import copy
55
import logging
66
import time
7+
import warnings
78
from collections.abc import Coroutine, MutableMapping
89
from functools import wraps
910
from time import sleep
@@ -105,6 +106,15 @@ def wrapper(client: InfrahubClientSync, *args: Any, **kwargs: Any) -> httpx.Resp
105106
return wrapper
106107

107108

109+
def raise_for_error_deprecation_warning(value: bool | None) -> None:
110+
if value is not None:
111+
warnings.warn(
112+
"Using `raise_for_error` is deprecated, use `try/except` to handle errors.",
113+
DeprecationWarning,
114+
stacklevel=1,
115+
)
116+
117+
108118
class BaseClient:
109119
"""Base class for InfrahubClient and InfrahubClientSync"""
110120

@@ -881,7 +891,7 @@ async def execute_graphql(
881891
branch_name: str | None = None,
882892
at: str | Timestamp | None = None,
883893
timeout: int | None = None,
884-
raise_for_error: bool = True,
894+
raise_for_error: bool | None = None,
885895
tracker: str | None = None,
886896
) -> dict:
887897
"""Execute a GraphQL query (or mutation).
@@ -893,13 +903,17 @@ async def execute_graphql(
893903
branch_name (str, optional): Name of the branch on which the query will be executed. Defaults to None.
894904
at (str, optional): Time when the query should be executed. Defaults to None.
895905
timeout (int, optional): Timeout in second for the query. Defaults to None.
896-
raise_for_error (bool, optional): Flag to indicate that we need to raise an exception if the response has some errors. Defaults to True.
906+
raise_for_error (bool | None, optional): Deprecated. Controls only HTTP status handling.
907+
- None (default) or True: HTTP errors raise via resp.raise_for_status().
908+
- False: HTTP errors are not automatically raised. Defaults to None.
909+
897910
Raises:
898-
GraphQLError: _description_
911+
GraphQLError: When the GraphQL response contains errors.
899912
900913
Returns:
901-
_type_: _description_
914+
dict: The GraphQL data payload (response["data"]).
902915
"""
916+
raise_for_error_deprecation_warning(value=raise_for_error)
903917

904918
branch_name = branch_name or self.default_branch
905919
url = self._graphql_url(branch_name=branch_name, at=at)
@@ -922,7 +936,7 @@ async def execute_graphql(
922936
try:
923937
resp = await self._post(url=url, payload=payload, headers=headers, timeout=timeout)
924938

925-
if raise_for_error:
939+
if raise_for_error in (None, True):
926940
resp.raise_for_status()
927941

928942
retry = False
@@ -1099,8 +1113,10 @@ async def query_gql_query(
10991113
at: str | None = None,
11001114
timeout: int | None = None,
11011115
tracker: str | None = None,
1102-
raise_for_error: bool = True,
1116+
raise_for_error: bool | None = None,
11031117
) -> dict:
1118+
raise_for_error_deprecation_warning(value=raise_for_error)
1119+
11041120
url = f"{self.address}/api/query/{name}"
11051121
url_params = copy.deepcopy(params or {})
11061122
headers = copy.copy(self.headers or {})
@@ -1145,7 +1161,7 @@ async def query_gql_query(
11451161
timeout=timeout or self.default_timeout,
11461162
)
11471163

1148-
if raise_for_error:
1164+
if raise_for_error in (None, True):
11491165
resp.raise_for_status()
11501166

11511167
return decode_json(response=resp)
@@ -1155,7 +1171,7 @@ async def get_diff_summary(
11551171
branch: str,
11561172
timeout: int | None = None,
11571173
tracker: str | None = None,
1158-
raise_for_error: bool = True,
1174+
raise_for_error: bool | None = None,
11591175
) -> list[NodeDiff]:
11601176
query = get_diff_summary_query()
11611177
response = await self.execute_graphql(
@@ -1220,7 +1236,7 @@ async def allocate_next_ip_address(
12201236
branch: str | None = ...,
12211237
timeout: int | None = ...,
12221238
tracker: str | None = ...,
1223-
raise_for_error: bool = ...,
1239+
raise_for_error: bool | None = ...,
12241240
) -> SchemaType: ...
12251241

12261242
@overload
@@ -1265,7 +1281,7 @@ async def allocate_next_ip_address(
12651281
branch: str | None = ...,
12661282
timeout: int | None = ...,
12671283
tracker: str | None = ...,
1268-
raise_for_error: bool = ...,
1284+
raise_for_error: bool | None = ...,
12691285
) -> CoreNode | None: ...
12701286

12711287
async def allocate_next_ip_address(
@@ -1279,7 +1295,7 @@ async def allocate_next_ip_address(
12791295
branch: str | None = None,
12801296
timeout: int | None = None,
12811297
tracker: str | None = None,
1282-
raise_for_error: bool = True,
1298+
raise_for_error: bool | None = None,
12831299
) -> CoreNode | SchemaType | None:
12841300
"""Allocate a new IP address by using the provided resource pool.
12851301
@@ -1292,7 +1308,7 @@ async def allocate_next_ip_address(
12921308
branch (str, optional): Name of the branch to allocate from. Defaults to default_branch.
12931309
timeout (int, optional): Flag to indicate whether to populate the store with the retrieved nodes.
12941310
tracker (str, optional): The offset for pagination.
1295-
raise_for_error (bool, optional): The limit for pagination.
1311+
raise_for_error (bool, optional): Deprecated, raise an error if the HTTP status is not 2XX.
12961312
Returns:
12971313
InfrahubNode: Node corresponding to the allocated resource.
12981314
"""
@@ -1367,7 +1383,7 @@ async def allocate_next_ip_prefix(
13671383
branch: str | None = ...,
13681384
timeout: int | None = ...,
13691385
tracker: str | None = ...,
1370-
raise_for_error: bool = ...,
1386+
raise_for_error: bool | None = ...,
13711387
) -> SchemaType: ...
13721388

13731389
@overload
@@ -1415,7 +1431,7 @@ async def allocate_next_ip_prefix(
14151431
branch: str | None = ...,
14161432
timeout: int | None = ...,
14171433
tracker: str | None = ...,
1418-
raise_for_error: bool = ...,
1434+
raise_for_error: bool | None = ...,
14191435
) -> CoreNode | None: ...
14201436

14211437
async def allocate_next_ip_prefix(
@@ -1430,7 +1446,7 @@ async def allocate_next_ip_prefix(
14301446
branch: str | None = None,
14311447
timeout: int | None = None,
14321448
tracker: str | None = None,
1433-
raise_for_error: bool = True,
1449+
raise_for_error: bool | None = None,
14341450
) -> CoreNode | SchemaType | None:
14351451
"""Allocate a new IP prefix by using the provided resource pool.
14361452
@@ -1444,7 +1460,7 @@ async def allocate_next_ip_prefix(
14441460
branch: Name of the branch to allocate from. Defaults to default_branch.
14451461
timeout: Flag to indicate whether to populate the store with the retrieved nodes.
14461462
tracker: The offset for pagination.
1447-
raise_for_error: The limit for pagination.
1463+
raise_for_error (bool, optional): Deprecated, raise an error if the HTTP status is not 2XX.
14481464
Returns:
14491465
InfrahubNode: Node corresponding to the allocated resource.
14501466
"""
@@ -1625,7 +1641,7 @@ def execute_graphql(
16251641
branch_name: str | None = None,
16261642
at: str | Timestamp | None = None,
16271643
timeout: int | None = None,
1628-
raise_for_error: bool = True,
1644+
raise_for_error: bool | None = None,
16291645
tracker: str | None = None,
16301646
) -> dict:
16311647
"""Execute a GraphQL query (or mutation).
@@ -1637,13 +1653,18 @@ def execute_graphql(
16371653
branch_name (str, optional): Name of the branch on which the query will be executed. Defaults to None.
16381654
at (str, optional): Time when the query should be executed. Defaults to None.
16391655
timeout (int, optional): Timeout in second for the query. Defaults to None.
1640-
raise_for_error (bool, optional): Flag to indicate that we need to raise an exception if the response has some errors. Defaults to True.
1656+
raise_for_error (bool | None, optional): Deprecated. Controls only HTTP status handling.
1657+
- None (default) or True: HTTP errors raise via `resp.raise_for_status()`.
1658+
- False: HTTP errors are not automatically raised.
1659+
GraphQL errors always raise `GraphQLError`. Defaults to None.
1660+
16411661
Raises:
1642-
GraphQLError: When an error occurs during the execution of the GraphQL query or mutation.
1662+
GraphQLError: When the GraphQL response contains errors.
16431663
16441664
Returns:
1645-
dict: The result of the GraphQL query or mutation.
1665+
dict: The GraphQL data payload (`response["data"]`).
16461666
"""
1667+
raise_for_error_deprecation_warning(value=raise_for_error)
16471668

16481669
branch_name = branch_name or self.default_branch
16491670
url = self._graphql_url(branch_name=branch_name, at=at)
@@ -1666,7 +1687,7 @@ def execute_graphql(
16661687
try:
16671688
resp = self._post(url=url, payload=payload, headers=headers, timeout=timeout)
16681689

1669-
if raise_for_error:
1690+
if raise_for_error in (None, True):
16701691
resp.raise_for_status()
16711692

16721693
retry = False
@@ -2238,8 +2259,10 @@ def query_gql_query(
22382259
at: str | None = None,
22392260
timeout: int | None = None,
22402261
tracker: str | None = None,
2241-
raise_for_error: bool = True,
2262+
raise_for_error: bool | None = None,
22422263
) -> dict:
2264+
raise_for_error_deprecation_warning(value=raise_for_error)
2265+
22432266
url = f"{self.address}/api/query/{name}"
22442267
url_params = copy.deepcopy(params or {})
22452268
headers = copy.copy(self.headers or {})
@@ -2283,7 +2306,7 @@ def query_gql_query(
22832306
timeout=timeout or self.default_timeout,
22842307
)
22852308

2286-
if raise_for_error:
2309+
if raise_for_error in (None, True):
22872310
resp.raise_for_status()
22882311

22892312
return decode_json(response=resp)
@@ -2293,7 +2316,7 @@ def get_diff_summary(
22932316
branch: str,
22942317
timeout: int | None = None,
22952318
tracker: str | None = None,
2296-
raise_for_error: bool = True,
2319+
raise_for_error: bool | None = None,
22972320
) -> list[NodeDiff]:
22982321
query = get_diff_summary_query()
22992322
response = self.execute_graphql(
@@ -2358,7 +2381,7 @@ def allocate_next_ip_address(
23582381
branch: str | None = ...,
23592382
timeout: int | None = ...,
23602383
tracker: str | None = ...,
2361-
raise_for_error: bool = ...,
2384+
raise_for_error: bool | None = ...,
23622385
) -> SchemaTypeSync: ...
23632386

23642387
@overload
@@ -2403,7 +2426,7 @@ def allocate_next_ip_address(
24032426
branch: str | None = ...,
24042427
timeout: int | None = ...,
24052428
tracker: str | None = ...,
2406-
raise_for_error: bool = ...,
2429+
raise_for_error: bool | None = ...,
24072430
) -> CoreNodeSync | None: ...
24082431

24092432
def allocate_next_ip_address(
@@ -2417,7 +2440,7 @@ def allocate_next_ip_address(
24172440
branch: str | None = None,
24182441
timeout: int | None = None,
24192442
tracker: str | None = None,
2420-
raise_for_error: bool = True,
2443+
raise_for_error: bool | None = None,
24212444
) -> CoreNodeSync | SchemaTypeSync | None:
24222445
"""Allocate a new IP address by using the provided resource pool.
24232446
@@ -2501,7 +2524,7 @@ def allocate_next_ip_prefix(
25012524
branch: str | None = ...,
25022525
timeout: int | None = ...,
25032526
tracker: str | None = ...,
2504-
raise_for_error: bool = ...,
2527+
raise_for_error: bool | None = ...,
25052528
) -> SchemaTypeSync: ...
25062529

25072530
@overload
@@ -2549,7 +2572,7 @@ def allocate_next_ip_prefix(
25492572
branch: str | None = ...,
25502573
timeout: int | None = ...,
25512574
tracker: str | None = ...,
2552-
raise_for_error: bool = ...,
2575+
raise_for_error: bool | None = ...,
25532576
) -> CoreNodeSync | None: ...
25542577

25552578
def allocate_next_ip_prefix(
@@ -2564,7 +2587,7 @@ def allocate_next_ip_prefix(
25642587
branch: str | None = None,
25652588
timeout: int | None = None,
25662589
tracker: str | None = None,
2567-
raise_for_error: bool = True,
2590+
raise_for_error: bool | None = None,
25682591
) -> CoreNodeSync | SchemaTypeSync | None:
25692592
"""Allocate a new IP prefix by using the provided resource pool.
25702593

0 commit comments

Comments
 (0)