Refactor APIRequest class to use httpx.AsyncClient

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-03-05 21:27:05 -03:00
commit 2b8164f28d

View file

@ -1,8 +1,7 @@
import asyncio import asyncio
from typing import List, Optional, Union from typing import List, Optional
import httpx
import requests import httpx
from langflow import CustomComponent from langflow import CustomComponent
from langflow.schema import Record from langflow.schema import Record
@ -42,7 +41,7 @@ class APIRequest(CustomComponent):
async def make_request( async def make_request(
self, self,
session: requests.Session, client: httpx.AsyncClient,
method: str, method: str,
url: str, url: str,
headers: Optional[dict] = None, headers: Optional[dict] = None,
@ -55,23 +54,22 @@ class APIRequest(CustomComponent):
data = record.text if record else None data = record.text if record else None
try: try:
async with httpx.AsyncClient() as client: response = await client.request(
response = await client.request( method, url, headers=headers, content=data, timeout=timeout
method, url, headers=headers, content=data, timeout=timeout )
) try:
try: response_json = response.json()
response_json = response.json() result = orjson_dumps(response_json, indent_2=False)
result = orjson_dumps(response_json, indent_2=False) except Exception:
except Exception: result = response.text
result = response.text return Record(
return Record( text=result,
text=result, data={
data={ "source": url,
"source": url, "headers": headers,
"headers": headers, "status_code": response.status_code,
"status_code": response.status_code, },
}, )
)
except httpx.TimeoutException: except httpx.TimeoutException:
return Record( return Record(
text="Request Timed Out", text="Request Timed Out",
@ -88,7 +86,7 @@ class APIRequest(CustomComponent):
method: str, method: str,
url: List[str], url: List[str],
headers: Optional[dict] = None, headers: Optional[dict] = None,
record: Optional[Union[Record, List[Record]]] = None, record: Optional[Record] = None,
timeout: int = 5, timeout: int = 5,
) -> List[Record]: ) -> List[Record]:
if headers is None: if headers is None:
@ -99,11 +97,11 @@ class APIRequest(CustomComponent):
if isinstance(record, list) if isinstance(record, list)
else [record] if record else [None] * len(urls) else [record] if record else [None] * len(urls)
) )
async with httpx.AsyncClient() as client:
results = await asyncio.gather( results = await asyncio.gather(
*[ *[
self.make_request(method, u, headers, doc, timeout) self.make_request(client, method, u, headers, rec, timeout)
for u, doc in zip(urls, records) for u, rec in zip(urls, records)
] ]
) )
return results return results