Spaces:
Running
Running
Update api/endpoints/location.py
Browse files- api/endpoints/location.py +26 -22
api/endpoints/location.py
CHANGED
@@ -3,37 +3,41 @@ from models.location_models import BodyData,ErrorResponse
|
|
3 |
from services.location_service import LocationService
|
4 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
5 |
import core.init_supabase as sp
|
6 |
-
|
7 |
router = APIRouter()
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
@router.post("/get_coordinates")
|
10 |
-
async def
|
11 |
-
# token = data.jwt_token
|
12 |
-
# user_id = sp.authenticate_user(token)
|
13 |
-
# if user_id == "Exceptional error":
|
14 |
-
# return {"User not Authenticated!"}
|
15 |
-
# else:
|
16 |
print(user_id)
|
17 |
supabase_user_data = sp.fetch_data(user_id)
|
18 |
print("supabase data")
|
19 |
print(supabase_user_data)
|
20 |
print(len(supabase_user_data))
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
result = future.result()
|
28 |
-
coords.append(result)
|
29 |
-
except Exception as exc:
|
30 |
-
print(f'Coordinate {cord} generated an exception: {exc}')
|
31 |
-
if isinstance(result, ErrorResponse):
|
32 |
-
print(HTTPException(status_code=400, detail=result.error))
|
33 |
-
return {"message":"An unexpected error occured please try again !!"}
|
34 |
print(coords)
|
35 |
-
return {"data":coords}
|
36 |
-
|
37 |
|
38 |
|
39 |
@router.post("/get_card_reccomendation")
|
|
|
3 |
from services.location_service import LocationService
|
4 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
5 |
import core.init_supabase as sp
|
6 |
+
import asyncio
|
7 |
router = APIRouter()
|
8 |
|
9 |
+
async def get_coordinates(cord):
|
10 |
+
try:
|
11 |
+
return await LocationService.get_coordinates(cord)
|
12 |
+
except Exception as exc:
|
13 |
+
print(f'Coordinate {cord} generated an exception: {exc}')
|
14 |
+
return None
|
15 |
+
|
16 |
+
async def process_coordinates(supabase_user_data, max_concurrency=15):
|
17 |
+
semaphore = asyncio.Semaphore(max_concurrency)
|
18 |
+
|
19 |
+
async def bounded_get_coordinates(cord):
|
20 |
+
async with semaphore:
|
21 |
+
return await get_coordinates(cord)
|
22 |
+
|
23 |
+
coords = await asyncio.gather(*[bounded_get_coordinates(cord) for cord in supabase_user_data])
|
24 |
+
return [coord for coord in coords if coord is not None]
|
25 |
+
|
26 |
@router.post("/get_coordinates")
|
27 |
+
async def get_coordinates_route(user_id: str = Query(..., description="User's hush ID")):
|
|
|
|
|
|
|
|
|
|
|
28 |
print(user_id)
|
29 |
supabase_user_data = sp.fetch_data(user_id)
|
30 |
print("supabase data")
|
31 |
print(supabase_user_data)
|
32 |
print(len(supabase_user_data))
|
33 |
+
|
34 |
+
coords = await process_coordinates(supabase_user_data)
|
35 |
+
|
36 |
+
if not coords:
|
37 |
+
return {"message": "An unexpected error occurred please try again!"}
|
38 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
print(coords)
|
40 |
+
return {"data": coords}
|
|
|
41 |
|
42 |
|
43 |
@router.post("/get_card_reccomendation")
|