import json
import requests


def validate_private_contribution(contribution_id,username="",password="",verbose=True):
    """
    validate private contribution in MagIC

    Parameters
    ----------
    contribution_id: int
        ID of MagIC contribution to delete
    username : str
        personal username for MagIC
    password : str
        password for username
    verbose : bool
        if True, print error messages

    Returns
    ---------
    response: API requests.models.Response
        response.status_code: bool
            True : successful validation of private workspace
        response['url'] : str
            URL of request
        response['results'] : dictionary of validation results
        response['method']='POST'
        response['errors'] : str
            if unsuccessful, error message
    """
    api = 'https://api.earthref.org/v1/MagIC/{}'
    response={}
    response['status_code']=False
    response['errors']='Failed to contact database'
    response['method']='POST'
    response['contribution_id']=contribution_id
    try:
        create_response = requests.put(api.format('private/validate'),
                                          params={'id':contribution_id},
                                          auth=(username, password))
        if create_response.status_code==200:
            response['status_code']=True
            response['url']=create_response.request.url
            response['errors']='None'
            errors_dict=json.loads(create_response.text)
            response['validation_results']=errors_dict['validation']['errors']
            if verbose:print('Validated contribution with ID', contribution_id, ':\n', response['validation_results'])
        else:
            response['status_code']=False
            response['url']=create_response.request.url
            response['errors']=create_response.json()['errors'][0]['message']
            response['validation_results']='None'
            print('A private contribution with ID', contribution_id,
                  ' could not be found in your private workspace for validation\n')
    except:
        print ('trouble validating:')
    return response