def process_response(response):
"""All responses should be httplib.OK.
The response should contain a BSON document (content-type
application/bson) or a JSON document (content-type application/json). If
so, the document will be decoded and the result returned, otherwise the
raw binary content will be returned.
:param response: The result of MAASClient.get/post/etc.
:type response: urllib.request.addinfourl (a file-like object that has a
.code attribute.)
"""
if response.code != http.client.OK:
text_status = http.client.responses.get(response.code, '<unknown>')
message = '%s, expected 200 OK' % text_status
raise urllib.error.HTTPError(
response.url, response.code, message,
response.headers, response.fp)
content = response.read()
content_type = response.headers.get_content_type()
if content_type == "application/bson":
return bson.BSON(content).decode()
elif content_type == "application/json":
content_charset = response.headers.get_content_charset()
return json.loads(content.decode(
"utf-8" if content_charset is None else content_charset))
else:
return content
评论列表
文章目录