"""Check Taskid status."""
import logging

import requests
from tryagain import retries

from ..exceptions import SpinnakerTaskError

HEADERS = {'Content-Type': 'application/json', 'Accept': '*/*'}
GATE_URL = "http://gate-api.build.example.com:8084"
LOG = logging.getLogger(__name__)


@retries(max_attempts=10, wait=10, exceptions=(AssertionError, ValueError))
def check_task(taskid, app_name):
    """Check task status.

    Args:
        taskid: the task id returned from create_elb.
        app_name: application name related to this task.

    Returns:
        polls for task status.
    """
    try:
        taskurl = taskid.get('ref', '0000')
    except AttributeError:
        taskurl = taskid

    taskid = taskurl.split('/tasks/')[-1]

    LOG.info('Checking taskid %s', taskid)

    url = '{0}/applications/{1}/tasks/{2}'.format(GATE_URL, app_name, taskid)
    task_response = requests.get(url, headers=HEADERS)

    LOG.debug(task_response.json())

    assert task_response.ok

    task_state = task_response.json()
    status = task_state['status']
    LOG.info('Current task status: %s', status)

    if status == 'SUCCEEDED':
        return status
    elif status == 'TERMINAL':
        raise SpinnakerTaskError(task_state)
    else:
        raise ValueError

"""Check Taskid status."""
import logging

import requests
from tryagain import retries

from ..consts import API_URL, HEADERS
from ..exceptions import SpinnakerTaskError

LOG = logging.getLogger(__name__)


@retries(max_attempts=10, wait=10, exceptions=(AssertionError, ValueError))
def check_task(taskid, app_name):
    """Check task status.

    Args:
        taskid: the task id returned from create_elb.
        app_name: application name related to this task.

    Returns:
        polls for task status.
    """
    try:
        taskurl = taskid.get('ref', '0000')
    except AttributeError:
        taskurl = taskid

    taskid = taskurl.split('/tasks/')[-1]

    LOG.info('Checking taskid %s', taskid)

    url = '{0}/applications/{1}/tasks/{2}'.format(API_URL, app_name, taskid)
    task_response = requests.get(url, headers=HEADERS)

    LOG.debug(task_response.json())

    assert task_response.ok

    task_state = task_response.json()
    status = task_state['status']
    LOG.info('Current task status: %s', status)

    if status == 'SUCCEEDED':
        return status
    elif status == 'TERMINAL':
        raise SpinnakerTaskError(task_state)
    else:
        raise ValueError

Use global API_URL and HEADERS
refactor: Use global API_URL and HEADERS

See also: PSOBAT-1197
