ca_handler.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 CA services.
  14. """
  15. import http.client
  16. import json
  17. from baidubce import compat, utils
  18. from baidubce.exception import BceClientError
  19. from baidubce.exception import BceServerError
  20. def parse_json_list(http_response, response):
  21. """If the body is not empty, convert it to a python object and set autoscaling 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: http.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. body = compat.convert_to_string(body)
  33. response.__dict__["result"] = json.loads(body, object_hook=utils.dict_to_python_object)
  34. response.__dict__["raw_data"] = body
  35. http_response.close()
  36. return True
  37. def parse_error(http_response, response):
  38. """If the body is not empty, convert it to a python object and set autoscaling the value of
  39. response.body. http_response is always closed if no error occurs.
  40. :param http_response: the http_response object returned by HTTPConnection.getresponse()
  41. :type http_response: http.HTTPResponse
  42. :param response: general response object which will be returned to the caller
  43. :type response: baidubce.BceResponse
  44. :return: false if http status code is 2xx, raise an error otherwise
  45. :rtype bool
  46. :raise baidubce.exception.BceClientError: if http status code is NOT 2xx
  47. """
  48. if http_response.status // 100 == http.client.OK // 100:
  49. return False
  50. if http_response.status // 100 == http.client.CONTINUE // 100:
  51. raise BceClientError(b'Can not handle 1xx http status code')
  52. body = http_response.read()
  53. if not body:
  54. bse = BceServerError(http_response.reason, request_id=response.metadata.bce_request_id)
  55. bse.status_code = http_response.status
  56. raise bse
  57. error_dict = json.loads(compat.convert_to_string(body))
  58. message = str(error_dict)
  59. if 'message' in error_dict and error_dict['message'] is not None:
  60. message = error_dict['message']
  61. code = "Exception"
  62. if 'code' in error_dict and error_dict['code'] is not None:
  63. code = error_dict['code']
  64. request_id = response.metadata.bce_request_id
  65. if 'request_id' in error_dict and error_dict['request_id'] is not None:
  66. request_id = error_dict['request_id']
  67. bse = BceServerError(message, code=code, request_id=request_id)
  68. bse.status_code = http_response.status
  69. raise bse