import logging


def parse_agruments(req):
    """Parse Argument from HTTP request"""
    try:
        args = {
            'datasetId': req.route_params.get('datasetId'), 
            'tableName': req.route_params.get('tableName')
        }

        req_body = req.get_json()
        if ('topNRows' not in req_body): 
            args['topNRows'] = 100000 #Set default to the maximum 100,000 rows allowed for a Power BI Tables
        else:
            args['topNRows'] = req_body['topNRows']

        if ('convertToCsv' not in req_body): 
            args['convertToCsv'] = True #Convert to CSV by default
        else:
            args['convertToCsv'] = req_body['convertToCsv']
                
        if ('filePath' not in req_body): #Default file path is /<container name>/<dataset id>/<table name>.csv or .json
            if (args['convertToCsv']):
                args['filePath'] = f"/{args['datasetId']}/{args['tableName']}.csv" 
            else:
                args['filePath'] = f"/{args['datasetId']}/{args['tableName']}.json" 
        else:
            args['filePath'] = req_body['filePath']

        return args        
    except Exception as e:
        logging.exception(e)
        raise