import requests
import json
from positron_common.exceptions import RemoteCallException
from ..env_config import env
from ..cli.logging_config import logger

def list_jobs():
    Headers = {"PositronAuthToken": env.USER_AUTH_TOKEN}
    url = f'{env.API_BASE}/list-jobs'
    
    logger.debug(f'Calling: {url}')
    response = requests.get(url, headers=Headers)
    
    logger.debug(response)
    if response.status_code != 200:
        body = response.json()
        logger.debug(json.dumps(body, indent=2))
        if body.get('userFriendlyErrorMessage'):
            raise RemoteCallException(body.get('userFriendlyErrorMessage'))
        else:
            raise RemoteCallException(f'list jobs failed with http code: {response.status_code} \n {response.text}')
    else:
        logger.debug(response.json())
        return response.json()
