handler.py 2.9 KB

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