bos_handler.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright 2014 Baidu, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  4. # except in compliance with the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the
  9. # License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  10. # either express or implied. See the License for the specific language governing permissions
  11. # and limitations under the License.
  12. """
  13. This module provides general http handler functions for processing http responses from bos services.
  14. """
  15. import json
  16. from baidubce import utils
  17. from baidubce.exception import BceServerError
  18. from baidubce.http import handler
  19. from builtins import str
  20. from builtins import bytes
  21. def parse_copy_object_response(http_response, response):
  22. """
  23. response parser for copy object
  24. """
  25. TRANSFER_ENCODING = b'transfer-encoding'
  26. headers_list = {k: v for k, v in http_response.getheaders()}
  27. if headers_list.get(TRANSFER_ENCODING, b'not exist') == b'chunked':
  28. body = http_response.read()
  29. if body:
  30. d = json.loads(body)
  31. if b'code' in d:
  32. http_response.close()
  33. raise BceServerError(d[b'message'], code=d[b'code'], request_id=d[b'requestId'])
  34. else:
  35. response.__dict__.update(
  36. json.loads(body, object_hook=utils.dict_to_python_object).__dict__)
  37. http_response.close()
  38. else:
  39. e = BceServerError(http_response.reason, request_id=response.metadata.bce_request_id)
  40. http_response.close()
  41. raise e
  42. return True
  43. else:
  44. return handler.parse_json(http_response, response)