Pythonify responses from JetBrains plugin server (camelCase -> snake_case)

This commit is contained in:
Dominik Jain
2025-07-06 20:15:53 +02:00
committed by Dominik Jain
parent f49802a040
commit e306be796f
+24 -3
View File
@@ -3,10 +3,12 @@ Client for the Serena JetBrains Plugin
"""
import json
from typing import Any, Optional, Self
from typing import Any, Optional, Self, TypeVar
import requests
T = TypeVar("T")
class SerenaClientError(Exception):
"""Base exception for Serena client errors."""
@@ -49,7 +51,7 @@ class JetBrainsPluginClient:
# Try to parse JSON response
try:
return response.json()
return self._pythonify_response(response.json())
except json.JSONDecodeError:
# If response is not JSON, return raw text
return {"response": response.text}
@@ -63,6 +65,25 @@ class JetBrainsPluginClient:
except requests.exceptions.RequestException as e:
raise SerenaClientError(f"Request failed: {e}")
@staticmethod
def _pythonify_response(response: T) -> T:
"""
Converts dictionary keys from camelCase to snake_case recursively.
:response: the response in which to convert keys (dictionary or list)
"""
to_snake_case = lambda s: "".join(["_" + c.lower() if c.isupper() else c for c in s])
def convert(x):
if isinstance(x, dict):
return {to_snake_case(k): convert(v) for k, v in x.items()}
elif isinstance(x, list):
return [convert(item) for item in x]
else:
return x
return convert(response)
def heartbeat(self) -> dict[str, Any]:
return self._make_request("GET", "/heartbeat")
@@ -127,5 +148,5 @@ if __name__ == "__main__":
# find references
if symbols:
first_symbol = symbols[0]
refs_response = client.find_references(name_path=first_symbol["namePath"], relative_path=first_symbol["relativePath"])
refs_response = client.find_references(name_path=first_symbol["name_path"], relative_path=first_symbol["relative_path"])
pprint(refs_response)