dumap_client.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Copyright (c) 2014 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 CDN.
  14. """
  15. import copy
  16. import logging
  17. import uuid
  18. from baidubce import bce_base_client
  19. from baidubce.auth import bce_v1_signer
  20. from baidubce.http import bce_http_client
  21. from baidubce.http import handler
  22. from baidubce.http import http_methods
  23. from baidubce.utils import required
  24. _logger = logging.getLogger(__name__)
  25. class DumapClient(bce_base_client.BceBaseClient):
  26. """
  27. DumapClient
  28. """
  29. def __init__(self, config=None):
  30. bce_base_client.BceBaseClient.__init__(self, config)
  31. @required(app_id=(bytes, str), uri=(bytes, str), params=dict)
  32. def call_open_api(self, app_id=None, uri=None, params=None, body=None, method=b'GET', config=None):
  33. """
  34. call open_api
  35. :param app_id: app_id
  36. :type app_id: string
  37. :param uri: open api uri
  38. :type uri: string
  39. :param params: dict
  40. :type params:request params
  41. :param body: request body (default: None)
  42. :type body: string
  43. :param method: http method (default GET)
  44. :type method: http_methods
  45. :param config: None
  46. :type config: baidubce.BceClientConfiguration
  47. :return:
  48. :rtype: baidubce.bce_response.BceResponse
  49. """
  50. response = self._send_request(
  51. http_method=method,
  52. path=uri,
  53. params=params,
  54. body=body,
  55. headers={b'x-app-id': app_id},
  56. config=config
  57. )
  58. if response.body:
  59. return response.body.decode("utf-8")
  60. else:
  61. return response
  62. @staticmethod
  63. def _merge_config(self, config):
  64. if config is None:
  65. return self.config
  66. else:
  67. new_config = copy.copy(self.config)
  68. new_config.merge_non_none_values(config)
  69. return new_config
  70. def _send_request(
  71. self, http_method, path,
  72. body=None, headers=None, params=None,
  73. config=None):
  74. config = self._merge_config(self, config)
  75. headers[b'x-bce-request-id'] = uuid.uuid4()
  76. headers[b'Content-Type'] = b'application/json;charset=utf-8'
  77. return bce_http_client.send_request(
  78. config, sign_wrapper([b'host', b'x-bce-date', b'x-bce-request-id', b'x-app-id']),
  79. [handler.parse_error, parse_none],
  80. http_method, path, body, headers, params)
  81. def sign_wrapper(headers_to_sign):
  82. """wrapper the bce_v1_signer.sign()."""
  83. def _wrapper(credentials, http_method, path, headers, params):
  84. return bce_v1_signer.sign(credentials, http_method, path, headers, params,
  85. headers_to_sign=headers_to_sign)
  86. return _wrapper
  87. def parse_none(http_response, response):
  88. """If the body is not empty, convert it to a python object and set as the value of
  89. response.body. http_response is always closed if no error occurs.
  90. :param http_response: the http_response object returned by HTTPConnection.getresponse()
  91. :type http_response: httplib.HTTPResponse
  92. :param response: general response object which will be returned to the caller
  93. :type response: baidubce.BceResponse
  94. :return: always true
  95. :rtype bool
  96. """
  97. body = http_response.read()
  98. if body:
  99. response.body = body
  100. http_response.close()
  101. return True