import requests
import copy


def get_dimensions_citations_web(headers, altmetric_id):
    """
    Use the dimensions web URL and requests API to obtain the citations
    for a given altmetric id associated with a scholarly article
    Parameters
    ----------
    arg1 | headers: str
        The login headers from dimensions to query
    arg2 | altmetric_id: int
        The altmetric id of a scholarly article
    Returns
    -------
    Dictionary
        dict
    """
    # create the query
    query = """search publications where altmetric_id in""" + \
        str([altmetric_id]) + \
        """return publications[id+doi+altmetric_id+title+times_cited+authors]"""

    # Execute DSL query.
    resp = requests.post(
        'https://app.dimensions.ai/api/dsl.json',
        data=query.encode(),
        headers=headers)

    # check for 200 status
    if resp.status_code == 200 and 'publications' in resp.json() \
       and len(resp.json()['publications']) > 0:
        # just obtain the first author
        response = copy.deepcopy(resp.json()['publications'][0])

        if 'authors' in response.keys():
            # set the first name
            response['first_name'] = response['authors'][0]['first_name'] + \
                ' ' + response['authors'][0]['last_name']

            # remove the authors key
            del response['authors']

        # return the final dict
        return response
    else:
        # return the json
        return dict()