cert_client.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # Copyright (c) 2020 Baidu.com, Inc. All Rights Reserved
  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 client class for CERT.
  14. """
  15. from __future__ import unicode_literals
  16. import copy
  17. import json
  18. import logging
  19. from baidubce import bce_base_client
  20. from baidubce import utils
  21. from baidubce.auth import bce_v1_signer
  22. from baidubce.http import bce_http_client
  23. from baidubce.http import handler
  24. from baidubce.http import http_content_types
  25. from baidubce.http import http_headers
  26. from baidubce.http import http_methods
  27. from baidubce.services.cert import cert_model
  28. _logger = logging.getLogger(__name__)
  29. class CertClient(bce_base_client.BceBaseClient):
  30. """
  31. CertClient
  32. """
  33. prefix = b"/v1/certificate"
  34. def __init__(self, config=None):
  35. bce_base_client.BceBaseClient.__init__(self, config)
  36. @staticmethod
  37. def _merge_config(self, config):
  38. if config is None:
  39. return self.config
  40. else:
  41. new_config = copy.copy(self.config)
  42. new_config.merge_non_none_values(config)
  43. return new_config
  44. def _send_request(
  45. self, http_method, path,
  46. body=None, headers=None, params=None,
  47. config=None,
  48. body_parser=None):
  49. config = self._merge_config(self, config)
  50. if body_parser is None:
  51. body_parser = handler.parse_json
  52. headers = headers or {}
  53. headers[http_headers.CONTENT_TYPE] = http_content_types.JSON
  54. return bce_http_client.send_request(
  55. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  56. http_method, utils.append_uri(CertClient.prefix, path), body, headers, params)
  57. def create_cert(self, cert_create_request, config=None):
  58. """
  59. create certificate
  60. :param cert_create_request: certificate base informations
  61. :type cert_create_request: cert_model.CertCreateRequest
  62. :param config: None
  63. :type config: baidubce.BceClientConfiguration
  64. :return:
  65. :rtype: baidubce.bce_response.BceResponse
  66. """
  67. return self._send_request(
  68. http_methods.POST, '', body=json.dumps(cert_create_request.__dict__),
  69. config=config)
  70. def list_user_certs(self, config=None):
  71. """
  72. list user's certificates
  73. :param config: None
  74. :type config: baidubce.BceClientConfiguration
  75. :return:
  76. :rtype: baidubce.bce_response.BceResponse
  77. """
  78. return self._send_request(
  79. http_methods.GET, '',
  80. config=config)
  81. def get_cert_info(self, cert_id, config=None):
  82. """
  83. get a certificate information by id
  84. :param cert_id: certificate id
  85. :type cert_id: string
  86. :param config: None
  87. :type config: baidubce.BceClientConfiguration
  88. :return:
  89. :rtype: baidubce.bce_response.BceResponse
  90. """
  91. return self._send_request(
  92. http_methods.GET, '/' + cert_id,
  93. config=config)
  94. def delete_cert(self, cert_id, config=None):
  95. """
  96. delete a certificate by id
  97. :param cert_id: certificate id
  98. :type cert_id: string
  99. :param config: None
  100. :type config: baidubce.BceClientConfiguration
  101. :return:
  102. :rtype: baidubce.bce_response.BceResponse
  103. """
  104. return self._send_request(
  105. http_methods.DELETE, '/' + cert_id,
  106. config=config)
  107. def replace_cert(self, cert_id, cert_create_request, config=None):
  108. """
  109. delete a certificate by id
  110. :param cert_id: certificate id
  111. :type cert_id: string
  112. :param cert_create_request: certificate base informations
  113. :type cert_create_request: cert_model.CertCreateRequest
  114. :param config: None
  115. :type config: baidubce.BceClientConfiguration
  116. :return:
  117. :rtype: baidubce.bce_response.BceResponse
  118. """
  119. return self._send_request(
  120. http_methods.PUT, '/' + cert_id,
  121. body=json.dumps(cert_create_request.__dict__),
  122. params={'certData': ''},
  123. config=config)