Skip to content

Commit 494298f

Browse files
committed
Make more raise_for_error optional
1 parent d7befe2 commit 494298f

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

infrahub_sdk/client.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ def wrapper(client: InfrahubClientSync, *args: Any, **kwargs: Any) -> httpx.Resp
106106
return wrapper
107107

108108

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+
109118
class BaseClient:
110119
"""Base class for InfrahubClient and InfrahubClientSync"""
111120

@@ -896,22 +905,15 @@ async def execute_graphql(
896905
timeout (int, optional): Timeout in second for the query. Defaults to None.
897906
raise_for_error (bool | None, optional): Deprecated. Controls only HTTP status handling.
898907
- None (default) or True: HTTP errors raise via resp.raise_for_status().
899-
- False: HTTP errors are not automatically raised.
900-
GraphQL errors always raise GraphQLError.
901-
Defaults to None.
908+
- False: HTTP errors are not automatically raised. Defaults to None.
902909
903910
Raises:
904911
GraphQLError: When the GraphQL response contains errors.
905912
906913
Returns:
907914
dict: The GraphQL data payload (response["data"]).
908915
"""
909-
if raise_for_error is not None:
910-
warnings.warn(
911-
"Using `raise_for_error` is deprecated, use `try/except` to handle errors.",
912-
DeprecationWarning,
913-
stacklevel=1,
914-
)
916+
raise_for_error_deprecation_warning(value=raise_for_error)
915917

916918
branch_name = branch_name or self.default_branch
917919
url = self._graphql_url(branch_name=branch_name, at=at)
@@ -1111,8 +1113,10 @@ async def query_gql_query(
11111113
at: str | None = None,
11121114
timeout: int | None = None,
11131115
tracker: str | None = None,
1114-
raise_for_error: bool = True,
1116+
raise_for_error: bool | None = None,
11151117
) -> dict:
1118+
raise_for_error_deprecation_warning(value=raise_for_error)
1119+
11161120
url = f"{self.address}/api/query/{name}"
11171121
url_params = copy.deepcopy(params or {})
11181122
headers = copy.copy(self.headers or {})
@@ -1157,7 +1161,7 @@ async def query_gql_query(
11571161
timeout=timeout or self.default_timeout,
11581162
)
11591163

1160-
if raise_for_error:
1164+
if raise_for_error in (None, True):
11611165
resp.raise_for_status()
11621166

11631167
return decode_json(response=resp)
@@ -1167,7 +1171,7 @@ async def get_diff_summary(
11671171
branch: str,
11681172
timeout: int | None = None,
11691173
tracker: str | None = None,
1170-
raise_for_error: bool = True,
1174+
raise_for_error: bool | None = None,
11711175
) -> list[NodeDiff]:
11721176
query = get_diff_summary_query()
11731177
response = await self.execute_graphql(
@@ -1232,7 +1236,7 @@ async def allocate_next_ip_address(
12321236
branch: str | None = ...,
12331237
timeout: int | None = ...,
12341238
tracker: str | None = ...,
1235-
raise_for_error: bool = ...,
1239+
raise_for_error: bool | None = ...,
12361240
) -> SchemaType: ...
12371241

12381242
@overload
@@ -1277,7 +1281,7 @@ async def allocate_next_ip_address(
12771281
branch: str | None = ...,
12781282
timeout: int | None = ...,
12791283
tracker: str | None = ...,
1280-
raise_for_error: bool = ...,
1284+
raise_for_error: bool | None = ...,
12811285
) -> CoreNode | None: ...
12821286

12831287
async def allocate_next_ip_address(
@@ -1291,7 +1295,7 @@ async def allocate_next_ip_address(
12911295
branch: str | None = None,
12921296
timeout: int | None = None,
12931297
tracker: str | None = None,
1294-
raise_for_error: bool = True,
1298+
raise_for_error: bool | None = None,
12951299
) -> CoreNode | SchemaType | None:
12961300
"""Allocate a new IP address by using the provided resource pool.
12971301
@@ -1304,7 +1308,7 @@ async def allocate_next_ip_address(
13041308
branch (str, optional): Name of the branch to allocate from. Defaults to default_branch.
13051309
timeout (int, optional): Flag to indicate whether to populate the store with the retrieved nodes.
13061310
tracker (str, optional): The offset for pagination.
1307-
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.
13081312
Returns:
13091313
InfrahubNode: Node corresponding to the allocated resource.
13101314
"""
@@ -1379,7 +1383,7 @@ async def allocate_next_ip_prefix(
13791383
branch: str | None = ...,
13801384
timeout: int | None = ...,
13811385
tracker: str | None = ...,
1382-
raise_for_error: bool = ...,
1386+
raise_for_error: bool | None = ...,
13831387
) -> SchemaType: ...
13841388

13851389
@overload
@@ -1427,7 +1431,7 @@ async def allocate_next_ip_prefix(
14271431
branch: str | None = ...,
14281432
timeout: int | None = ...,
14291433
tracker: str | None = ...,
1430-
raise_for_error: bool = ...,
1434+
raise_for_error: bool | None = ...,
14311435
) -> CoreNode | None: ...
14321436

14331437
async def allocate_next_ip_prefix(
@@ -1442,7 +1446,7 @@ async def allocate_next_ip_prefix(
14421446
branch: str | None = None,
14431447
timeout: int | None = None,
14441448
tracker: str | None = None,
1445-
raise_for_error: bool = True,
1449+
raise_for_error: bool | None = None,
14461450
) -> CoreNode | SchemaType | None:
14471451
"""Allocate a new IP prefix by using the provided resource pool.
14481452
@@ -1456,7 +1460,7 @@ async def allocate_next_ip_prefix(
14561460
branch: Name of the branch to allocate from. Defaults to default_branch.
14571461
timeout: Flag to indicate whether to populate the store with the retrieved nodes.
14581462
tracker: The offset for pagination.
1459-
raise_for_error: The limit for pagination.
1463+
raise_for_error (bool, optional): Deprecated, raise an error if the HTTP status is not 2XX.
14601464
Returns:
14611465
InfrahubNode: Node corresponding to the allocated resource.
14621466
"""
@@ -1652,21 +1656,15 @@ def execute_graphql(
16521656
raise_for_error (bool | None, optional): Deprecated. Controls only HTTP status handling.
16531657
- None (default) or True: HTTP errors raise via `resp.raise_for_status()`.
16541658
- False: HTTP errors are not automatically raised.
1655-
GraphQL errors always raise `GraphQLError`.
1656-
Defaults to None.
1659+
GraphQL errors always raise `GraphQLError`. Defaults to None.
16571660
16581661
Raises:
16591662
GraphQLError: When the GraphQL response contains errors.
16601663
16611664
Returns:
16621665
dict: The GraphQL data payload (`response["data"]`).
16631666
"""
1664-
if raise_for_error is not None:
1665-
warnings.warn(
1666-
"Using `raise_for_error` is deprecated, use `try/except` to handle errors.",
1667-
DeprecationWarning,
1668-
stacklevel=1,
1669-
)
1667+
raise_for_error_deprecation_warning(value=raise_for_error)
16701668

16711669
branch_name = branch_name or self.default_branch
16721670
url = self._graphql_url(branch_name=branch_name, at=at)
@@ -2261,8 +2259,10 @@ def query_gql_query(
22612259
at: str | None = None,
22622260
timeout: int | None = None,
22632261
tracker: str | None = None,
2264-
raise_for_error: bool = True,
2262+
raise_for_error: bool | None = None,
22652263
) -> dict:
2264+
raise_for_error_deprecation_warning(value=raise_for_error)
2265+
22662266
url = f"{self.address}/api/query/{name}"
22672267
url_params = copy.deepcopy(params or {})
22682268
headers = copy.copy(self.headers or {})
@@ -2306,7 +2306,7 @@ def query_gql_query(
23062306
timeout=timeout or self.default_timeout,
23072307
)
23082308

2309-
if raise_for_error:
2309+
if raise_for_error in (None, True):
23102310
resp.raise_for_status()
23112311

23122312
return decode_json(response=resp)
@@ -2316,7 +2316,7 @@ def get_diff_summary(
23162316
branch: str,
23172317
timeout: int | None = None,
23182318
tracker: str | None = None,
2319-
raise_for_error: bool = True,
2319+
raise_for_error: bool | None = None,
23202320
) -> list[NodeDiff]:
23212321
query = get_diff_summary_query()
23222322
response = self.execute_graphql(
@@ -2381,7 +2381,7 @@ def allocate_next_ip_address(
23812381
branch: str | None = ...,
23822382
timeout: int | None = ...,
23832383
tracker: str | None = ...,
2384-
raise_for_error: bool = ...,
2384+
raise_for_error: bool | None = ...,
23852385
) -> SchemaTypeSync: ...
23862386

23872387
@overload
@@ -2426,7 +2426,7 @@ def allocate_next_ip_address(
24262426
branch: str | None = ...,
24272427
timeout: int | None = ...,
24282428
tracker: str | None = ...,
2429-
raise_for_error: bool = ...,
2429+
raise_for_error: bool | None = ...,
24302430
) -> CoreNodeSync | None: ...
24312431

24322432
def allocate_next_ip_address(
@@ -2440,7 +2440,7 @@ def allocate_next_ip_address(
24402440
branch: str | None = None,
24412441
timeout: int | None = None,
24422442
tracker: str | None = None,
2443-
raise_for_error: bool = True,
2443+
raise_for_error: bool | None = None,
24442444
) -> CoreNodeSync | SchemaTypeSync | None:
24452445
"""Allocate a new IP address by using the provided resource pool.
24462446
@@ -2524,7 +2524,7 @@ def allocate_next_ip_prefix(
25242524
branch: str | None = ...,
25252525
timeout: int | None = ...,
25262526
tracker: str | None = ...,
2527-
raise_for_error: bool = ...,
2527+
raise_for_error: bool | None = ...,
25282528
) -> SchemaTypeSync: ...
25292529

25302530
@overload
@@ -2572,7 +2572,7 @@ def allocate_next_ip_prefix(
25722572
branch: str | None = ...,
25732573
timeout: int | None = ...,
25742574
tracker: str | None = ...,
2575-
raise_for_error: bool = ...,
2575+
raise_for_error: bool | None = ...,
25762576
) -> CoreNodeSync | None: ...
25772577

25782578
def allocate_next_ip_prefix(
@@ -2587,7 +2587,7 @@ def allocate_next_ip_prefix(
25872587
branch: str | None = None,
25882588
timeout: int | None = None,
25892589
tracker: str | None = None,
2590-
raise_for_error: bool = True,
2590+
raise_for_error: bool | None = None,
25912591
) -> CoreNodeSync | SchemaTypeSync | None:
25922592
"""Allocate a new IP prefix by using the provided resource pool.
25932593

0 commit comments

Comments
 (0)