Skip to content

Commit 33e0e93

Browse files
committed
♻️ Refactor: modify apicall class signatute to add method and request_body as attributes and modify the scripts accordingly
1 parent 4939f02 commit 33e0e93

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

src/vuegen/config_manager.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,11 @@ def _create_apicall_component(self, component_data: dict) -> r.APICall:
565565
title=component_data["title"],
566566
logger=self.logger,
567567
api_url=component_data["api_url"],
568+
method=component_data["method"],
568569
caption=component_data.get("caption"),
569570
headers=component_data.get("headers"),
570571
params=component_data.get("params"),
572+
request_body=component_data.get("request_body"),
571573
)
572574

573575
def _create_chatbot_component(self, component_data: dict) -> r.ChatBot:

src/vuegen/report.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -530,20 +530,26 @@ class APICall(Component):
530530
----------
531531
api_url : str
532532
The URL of the API to interact with.
533+
method : str
534+
HTTP method to use for the request ("GET", "POST", or "PUT"). The deafult is "GET".
533535
headers : Optional[dict]
534536
Headers to include in the API request (default is None).
535537
params : Optional[dict]
536538
Query parameters to include in the API request (default is None).
539+
request_body : Optional[dict]
540+
The request body for methods like POST or PUT (default is None).
537541
"""
538542

539543
def __init__(
540544
self,
541545
title: str,
542546
logger: logging.Logger,
543547
api_url: str,
548+
method: str = "GET",
544549
caption: str = None,
545550
headers: Optional[dict] = None,
546551
params: Optional[dict] = None,
552+
request_body: Optional[dict] = None,
547553
):
548554
super().__init__(
549555
title=title,
@@ -552,35 +558,28 @@ def __init__(
552558
caption=caption,
553559
)
554560
self.api_url = api_url
561+
self.method = method.upper()
555562
self.headers = headers or {}
556563
self.params = params or {}
564+
self.request_body = request_body or {}
557565

558-
def make_api_request(
559-
self, method: str, request_body: Optional[dict] = None
560-
) -> Optional[dict]:
566+
def make_api_request(self) -> Optional[dict]:
561567
"""
562568
Sends an HTTP request to the specified API and returns the JSON response.
563569
564-
Parameters
565-
----------
566-
method : str
567-
HTTP method to use for the request.
568-
request_body : Optional[dict], optional
569-
The request body for POST or PUT methods (default is None).
570-
571570
Returns
572571
-------
573572
response : Optional[dict]
574573
The JSON response from the API, or None if the request fails.
575574
"""
576575
try:
577-
self.logger.info(f"Making {method} request to API: {self.api_url}")
576+
self.logger.info(f"Making {self.method} request to API: {self.api_url}")
578577
response = requests.request(
579-
method,
578+
self.method,
580579
self.api_url,
581580
headers=self.headers,
582581
params=self.params,
583-
json=request_body,
582+
json=self.request_body,
584583
)
585584
response.raise_for_status()
586585
self.logger.info(

src/vuegen/streamlit_reportview.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ def _generate_apicall_content(self, apicall) -> List[str]:
834834
)
835835
)
836836
try:
837-
apicall_response = apicall.make_api_request(method="GET")
837+
apicall_response = apicall.make_api_request()
838838
apicall_content.append(f"""st.write({apicall_response})\n""")
839839
except Exception as e:
840840
self.report.logger.error(
@@ -851,7 +851,7 @@ def _generate_apicall_content(self, apicall) -> List[str]:
851851
)
852852

853853
self.report.logger.info(
854-
f"Successfully generated content for APICall: '{apicall.title}'"
854+
f"Successfully generated content for APICall '{apicall.title}' using method '{apicall.method}'"
855855
)
856856
return apicall_content
857857

0 commit comments

Comments
 (0)