Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions backend/python/app/sources/client/http/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(
token: str,
token_type: str = "Bearer",
timeout: float = 30.0,
follow_redirects: bool = True
follow_redirects: bool = True,
) -> None:
self.headers = {
"Authorization": f"{token_type} {token}",
Expand All @@ -30,8 +30,7 @@ async def _ensure_client(self) -> httpx.AsyncClient:
"""Ensure client is created and available"""
if self.client is None:
self.client = httpx.AsyncClient(
timeout=self.timeout,
follow_redirects=self.follow_redirects
timeout=self.timeout, follow_redirects=self.follow_redirects
)
return self.client

Expand All @@ -43,28 +42,31 @@ async def execute(self, request: HTTPRequest, **kwargs) -> HTTPResponse:
Returns:
A HTTPResponse object containing the response from the server
"""
url = f"{request.url.format(**request.path_params)}"
url = request.url.format(**request.path_params)

client = await self._ensure_client()

# Merge client headers with request headers (request headers take precedence)
merged_headers = {**self.headers, **request.headers}
# Optimize header merging for most common case
if request.headers:
merged_headers = {**self.headers, **request.headers}
else:
merged_headers = self.headers

request_kwargs = {
"params": request.query_params,
"headers": merged_headers,
**kwargs
**kwargs,
}

if isinstance(request.body, dict):
# Check if Content-Type indicates form data
body = request.body
if isinstance(body, dict):
content_type = request.headers.get("Content-Type", "").lower()
if "application/x-www-form-urlencoded" in content_type:
# Send as form data
request_kwargs["data"] = request.body
request_kwargs["data"] = body
else:
# Send as JSON (default behavior)
request_kwargs["json"] = request.body
elif isinstance(request.body, bytes):
request_kwargs["content"] = request.body
request_kwargs["json"] = body
elif isinstance(body, bytes):
request_kwargs["content"] = body

response = await client.request(request.method, url, **request_kwargs)
return HTTPResponse(response)
Expand Down
1 change: 1 addition & 0 deletions backend/python/app/sources/client/http/http_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class HTTPResponse:
Args:
response: The httpx response object
"""

def __init__(self, response: httpx.Response) -> None:
self.response = response

Expand Down
5 changes: 3 additions & 2 deletions backend/python/app/sources/external/bookstack/bookstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2407,9 +2407,10 @@ async def destroy_recycle_bin_item(
"""
params: Dict[str, Union[str, int]] = {}

url = self.base_url + "/api/recycle-bin/{deletion_id}".format(deletion_id=deletion_id)
url = f"{self.base_url}/api/recycle-bin/{deletion_id}"

headers = dict(self.http.headers)
# No mutation of headers occurs, so direct reference is safe and avoids copying
headers = self.http.headers

request = HTTPRequest(
method="DELETE",
Expand Down