tsdb_handler.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 TSDB services.
  14. """
  15. import http.client
  16. import json
  17. from baidubce import utils
  18. from baidubce.exception import BceClientError
  19. from baidubce.exception import BceServerError
  20. from baidubce.utils import Expando
  21. def parse_json(http_response, response):
  22. """If the body is not empty, convert it to a python object and set as the value of
  23. response.body. http_response is always closed if no error occurs.
  24. :param http_response: the http_response object returned by HTTPConnection.getresponse()
  25. :type http_response: httplib.HTTPResponse
  26. :param response: general response object which will be returned to the caller
  27. :type response: baidubce.BceResponse
  28. :return: always true
  29. :rtype bool
  30. """
  31. body = http_response.read()
  32. if body:
  33. response.__dict__.update(json.loads(body, object_hook=dict_to_python_object).__dict__)
  34. http_response.close()
  35. return True
  36. def dict_to_python_object(d):
  37. """
  38. :param d:
  39. :return:
  40. """
  41. attr = {}
  42. for k, v in list(d.items()):
  43. k = str(k)
  44. attr[k] = v
  45. return Expando(attr)