def _get_repository(repository_id):
"""Get a repository from the accounts service
:param repository_id: str
:returns: repository resource
:raises: koi.exceptions.HTTPError
"""
client = API(options.url_accounts, ssl_options=ssl_server_options())
try:
repo = yield client.accounts.repositories[repository_id].get()
raise Return(repo)
except httpclient.HTTPError as exc:
if exc.code == 404:
msg = 'Unknown repository ID'
else:
msg = 'Unexpected error'
raise exceptions.HTTPError(exc.code, msg, source='accounts')
python类HTTPError()的实例源码
def _get_provider_by_name(provider):
"""Get a provider from the accounts service
:param provider: str
:returns: organisation resource
:raises: koi.exceptions.HTTPError
"""
client = API(options.url_accounts, ssl_options=ssl_server_options())
try:
res = yield client.accounts.organisations.get(name=provider)
raise Return(res['data'][0])
except httpclient.HTTPError as exc:
if exc.code == 404:
msg = 'Unknown provider'
else:
msg = 'Unexpected error ' + exc.message
raise exceptions.HTTPError(exc.code, msg, source='accounts')
def _get_provider(provider_id):
"""Get a provider from the accounts service
:param provider_id: str
:returns: organisation resource
:raises: koi.exceptions.HTTPError
"""
client = API(options.url_accounts, ssl_options=ssl_server_options())
try:
org = yield client.accounts.organisations[provider_id].get()
raise Return(org)
except httpclient.HTTPError as exc:
if exc.code == 404:
msg = 'Unknown provider ID'
else:
msg = 'Unexpected error'
raise exceptions.HTTPError(exc.code, msg, source='accounts')
def _get_ids(repository_id, entity_id):
"""Get ids from the repository service
:param provider_id: str
:returns: organisation resource
:raises: koi.exceptions.HTTPError
"""
repository = yield _get_repository(repository_id)
repository_url = repository['data']['service']['location']
token = yield get_token(
options.url_auth, options.service_id,
options.client_secret, scope=Read(),
ssl_options=ssl_server_options()
)
client = API(repository_url, token=token, ssl_options=ssl_server_options())
try:
res = yield client.repository.repositories[repository_id].assets[entity_id].ids.get()
raise Return(res['data'])
except httpclient.HTTPError as exc:
raise exceptions.HTTPError(exc.code, str(exc), source='repository')
def _get_offers_by_type_and_id(source_id_type, source_id):
""" get asset offers for given type and id
:param source_id_type: str
:param source_id: str
:returns: list of offers json
:raises: koi.exceptions.HTTPError
"""
client = API(options.url_query, ssl_options=ssl_server_options())
try:
req_body = '[{"source_id_type": "' + source_id_type + '", "source_id": "' + source_id + '"}]'
client.query.search.offers.prepare_request(headers={'Content-Type': 'application/json'},
body=req_body.strip())
res = yield client.query.search.offers.post()
raise Return(res['data'])
except httpclient.HTTPError as exc:
msg = 'Unexpected error ' + exc.message
raise exceptions.HTTPError(exc.code, msg, source='query')
def _get_providers_by_type_and_id(source_id_type, source_id):
""" get the matching providers for a given source_id_type and source_id
:param source_id_type: str
:param source_id: str
:returns: list of organisations
:raises: koi.exceptsion.HTTPError
"""
client = API(options.url_query, ssl_options=ssl_server_options())
try:
res = yield client.query.licensors.get(source_id_type=source_id_type, source_id=source_id)
raise Return(res['data'])
except httpclient.HTTPError as exc:
if exc.code == 404:
msg = 'No matching providers found'
else:
msg = 'Unexpected error ' + exc.message
raise exceptions.HTTPError(exc.code, msg, source='query')
def fetch(url, *args, **kwargs):
retry_max = kwargs.pop('retry_max', 5)
retry = 0
while True:
try:
result = yield http_client.fetch(url, *args, **kwargs)
return result
except HTTPError as e:
if e.code == 599:
retry += 1
if retry < retry_max:
logger.error(
"Timeout in request: "
"sleeping for {}: {}".format(2**retry, url)
)
yield gen.sleep(2**retry)
continue
raise
def find_faces_url(url, hash_face=False, upsample=1):
"""
Given a URL to an image, find all the faces. The returned list has
dictionaries with the following fields:
rect -> bounding rectangle around the face
score -> score of the face detection (higher == better)
pose -> index of sub-detector matched which roughly corresponds to pos
(0 is best)
"""
try:
image_req = yield httpclient.fetch(url, request_timeout=30.0)
except HTTPError as e:
print("Exception while fetching image URL: {}: {}".format(url, e))
return []
if image_req.code != 200:
return []
image_fd = BytesIO(image_req.body)
return (yield find_faces_buffer(image_fd, hash_face=hash_face,
upsample=upsample))
def facebook_paginate(data, max_results=500):
paginated_data = []
while True:
paginated_data.extend(data['data'])
if max_results is not None and len(paginated_data) >= max_results:
break
try:
paginate_url = data['paging']['next']
except KeyError:
break
try:
request = yield httpclient.fetch(paginate_url,
validate_cert=False,
request_timeout=10.0)
except HTTPError:
logger.exception("Exception while paginating facebook")
break
data = json.loads(request.body)
return paginated_data
def send_async_request(url, headers=None, method="GET", body=None):
headers = headers or {}
if body or method.upper() == "POST":
if 'Content-Type' not in headers:
headers["Content-Type"] = "application/x-www-form-urlencoded"
req = httpclient.HTTPRequest(
url, method=method, body=body, headers=headers, allow_nonstandard_methods=True)
http_request = AsyncHTTPClient()
response = ""
try:
response = yield http_request.fetch(req)
except httpclient.HTTPError as e:
print("Error:" + str(e))
except Exception as e:
print("Error:" + str(e))
else:
return response
finally:
http_request.close()
def send_sync_request(url, headers=None, method="GET", body=None):
headers = headers or {}
if body or method.upper() == "POST":
if "Content-Type" not in headers:
headers["Content-Type"] = "application/x-www-form-urlencoded"
req = httpclient.HTTPRequest(
url, method=method, body=body, headers=headers, allow_nonstandard_methods=True)
http_client = httpclient.HTTPClient()
response = ""
try:
response = http_client.fetch(req)
return response
except httpclient.HTTPError as e:
print("Error:" + str(e))
raise httpclient.HTTPError()
except Exception as e:
print("Error:" + str(e))
raise Exception
finally:
http_client.close()
def _on_timeout(self, key, info=None):
"""Timeout callback of request.
Construct a timeout HTTPResponse when a timeout occurs.
:arg object key: A simple object to mark the request.
:info string key: More detailed timeout information.
"""
request, callback, timeout_handle = self.waiting[key]
self.queue.remove((key, request, callback))
error_message = "Timeout {0}".format(info) if info else "Timeout"
timeout_response = HTTPResponse(
request, 599, error=HTTPError(599, error_message),
request_time=self.io_loop.time() - request.start_time)
self.io_loop.add_callback(callback, timeout_response)
del self.waiting[key]
def _handle_exception(self, typ, value, tb):
if self.final_callback:
self._remove_timeout()
if isinstance(value, StreamClosedError):
if value.real_error is None:
value = HTTPError(599, "Stream closed")
else:
value = value.real_error
self._run_callback(HTTPResponse(self.request, 599, error=value,
request_time=self.io_loop.time() - self.start_time,
))
if hasattr(self, "stream"):
# TODO: this may cause a StreamClosedError to be raised
# by the connection's Future. Should we cancel the
# connection more gracefully?
self.stream.close()
return True
else:
# If our callback has already been called, we are probably
# catching an exception that is not caused by us but rather
# some child of our callback. Rather than drop it on the floor,
# pass it along, unless it's just the stream being closed.
return isinstance(value, StreamClosedError)
def send_email(to, subject, html):
if isinstance(to, unicode):
to = to.encode('utf-8')
if isinstance(subject, unicode):
subject = subject.encode('utf-8')
if isinstance(html, unicode):
html = html.encode('utf-8')
data = {
'from': CONFIG.EMAIL_SENDER,
'to': to,
'subject': subject,
'html': html
}
data = urlencode(data)
request = HTTPRequest(
url=_MAILGUN_API_URL,
method='POST',
auth_username='api',
auth_password=CONFIG.MAILGUN_API_KEY,
body=data
)
client = AsyncHTTPClient()
try:
yield client.fetch(request)
except HTTPError as e:
try:
response = e.response.body
except AttributeError:
response = None
logging.exception('failed to send email:\nto: %s\nsubject: %s\nhtml: %s\nresponse: %s', to, subject, html, response)
def _on_timeout(self, key):
request, callback, timeout_handle = self.waiting[key]
self.queue.remove((key, request, callback))
timeout_response = HTTPResponse(
request, 599, error=HTTPError(599, "Timeout"),
request_time=self.io_loop.time() - request.start_time)
self.io_loop.add_callback(callback, timeout_response)
del self.waiting[key]
def _on_timeout(self):
self._timeout = None
if self.final_callback is not None:
raise HTTPError(599, "Timeout")
def on_connection_close(self):
if self.final_callback is not None:
message = "Connection closed"
if self.stream.error:
raise self.stream.error
try:
raise HTTPError(599, message)
except HTTPError:
self._handle_exception(*sys.exc_info())
def test_websocket_http_fail(self):
with self.assertRaises(HTTPError) as cm:
yield self.ws_connect('/notfound')
self.assertEqual(cm.exception.code, 404)
def test_check_origin_invalid(self):
port = self.get_http_port()
url = 'ws://127.0.0.1:%d/echo' % port
# Host is 127.0.0.1, which should not be accessible from some other
# domain
headers = {'Origin': 'http://somewhereelse.com'}
with self.assertRaises(HTTPError) as cm:
yield websocket_connect(HTTPRequest(url, headers=headers),
io_loop=self.io_loop)
self.assertEqual(cm.exception.code, 403)
def test_check_origin_invalid_subdomains(self):
port = self.get_http_port()
url = 'ws://localhost:%d/echo' % port
# Subdomains should be disallowed by default. If we could pass a
# resolver to websocket_connect we could test sibling domains as well.
headers = {'Origin': 'http://subtenant.localhost'}
with self.assertRaises(HTTPError) as cm:
yield websocket_connect(HTTPRequest(url, headers=headers),
io_loop=self.io_loop)
self.assertEqual(cm.exception.code, 403)