bce_client_configuration.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 defines a common configuration class for BCE.
  14. """
  15. from future.utils import iteritems
  16. from builtins import str
  17. from builtins import bytes
  18. import baidubce.protocol
  19. import baidubce.region
  20. from baidubce.retry.retry_policy import BackOffRetryPolicy
  21. from baidubce import compat
  22. class BceClientConfiguration(object):
  23. """Configuration of Bce client."""
  24. def __init__(self,
  25. credentials=None,
  26. endpoint=None,
  27. protocol=None,
  28. region=None,
  29. connection_timeout_in_mills=None,
  30. send_buf_size=None,
  31. recv_buf_size=None,
  32. retry_policy=None,
  33. security_token=None,
  34. cname_enabled=False,
  35. backup_endpoint=None,
  36. proxy_host=None,
  37. proxy_port=None,
  38. path_style_enable=False,
  39. auto_follow_redirect=False,
  40. under_line_headers=True,
  41. ):
  42. self.credentials = credentials
  43. self.endpoint = compat.convert_to_bytes(endpoint) if endpoint is not None else endpoint
  44. self.protocol = protocol
  45. self.region = region
  46. self.connection_timeout_in_mills = connection_timeout_in_mills
  47. self.send_buf_size = send_buf_size
  48. self.recv_buf_size = recv_buf_size
  49. self.proxy_host = proxy_host
  50. self.proxy_port = proxy_port
  51. if retry_policy is None:
  52. self.retry_policy = BackOffRetryPolicy()
  53. else:
  54. self.retry_policy = retry_policy
  55. self.security_token = security_token
  56. self.cname_enabled = cname_enabled
  57. self.path_style_enable = path_style_enable
  58. self.backup_endpoint = compat.convert_to_bytes(backup_endpoint) if backup_endpoint is not None else backup_endpoint
  59. self.auto_follow_redirect = auto_follow_redirect
  60. self.under_line_headers = under_line_headers
  61. def merge_non_none_values(self, other):
  62. """
  63. :param other:
  64. :return:
  65. """
  66. for k, v in iteritems(other.__dict__):
  67. if v is not None:
  68. self.__dict__[k] = v
  69. DEFAULT_PROTOCOL = baidubce.protocol.HTTP
  70. DEFAULT_REGION = baidubce.region.BEIJING
  71. DEFAULT_CONNECTION_TIMEOUT_IN_MILLIS = 50 * 1000
  72. DEFAULT_SEND_BUF_SIZE = 1024 * 1024
  73. DEFAULT_RECV_BUF_SIZE = 10 * 1024 * 1024
  74. DEFAULT_CONFIG = BceClientConfiguration(
  75. protocol=DEFAULT_PROTOCOL,
  76. region=DEFAULT_REGION,
  77. connection_timeout_in_mills=DEFAULT_CONNECTION_TIMEOUT_IN_MILLIS,
  78. send_buf_size=DEFAULT_SEND_BUF_SIZE,
  79. recv_buf_size=DEFAULT_RECV_BUF_SIZE,
  80. retry_policy=BackOffRetryPolicy())