示例#1
0
def _post(url, body, headers):
    """
    Send a post request to DataCite.

    Args:
        url (str): The URL to call
        body (str): The data
        headers (dict): A dictionary of headers to use

    Return:
        a HTTPResponse

    """
    _set_timeout()
    opener = get_opener()
    auth_string = (base64.encodestring(DATACITE_USER_NAME + ':'
                                       + DATACITE_PASSWORD)).rstrip()
    headers.update({'Authorization':'Basic ' + auth_string})

    # If the request body is a string, urllib2 attempts to concatenate the url,
    # body and headers. If the url is unicode, the request body can get
    # converted unicode. This has resulted in issues where there are characters
    # with diacritic marks in the request body. To avoid these issues the url is
    # UTF-8 encoded.
    url_encode = url.encode('utf-8')

    req = urllib2.Request(url_encode, data=body, headers=headers)
    try:
        response = opener.open(req)
    except (urllib2.HTTPError) as ex:
        msg = ex.readlines()
        LOGGING.warn('HTTPError error getting %s. %s', url, msg)
        return get_response(msg, ex.code)
    except (socket.timeout, urllib2.URLError) as ex:
        LOGGING.warn('Timeout or URLError error getting %s. %s', url, ex.reason)
        return get_response(ex.reason, 500)
    except (SSLError) as ex:
        LOGGING.warn('SSLError error getting %s. %s', url, ex)
        return get_response(ex, 500)
    except UnicodeDecodeError as ex:
        LOGGING.info('UnicodeDecodeError error getting %s. %s', url, ex)
        return get_response(ex, 500)
    finally:
        _close(opener)
    if response.headers.has_key('Content-Type'):
        ret_response = HttpResponse(content_type=
                                    response.headers.get('Content-Type'))
    else:
        ret_response = HttpResponse()
    ret_response.status_code = response.code
    ret_response.reason_phrase = response.msg
    # pylint: disable=maybe-no-member
    ret_response.writelines(response.readlines())
    if response.headers.has_key('location'):
        ret_response.setdefault('Location', response.headers.get('location'))
    return ret_response
示例#2
0
def get(request_method, url, headers):
    """
    Send a get or head request to DataCite.

    Args:
        request_method (str): This should be 'GET' or 'HEAD'
        url (str): The URL to call
        headers (dict): A dictionary of headers to use

    Return:
        a HTTPResponse

    """
    LOGGING.info("get(%s,%s,%s)", request_method, url, headers)
    _set_timeout()
    opener = get_opener()
    auth_string = ((base64.encodebytes(
        (DATACITE_USER_NAME + ":" +
         DATACITE_PASSWORD).encode())).decode("utf-8").rstrip())
    headers.update({"Authorization": "Basic " + auth_string})
    req = urllib.request.Request(url, data=None, headers=headers)
    if request_method == "HEAD":
        req.get_method = lambda: "HEAD"
    try:
        response = opener.open(req)
    except urllib.error.HTTPError as ex:
        msg = ex.readlines()
        if ex.code in [404, 410]:
            LOGGING.info("HTTPError error getting %s. %s", url, msg)
        else:
            LOGGING.warning("HTTPError error getting %s. %s", url, msg)
        return get_response(msg, ex.code)
    except (socket.timeout, urllib.error.URLError) as ex:
        LOGGING.warning("Timeout or URLError error getting %s. %s", url,
                        ex.reason)
        if isinstance(ex.reason, Exception):
            ex = ex.reason
            LOGGING.warning("Nested exception %s. %s", ex, ex.reason)
        return get_response(ex.reason, 500)
    except SSLError as ex:
        LOGGING.warning("SSLError error getting %s. %s", url, ex)
        return get_response(ex, 500)
    finally:
        _close(opener)
    if "Content-Type" in response.headers:
        ret_response = HttpResponse(
            content_type=response.headers.get("Content-Type"))
    else:
        ret_response = HttpResponse()
    ret_response.status_code = response.code
    ret_response.reason_phrase = response.msg
    # pylint: disable=maybe-no-member
    ret_response.writelines(response.readlines())
    return ret_response
示例#3
0
def get(request_method, url, headers):
    """
    Send a get or head request to DataCite.

    Args:
        request_method (str): This should be 'GET' or 'HEAD'
        url (str): The URL to call
        headers (dict): A dictionary of headers to use

    Return:
        a HTTPResponse

    """
    LOGGING.info('get(%s,%s,%s)', request_method, url, headers)
    _set_timeout()
    opener = get_opener()
    auth_string = (base64.encodestring(DATACITE_USER_NAME + ':' +
                                       DATACITE_PASSWORD)).rstrip()
    headers.update({'Authorization': 'Basic ' + auth_string})
    req = urllib2.Request(url, data=None, headers=headers)
    if request_method == "HEAD":
        req.get_method = lambda: 'HEAD'
    try:
        response = opener.open(req)
    except (urllib2.HTTPError) as ex:
        msg = ex.readlines()
        if ex.code in [404, 410]:
            LOGGING.info('HTTPError error getting %s. %s', url, msg)
        else:
            LOGGING.warn('HTTPError error getting %s. %s', url, msg)
        return get_response(msg, ex.code)
    except (socket.timeout, urllib2.URLError) as ex:
        LOGGING.warn('Timeout or URLError error getting %s. %s', url,
                     ex.reason)
        return get_response(ex.reason, 500)
    except (SSLError) as ex:
        LOGGING.warn('SSLError error getting %s. %s', url, ex)
        return get_response(ex, 500)
    finally:
        _close(opener)
    if response.headers.has_key('Content-Type'):
        ret_response = HttpResponse(
            content_type=response.headers.get('Content-Type'))
    else:
        ret_response = HttpResponse()
    ret_response.status_code = response.code
    ret_response.reason_phrase = response.msg
    # pylint: disable=maybe-no-member
    ret_response.writelines(response.readlines())
    return ret_response
示例#4
0
def get(request_method, url, headers):
    """
    Send a get or head request to DataCite.

    Args:
        request_method (str): This should be 'GET' or 'HEAD'
        url (str): The URL to call
        headers (dict): A dictionary of headers to use

    Return:
        a HTTPResponse

    """
    LOGGING.info('get(%s,%s,%s)', request_method, url, headers)
    _set_timeout()
    opener = get_opener()
    auth_string = (base64.encodestring(DATACITE_USER_NAME + ':'
                                       + DATACITE_PASSWORD)).rstrip()
    headers.update({'Authorization':'Basic ' + auth_string})
    req = urllib2.Request(url, data=None, headers=headers)
    if request_method == "HEAD":
        req.get_method = lambda: 'HEAD'
    try:
        response = opener.open(req)
    except (urllib2.HTTPError) as ex:
        msg = ex.readlines()
        if ex.code in [404, 410]:
            LOGGING.info('HTTPError error getting %s. %s', url, msg)
        else:
            LOGGING.warn('HTTPError error getting %s. %s', url, msg)
        return get_response(msg, ex.code)
    except (socket.timeout, urllib2.URLError) as ex:
        LOGGING.warn('Timeout or URLError error getting %s. %s', url, ex.reason)
        return get_response(ex.reason, 500)
    except (SSLError) as ex:
        LOGGING.warn('SSLError error getting %s. %s', url, ex)
        return get_response(ex, 500)
    finally:
        _close(opener)
    if response.headers.has_key('Content-Type'):
        ret_response = HttpResponse(content_type=
                                    response.headers.get('Content-Type'))
    else:
        ret_response = HttpResponse()
    ret_response.status_code = response.code
    ret_response.reason_phrase = response.msg
    # pylint: disable=maybe-no-member
    ret_response.writelines(response.readlines())
    return ret_response
示例#5
0
def _delete(url):
    """
    Send a delete request to DataCite.

    Args:
        url (str): The URL to call

    Return:
        a HTTPResponse

    """
    _set_timeout()
    opener = get_opener()
    auth_string = ((base64.encodebytes(
        (DATACITE_USER_NAME + ":" +
         DATACITE_PASSWORD).encode())).decode("utf-8").rstrip())
    headers = {"Authorization": "Basic " + auth_string}
    req = urllib.request.Request(url, data=None, headers=headers)
    req.get_method = lambda: "DELETE"
    try:
        response = opener.open(req)
    except urllib.error.HTTPError as ex:
        msg = ex.readlines()
        LOGGING.warning("HTTPError error getting %s. %s", url, msg)
        return get_response(msg, ex.code)
    except (socket.timeout, urllib.error.URLError) as ex:
        LOGGING.warning("Timeout or URLError error getting %s. %s", url,
                        ex.reason)
        return get_response(ex.reason, 500)
    except SSLError as ex:
        LOGGING.warning("SSLError error getting %s. %s", url, ex)
        return get_response(ex, 500)
    finally:
        _close(opener)
    if "Content-Type" in response.headers:
        ret_response = HttpResponse(
            content_type=response.headers.get("Content-Type"))
    else:
        ret_response = HttpResponse()
    ret_response.status_code = response.code
    ret_response.reason_phrase = response.msg
    # pylint: disable=maybe-no-member
    ret_response.writelines(response.readlines())
    return ret_response
示例#6
0
def _delete(url):
    """
    Send a delete request to DataCite.

    Args:
        url (str): The URL to call

    Return:
        a HTTPResponse

    """
    _set_timeout()
    opener = get_opener()
    auth_string = (base64.encodestring(DATACITE_USER_NAME + ':' +
                                       DATACITE_PASSWORD)).rstrip()
    headers = {'Authorization': 'Basic ' + auth_string}
    req = urllib2.Request(url, data=None, headers=headers)
    req.get_method = lambda: 'DELETE'
    try:
        response = opener.open(req)
    except (urllib2.HTTPError) as ex:
        msg = ex.readlines()
        LOGGING.warn('HTTPError error getting %s. %s', url, msg)
        return get_response(msg, ex.code)
    except (socket.timeout, urllib2.URLError) as ex:
        LOGGING.warn('Timeout or URLError error getting %s. %s', url,
                     ex.reason)
        return get_response(ex.reason, 500)
    except (SSLError) as ex:
        LOGGING.warn('SSLError error getting %s. %s', url, ex)
        return get_response(ex, 500)
    finally:
        _close(opener)
    if response.headers.has_key('Content-Type'):
        ret_response = HttpResponse(
            content_type=response.headers.get('Content-Type'))
    else:
        ret_response = HttpResponse()
    ret_response.status_code = response.code
    ret_response.reason_phrase = response.msg
    # pylint: disable=maybe-no-member
    ret_response.writelines(response.readlines())
    return ret_response
示例#7
0
def _delete(url):
    """
    Send a delete request to DataCite.

    Args:
        url (str): The URL to call

    Return:
        a HTTPResponse

    """
    _set_timeout()
    opener = get_opener()
    auth_string = (base64.encodestring(DATACITE_USER_NAME + ':'
                                       + DATACITE_PASSWORD)).rstrip()
    headers = {'Authorization':'Basic ' + auth_string}
    req = urllib2.Request(url, data=None, headers=headers)
    req.get_method = lambda: 'DELETE'
    try:
        response = opener.open(req)
    except (urllib2.HTTPError) as ex:
        msg = ex.readlines()
        LOGGING.warn('HTTPError error getting %s. %s', url, msg)
        return get_response(msg, ex.code)
    except (socket.timeout, urllib2.URLError) as ex:
        LOGGING.warn('Timeout or URLError error getting %s. %s', url, ex.reason)
        return get_response(ex.reason, 500)
    except (SSLError) as ex:
        LOGGING.warn('SSLError error getting %s. %s', url, ex)
        return get_response(ex, 500)
    finally:
        _close(opener)
    if response.headers.has_key('Content-Type'):
        ret_response = HttpResponse(content_type=
                                    response.headers.get('Content-Type'))
    else:
        ret_response = HttpResponse()
    ret_response.status_code = response.code
    ret_response.reason_phrase = response.msg
    # pylint: disable=maybe-no-member
    ret_response.writelines(response.readlines())
    return ret_response
示例#8
0
def _post(url, body, headers, method="POST"):
    """
    Send a post request to DataCite.

    Args:
        url (str): The URL to call
        body (str): The data
        headers (dict): A dictionary of headers to use

    Return:
        a HTTPResponse

    """
    _set_timeout()
    opener = get_opener()
    auth_string = ((base64.encodebytes(
        (DATACITE_USER_NAME + ":" +
         DATACITE_PASSWORD).encode())).decode("utf-8").rstrip())
    headers.update({"Authorization": "Basic " + auth_string})

    # If the request body is a string, urllib2 attempts to concatenate the url,
    # body and headers. If the url is unicode, the request body can get
    # converted unicode. This has resulted in issues where there are characters
    # with diacritic marks in the request body. To avoid these issues the url is
    # UTF-8 encoded.
    url_encode = url.encode("utf-8").decode()

    req = urllib.request.Request(url_encode,
                                 data=body,
                                 headers=headers,
                                 method=method)
    try:
        response = opener.open(req)
    except urllib.error.HTTPError as ex:
        msg = ex.readlines()
        LOGGING.warning("HTTPError error getting %s. %s", url, msg)
        return get_response(msg, ex.code)
    except (socket.timeout, urllib.error.URLError) as ex:
        LOGGING.warning("Timeout or URLError error getting %s. %s", url,
                        ex.reason)
        return get_response(ex.reason, 500)
    except SSLError as ex:
        LOGGING.warning("SSLError error getting %s. %s", url, ex)
        return get_response(ex, 500)
    except UnicodeDecodeError as ex:
        LOGGING.info("UnicodeDecodeError error getting %s. %s", url, ex)
        return get_response(ex, 500)
    finally:
        _close(opener)
    if "Content-Type" in response.headers:
        ret_response = HttpResponse(
            content_type=response.headers.get("Content-Type"))
    else:
        ret_response = HttpResponse()
    ret_response.status_code = response.code
    ret_response.reason_phrase = response.msg
    # pylint: disable=maybe-no-member
    ret_response.writelines(response.readlines())
    if "location" in response.headers:
        ret_response.setdefault("Location", response.headers.get("location"))
    return ret_response