Skip to content

Commit 8b8e62a

Browse files
committed
♻️ Change chatbot class signature and make_api_request method from APICall class
Define dynamic_request_body argument on the make_api_request to handle chatbot component instead of static request_body from config. Also, adapt chatbot class signature with these changes.
1 parent 1e80a97 commit 8b8e62a

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

src/vuegen/report.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,25 +561,40 @@ def __init__(
561561
self.method = method.upper()
562562
self.headers = headers or {}
563563
self.params = params or {}
564+
# NOTE: request_body is usually dynamically set before the call for POST/PUT
565+
# but we'll include it here if needed for values from a config file
564566
self.request_body = request_body or {}
565567

566-
def make_api_request(self) -> Optional[dict]:
568+
def make_api_request(self, dynamic_request_body: Optional[dict] = None) -> Optional[dict]:
567569
"""
568570
Sends an HTTP request to the specified API and returns the JSON response.
571+
It allows overriding the request body dynamically.
572+
573+
Parameters
574+
----------
575+
dynamic_request_body : Optional[dict]
576+
A dictionary to use as the JSON request body for this specific call.
577+
Overrides the instance's request_body if provided.
569578
570579
Returns
571580
-------
572581
response : Optional[dict]
573582
The JSON response from the API, or None if the request fails.
574583
"""
584+
request_body_to_send = dynamic_request_body if dynamic_request_body is not None else self.request_body
575585
try:
576586
self.logger.info(f"Making {self.method} request to API: {self.api_url}")
587+
self.logger.debug(f"Headers: {self.headers}")
588+
self.logger.debug(f"Params: {self.params}")
589+
self.logger.debug(f"Request Body: {request_body_to_send}")
590+
577591
response = requests.request(
578592
self.method,
579593
self.api_url,
580594
headers=self.headers,
581595
params=self.params,
582-
json=self.request_body,
596+
# Validate the request body based on the method
597+
json=request_body_to_send if self.method in ["POST", "PUT", "PATCH"] and request_body_to_send else None
583598
)
584599
response.raise_for_status()
585600
self.logger.info(
@@ -588,6 +603,17 @@ def make_api_request(self) -> Optional[dict]:
588603
return response.json()
589604
except requests.exceptions.RequestException as e:
590605
self.logger.error(f"API request failed: {e}")
606+
# Attempt to get error details from response body if possible
607+
try:
608+
error_details = e.response.json() if e.response else str(e)
609+
self.logger.error(f"Error details: {error_details}")
610+
except json.JSONDecodeError:
611+
error_details = e.response.text if e.response else str(e)
612+
self.logger.error(f"Error details (non-JSON): {error_details}")
613+
return None
614+
except json.JSONDecodeError as e:
615+
self.logger.error(f"Failed to decode JSON response: {e}")
616+
self.logger.error(f"Response text: {response.text}")
591617
return None
592618

593619

@@ -629,9 +655,12 @@ def __init__(
629655
title=title,
630656
logger=logger,
631657
api_url=api_url,
632-
caption=caption,
658+
method = "POST",
659+
caption=None,
633660
headers=headers,
634661
params=params,
662+
# Default request_body is empty, it will be set dynamically
663+
request_body=None
635664
)
636665

637666

0 commit comments

Comments
 (0)