import requests
import click


def _show_field_value(url, token, field_name):
    """
    Fetch a resource and extract an attribute from it.

    This function also prints the field right away.
    We use it to show attributes that are to too long to be displayed in a table.

    :param url: url to fetch the data
    :param token: user token
    :param field_name: field to be printed
    """
    r = requests.get(url, headers={'Authorization': 'Token {}'.format(token)})

    if r.status_code == 200:
        click.echo(r.json().get(field_name))
    else:
        click.echo(r.content)
        exit(1)

    return r