Spaces:
Running
Running
from fastapi import APIRouter, HTTPException , Body,Query | |
from models.location_models import BodyData,ErrorResponse | |
from services.location_service import LocationService | |
from concurrent.futures import ThreadPoolExecutor, as_completed | |
import core.init_supabase as sp | |
router = APIRouter() | |
async def get_coordinates(user_id: str = Query(..., description="User's hush ID")): | |
# token = data.jwt_token | |
# user_id = sp.authenticate_user(token) | |
# if user_id == "Exceptional error": | |
# return {"User not Authenticated!"} | |
# else: | |
print(user_id) | |
supabase_user_data = sp.fetch_data(user_id) | |
print("supabase data") | |
print(supabase_user_data) | |
print(len(supabase_user_data)) | |
coords=[]*len(supabase_user_data) | |
with ThreadPoolExecutor(max_workers=15) as executor: | |
future_to_cord = {executor.submit(get_coordinates, cord): cord for cord in supabase_user_data} | |
for future in as_completed(future_to_cord): | |
cord = future_to_cord[future] | |
try: | |
result = future.result() | |
coords.append(result) | |
except Exception as exc: | |
print(f'Coordinate {cord} generated an exception: {exc}') | |
if isinstance(result, ErrorResponse): | |
print(HTTPException(status_code=400, detail=result.error)) | |
return {"message":"An unexpected error occured please try again !!"} | |
print(coords) | |
return {"data":coords} | |
async def get_coordinates(data: BodyData = Body(...)): | |
token = data.jwt_token | |
user_id = sp.authenticate_user(token) | |
if user_id == "Exceptional error": | |
return {"User not Authenticated!"} | |
else: | |
print(user_id) | |
try: | |
supabase_card_data = sp.fetch_cards_data(user_id) | |
print("supabase data") | |
print(supabase_card_data) | |
except Exception as e: | |
print(HTTPException(status_code=400, detail=e)) | |
return {"message":"An unexpected error occured please try again !!"} | |
print("Returning the data") | |
return {"data":supabase_card_data} | |