Send results to where requested.

    If api_key is provided, it is used, otherwiese
    the key from the job will be used.

def send_result(job_id, api_key=None):
    ''' Send results to where requested.

    If api_key is provided, it is used, otherwiese
    the key from the job will be used.
    '''
    job_dict = db.get_job(job_id)
    result_url = job_dict.get('result_url')

    if not result_url:

        # A job with an API key (for using when posting to the callback URL)
        # but no callback URL is weird, but it can happen.
        db.delete_api_key(job_id)

        return True

    api_key_from_job = job_dict.pop('api_key', None)
    if not api_key:
        api_key = api_key_from_job
    headers = {'Content-Type': 'application/json'}
    if api_key:
        if ':' in api_key:
            header, key = api_key.split(':')
        else:
            header, key = 'Authorization', api_key
        headers[header] = key

    try:
        result = requests.post(
            result_url,
            data=json.dumps(job_dict, cls=DatetimeJsonEncoder),
            headers=headers, verify=SSL_VERIFY)

        db.delete_api_key(job_id)

    except requests.ConnectionError:
        return False

    return result.status_code == requests.codes.ok