bce_base_client.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 provide base class for BCE service clients.
  14. """
  15. from __future__ import absolute_import
  16. import copy
  17. from builtins import str, bytes
  18. import baidubce
  19. from baidubce import bce_client_configuration
  20. from baidubce.exception import BceClientError
  21. from baidubce.auth import bce_v1_signer
  22. from baidubce.http import handler
  23. from baidubce.http import bce_http_client
  24. class BceBaseClient(object):
  25. """
  26. TODO: add docstring
  27. """
  28. def __init__(self, config, region_supported=True):
  29. """
  30. :param config: the client configuration. The constructor makes a copy of this parameter so
  31. that it is safe to change the configuration after then.
  32. :type config: BceClientConfiguration
  33. :param region_supported: true if this client supports region.
  34. :type region_supported: bool
  35. """
  36. self.service_id = self._compute_service_id()
  37. self.region_supported = region_supported
  38. # just for debug
  39. self.config = copy.deepcopy(bce_client_configuration.DEFAULT_CONFIG)
  40. if config is not None:
  41. self.config.merge_non_none_values(config)
  42. if self.config.endpoint is None:
  43. self.config.endpoint = self._compute_endpoint()
  44. def _compute_service_id(self):
  45. return self.__module__.split('.')[2]
  46. def _compute_endpoint(self):
  47. if self.config.endpoint:
  48. return self.config.endpoint
  49. if self.region_supported:
  50. return b'%s://%s.%s.%s' % (
  51. self.config.protocol,
  52. self.service_id,
  53. self.config.region,
  54. baidubce.DEFAULT_SERVICE_DOMAIN)
  55. else:
  56. return b'%s://%s.%s' % (
  57. self.config.protocol,
  58. self.service_id,
  59. baidubce.DEFAULT_SERVICE_DOMAIN)
  60. def _send_request(self, http_method, path, headers=None, params=None, body=None):
  61. return bce_http_client.send_request(
  62. self.config, bce_v1_signer.sign, [handler.parse_error, handler.parse_json],
  63. http_method, path, body, headers, params)
  64. def _get_config(self, apiDict, apiName):
  65. return copy.deepcopy(apiDict[apiName])
  66. def _add_header(self, apiConfig, key, value):
  67. self._set_if_nonnull(apiConfig["headers"], key, value)
  68. def _add_query(self, apiConfig, key, value):
  69. # key-only query parameter's value is "" and can satisfy non-null
  70. self._set_if_nonnull(apiConfig["queries"], key, value)
  71. def _add_path_param(self, apiConfig, key, value):
  72. if value is None:
  73. raise BceClientError(b"Path param can't be none.")
  74. apiConfig["path"] = apiConfig["path"].replace("[" + key + "]", value)
  75. def _set_if_nonnull(self, params, param_name=None, value=None):
  76. if value is not None:
  77. params[param_name] = value