Skip to content

Commit 3435bab

Browse files
authored
show deprecation warnings (#66)
1 parent fb05c3c commit 3435bab

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

fishjam/api/_client.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
import warnings
13
from typing import cast
24

35
from fishjam._openapi_client.client import AuthenticatedClient
@@ -16,12 +18,39 @@ def __init__(self, fishjam_id: str, management_token: str):
1618
token=management_token,
1719
headers={"x-fishjam-api-client": f"python-server/{get_version()}"},
1820
)
21+
self.warnings_shown = False
1922

2023
def _request(self, method, **kwargs):
2124
response = method.sync_detailed(client=self.client, **kwargs)
25+
self._handle_deprecation_header(response.headers)
2226

2327
if isinstance(response.parsed, Error):
2428
response = cast(Response[Error], response)
2529
raise HTTPError.from_response(response)
2630

2731
return response.parsed
32+
33+
def _handle_deprecation_header(self, headers):
34+
deprecation_warning = headers.get("x-fishjam-api-deprecated")
35+
if deprecation_warning and not self.warnings_shown:
36+
self.warnings_shown = True
37+
try:
38+
deprecation_data = json.loads(deprecation_warning)
39+
except (json.JSONDecodeError, TypeError, ValueError):
40+
return
41+
42+
status = deprecation_data["status"]
43+
msg = deprecation_data["message"]
44+
45+
if not status or not msg:
46+
return
47+
48+
match status:
49+
case "unsupported":
50+
warnings.warn(message=msg, category=UserWarning, stacklevel=4)
51+
case "deprecated":
52+
warnings.warn(
53+
message=msg, category=DeprecationWarning, stacklevel=4
54+
)
55+
case _:
56+
pass

0 commit comments

Comments
 (0)