import requests


def http_request(method, url, headers, data=None):
    """
    Request util
    :param method: GET or POST or PUT
    :param url: url
    :param headers: headers
    :param data: optional data (needed for POST)
    :return: response text
    """
    response = requests.request(method, url, headers=headers, data=data)
    if response.status_code not in [200, 201, 204]:
        http_error_msg = u'%s HTTP request failed: %s for url: %s' % (response.status_code, response.text, url)
        #print ("utils.http_request ", http_error_msg)
        raise requests.exceptions.HTTPError(response.text)
    return response.text