bce_response.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 a general response class for BCE services.
  14. """
  15. from future.utils import iteritems
  16. from builtins import str
  17. from builtins import bytes
  18. from baidubce import utils
  19. from baidubce import compat
  20. from baidubce.http import http_headers
  21. class BceResponse(object):
  22. """
  23. """
  24. def __init__(self):
  25. self.metadata = utils.Expando()
  26. def set_metadata_from_headers(self, headers):
  27. """
  28. :param headers:
  29. :return:
  30. """
  31. for k, v in iteritems(headers):
  32. if k.startswith(compat.convert_to_string(http_headers.BCE_PREFIX)):
  33. k = 'bce_' + k[len(compat.convert_to_string(http_headers.BCE_PREFIX)):]
  34. k = utils.pythonize_name(k.replace('-', '_'))
  35. if k.lower() == compat.convert_to_string(http_headers.ETAG.lower()):
  36. v = v.strip('"')
  37. setattr(self.metadata, k, v)
  38. def set_metadata_from_headers_no_underlined(self, headers):
  39. """
  40. :param headers:
  41. :return:
  42. """
  43. for k, v in iteritems(headers):
  44. if k.lower() == compat.convert_to_string(http_headers.ETAG.lower()):
  45. v = v.strip('"')
  46. setattr(self.metadata, k, v)
  47. def __getattr__(self, item):
  48. if item.startswith('__'):
  49. raise AttributeError
  50. return None
  51. def __repr__(self):
  52. return utils.print_object(self)