2022-09-20 17:49:31 +08:00
|
|
|
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
|
|
|
|
2022-08-02 18:16:28 +08:00
|
|
|
from http import HTTPStatus
|
|
|
|
|
|
2023-05-10 20:58:44 +08:00
|
|
|
import requests
|
2022-07-08 20:03:23 +08:00
|
|
|
from requests.exceptions import HTTPError
|
|
|
|
|
|
2022-10-13 18:30:06 +08:00
|
|
|
from modelscope.utils.logger import get_logger
|
|
|
|
|
|
|
|
|
|
logger = get_logger()
|
|
|
|
|
|
2022-07-08 20:03:23 +08:00
|
|
|
|
2022-10-24 15:12:48 +08:00
|
|
|
class NotSupportError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2022-10-26 13:55:51 +08:00
|
|
|
class NoValidRevisionError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2022-06-21 20:04:25 +08:00
|
|
|
class NotExistError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RequestError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2022-06-24 16:43:32 +08:00
|
|
|
class GitError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2022-06-28 21:12:15 +08:00
|
|
|
class InvalidParameter(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2022-08-02 18:16:28 +08:00
|
|
|
class NotLoginException(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2022-08-11 10:45:04 +08:00
|
|
|
class FileIntegrityError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FileDownloadError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2022-06-21 20:04:25 +08:00
|
|
|
def is_ok(rsp):
|
|
|
|
|
""" Check the request is ok
|
|
|
|
|
|
|
|
|
|
Args:
|
2023-01-03 16:27:29 +08:00
|
|
|
rsp (Response): The request response body
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
bool: `True` if success otherwise `False`.
|
2022-06-21 20:04:25 +08:00
|
|
|
"""
|
2022-08-02 18:16:28 +08:00
|
|
|
return rsp['Code'] == HTTPStatus.OK and rsp['Success']
|
2022-06-21 20:04:25 +08:00
|
|
|
|
|
|
|
|
|
2023-05-10 20:58:44 +08:00
|
|
|
def _decode_response_error(response: requests.Response):
|
|
|
|
|
if 'application/json' in response.headers.get('content-type', ''):
|
|
|
|
|
message = response.json()
|
|
|
|
|
else:
|
|
|
|
|
message = response.content.decode('utf-8')
|
|
|
|
|
return message
|
|
|
|
|
|
|
|
|
|
|
2022-10-13 18:30:06 +08:00
|
|
|
def handle_http_post_error(response, url, request_body):
|
|
|
|
|
try:
|
|
|
|
|
response.raise_for_status()
|
|
|
|
|
except HTTPError as error:
|
2023-05-10 20:58:44 +08:00
|
|
|
message = _decode_response_error(response)
|
2023-07-31 17:14:12 +08:00
|
|
|
raise HTTPError('Request %s with body: %s exception, '
|
|
|
|
|
'Response details: %s' %
|
|
|
|
|
(url, request_body, message)) from error
|
2022-10-13 18:30:06 +08:00
|
|
|
|
|
|
|
|
|
2022-07-08 20:03:23 +08:00
|
|
|
def handle_http_response(response, logger, cookies, model_id):
|
|
|
|
|
try:
|
|
|
|
|
response.raise_for_status()
|
2022-10-13 18:30:06 +08:00
|
|
|
except HTTPError as error:
|
2022-07-08 20:03:23 +08:00
|
|
|
if cookies is None: # code in [403] and
|
|
|
|
|
logger.error(
|
2022-08-26 13:06:41 +08:00
|
|
|
f'Authentication token does not exist, failed to access model {model_id} which may not exist or may be \
|
|
|
|
|
private. Please login first.')
|
2023-05-10 20:58:44 +08:00
|
|
|
message = _decode_response_error(response)
|
2023-07-31 17:14:12 +08:00
|
|
|
raise HTTPError('Response details: %s' % message) from error
|
2022-07-08 20:03:23 +08:00
|
|
|
|
|
|
|
|
|
2022-06-21 20:04:25 +08:00
|
|
|
def raise_on_error(rsp):
|
|
|
|
|
"""If response error, raise exception
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
rsp (_type_): The server response
|
2023-01-03 16:27:29 +08:00
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
RequestError: the response error message.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
bool: True if request is OK, otherwise raise `RequestError` exception.
|
2022-06-21 20:04:25 +08:00
|
|
|
"""
|
2022-09-14 19:24:48 +08:00
|
|
|
if rsp['Code'] == HTTPStatus.OK:
|
2022-06-21 20:04:25 +08:00
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
raise RequestError(rsp['Message'])
|
2022-06-28 20:40:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def datahub_raise_on_error(url, rsp):
|
|
|
|
|
"""If response error, raise exception
|
|
|
|
|
|
|
|
|
|
Args:
|
2023-01-03 16:27:29 +08:00
|
|
|
url (str): The request url
|
|
|
|
|
rsp (HTTPResponse): The server response.
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
RequestError: the http request error.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
bool: `True` if request is OK, otherwise raise `RequestError` exception.
|
2022-06-28 20:40:57 +08:00
|
|
|
"""
|
2022-08-02 18:16:28 +08:00
|
|
|
if rsp.get('Code') == HTTPStatus.OK:
|
2022-06-28 20:40:57 +08:00
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
raise RequestError(
|
2023-01-10 07:01:34 +08:00
|
|
|
f"Url = {url}, Message = {rsp.get('Message')}, Please specify correct dataset_name and namespace."
|
2022-06-28 20:40:57 +08:00
|
|
|
)
|
2022-10-26 13:55:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def raise_for_http_status(rsp):
|
2023-01-03 16:27:29 +08:00
|
|
|
"""Attempt to decode utf-8 first since some servers
|
2022-10-26 13:55:51 +08:00
|
|
|
localize reason strings, for invalid utf-8, fall back
|
|
|
|
|
to decoding with iso-8859-1.
|
2023-01-03 16:27:29 +08:00
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
rsp: The http response.
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
HTTPError: The http error info.
|
2022-10-26 13:55:51 +08:00
|
|
|
"""
|
|
|
|
|
http_error_msg = ''
|
|
|
|
|
if isinstance(rsp.reason, bytes):
|
|
|
|
|
try:
|
|
|
|
|
reason = rsp.reason.decode('utf-8')
|
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
|
reason = rsp.reason.decode('iso-8859-1')
|
|
|
|
|
else:
|
|
|
|
|
reason = rsp.reason
|
|
|
|
|
|
|
|
|
|
if 400 <= rsp.status_code < 500:
|
|
|
|
|
http_error_msg = u'%s Client Error: %s for url: %s' % (rsp.status_code,
|
|
|
|
|
reason, rsp.url)
|
|
|
|
|
|
|
|
|
|
elif 500 <= rsp.status_code < 600:
|
|
|
|
|
http_error_msg = u'%s Server Error: %s for url: %s' % (rsp.status_code,
|
|
|
|
|
reason, rsp.url)
|
|
|
|
|
|
|
|
|
|
if http_error_msg:
|
|
|
|
|
req = rsp.request
|
|
|
|
|
if req.method == 'POST':
|
|
|
|
|
http_error_msg = u'%s, body: %s' % (http_error_msg, req.body)
|
|
|
|
|
raise HTTPError(http_error_msg, response=rsp)
|