File size: 2,145 Bytes
b3b089b
2542be6
 
bacf700
2542be6
b36d2a5
2542be6
 
 
2b9d1a7
 
 
 
 
 
 
 
 
 
f90914e
2b9d1a7
bacf700
 
 
 
 
 
 
 
 
2b9d1a7
 
 
 
 
2542be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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()

@router.post("/get_coordinates")
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}
    


@router.post("/get_card_reccomendation")
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}