malepati/custom_template_working
0
1# import httpx2# import asyncio3# from typing import List, Optional4 5# async def fetch_convo(client: httpx.AsyncClient, user_id: str, convo_id: str):6# url = f"https://mr-mvp-api-dev.dev.ingenspark.com/Db_store_router/conversations/{convo_id}"7# try:8# # Pass user_id as a query parameter as shown in the request9# r = await client.get(url, params={"user_id": user_id}, timeout=30)10# r.raise_for_status()11 12# # Parse the response safely13# json_data = r.json()14# data = json_data.get("conversation") or (json_data.get("conversations") or [None])[0]15 16# if not data: 17# return None18 19# resp = data.get("response", {})20# return {21# "query": data.get("user_query", {}).get("text", ""), 22# "response": resp.get("text", "")23# }24# except Exception as e:25# print(f"Error fetching {convo_id}: {e}")26# return None27 28# async def fetch_all(user_id: str, convo_ids: List[str]):29# async with httpx.AsyncClient() as client:30# tasks = [fetch_convo(client, user_id, cid) for cid in convo_ids]31# results = await asyncio.gather(*tasks)32# return [r for r in results if r]33 34 35import httpx36import asyncio37import logging38from typing import List39 40logger = logging.getLogger(__name__)41 42async def fetch_all(user_id: str, convo_ids: List[str]):43 url = "https://mr-mvp-api-dev.dev.ingenspark.com/Db_store_router/conversations/{}"44 async with httpx.AsyncClient(timeout=30.0) as client:45 tasks = [client.get(url.format(cid), params={"user_id": user_id}) for cid in convo_ids]46 results = await asyncio.gather(*tasks, return_exceptions=True)47 data = []48 for idx, r in enumerate(results):49 if isinstance(r, Exception):50 continue51 try:52 r.raise_for_status()53 json_data = r.json()54 conv = json_data.get("conversation") or (json_data.get("conversations") or [None])[0]55 if conv:56 query = conv.get("user_query", {}).get("text", "")57 response = conv.get("response", {}).get("text", "")58 if query or response:59 data.append({"query": query, "response": response})60 except Exception as e:61 logger.error(f"Error: {e}")62 return data63 