import requests
import pickle


def api_request(file, thresh=0.5):
    """
    Post request to serverless backend api where our model is lcoated
    Receives a csv with the classes and polygons classified by our model 

    Parameters
    ----------
    file: .tiff file
        Tiff file to be classified by our model
    thresh: float 
        Threshold applied. To be classified a prediction values has to be equal or greater than threshold 
    """
    try:
        req = requests.post('https://hxy1cn1sl8.execute-api.us-east-1.amazonaws.com/Prod/segment_tiff',
                            data=file, params={'threshold': thresh})

        polygons = pickle.loads(req.content)
        polygons.to_csv(f'src/data/polygons.csv', index=False)
    except:
        return api_request(file)