cfc_handler.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 BCE services.
  14. """
  15. import http.client
  16. import json
  17. from baidubce.exception import BceClientError
  18. from baidubce.exception import BceServerError
  19. from baidubce import utils
  20. def parse_json(http_response, response):
  21. """If the body is not empty, convert it to a python object and set as the value of
  22. response.body. http_response is always closed if no error occurs.
  23. :param http_response: the http_response object returned by HTTPConnection.getresponse()
  24. :type http_response: httplib.HTTPResponse
  25. :param response: general response object which will be returned to the caller
  26. :type response: baidubce.BceResponse
  27. :return: always true
  28. :rtype bool
  29. """
  30. body = http_response.read()
  31. if body:
  32. response.__dict__.update(json.loads(body, object_hook=dict_to_python_object).__dict__)
  33. http_response.close()
  34. return True
  35. def parse_error(http_response, response):
  36. """If the body is not empty, convert it to a python object and set as the value of
  37. response.body. http_response is always closed if no error occurs.
  38. :param http_response: the http_response object returned by HTTPConnection.getresponse()
  39. :type http_response: httplib.HTTPResponse
  40. :param response: general response object which will be returned to the caller
  41. :type response: baidubce.BceResponse
  42. :return: false if http status code is 2xx, raise an error otherwise
  43. :rtype bool
  44. :raise baidubce.exception.BceClientError: if http status code is NOT 2xx
  45. """
  46. if http_response.status // 100 == http.client.OK // 100:
  47. return False
  48. if http_response.status // 100 == http.client.CONTINUE // 100:
  49. raise BceClientError(b'Can not handle 1xx http status code')
  50. bse = None
  51. body = http_response.read()
  52. if body:
  53. d = json.loads(body)
  54. if 'code' in d:
  55. bse = BceServerError(d['message'], code=d['code'], request_id=d['requestId'])
  56. else:
  57. bse = BceServerError(d['Message'], code=d['Code'], request_id="")
  58. if bse is None:
  59. bse = BceServerError(http_response.reason, request_id=response.metadata.bce_request_id)
  60. bse.status_code = http_response.status
  61. raise bse
  62. def dict_to_python_object(d):
  63. """
  64. :param d:
  65. :return:
  66. """
  67. attr = {}
  68. for k, v in d.items():
  69. k = (str(k))
  70. attr[k] = v
  71. return utils.Expando(attr)