mvs_client.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # Copyright 2019 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 client class for MVS.
  14. """
  15. # from baidubce.services.vca.vca_client import VcaClient
  16. #
  17. # from baidubce.services.vcr.vcr_client import VcrClient
  18. import copy
  19. import json
  20. import logging
  21. from builtins import str
  22. from builtins import bytes
  23. from baidubce.auth import bce_v1_signer
  24. from baidubce.bce_base_client import BceBaseClient
  25. from baidubce.http import bce_http_client
  26. from baidubce.http import handler
  27. from baidubce.http import http_methods
  28. from baidubce.utils import required
  29. _logger = logging.getLogger(__name__)
  30. class MvsClient(BceBaseClient):
  31. """
  32. mvs client
  33. """
  34. def __init__(self, config=None):
  35. """init"""
  36. BceBaseClient.__init__(self, config)
  37. @required(source=(bytes, str))
  38. def insert_video(self, video_lib, source, notification, description=None, config=None):
  39. """
  40. insert a video.
  41. :param video_lib: video lib
  42. :type video_lib: string
  43. :param source: video source
  44. :type source: string
  45. :param notification: notification name
  46. :type notification: string
  47. :param description: description for this request
  48. :type description: string
  49. :return: **BceResponse**
  50. """
  51. body = {
  52. 'source': source,
  53. 'notification': notification
  54. }
  55. if description is not None:
  56. body['description'] = description
  57. return self._send_request(http_methods.PUT, str.encode('/v2/videolib/' + video_lib),
  58. headers={b'Content-Type': b'application/json'},
  59. body=json.dumps(body),
  60. config=config)
  61. @required(source=(bytes, str))
  62. def search_video_by_video(self, video_lib, source, notification, description=None, config=None):
  63. """
  64. search video by video.
  65. :param video_lib: video lib
  66. :type video_lib: string
  67. :param source: video source
  68. :type source: string
  69. :param notification: notification name
  70. :type notification: string
  71. :param description: description for this request
  72. :type description: string
  73. :return: **BceResponse**
  74. """
  75. body = {
  76. 'source': source,
  77. 'notification': notification
  78. }
  79. if description is not None:
  80. body['description'] = description
  81. return self._send_request(http_methods.POST, str.encode('/v2/videolib/' + video_lib),
  82. headers={b'Content-Type': b'application/json'},
  83. params={b'searchByVideo': b''},
  84. body=json.dumps(body),
  85. config=config)
  86. @required(source=(bytes, str))
  87. def insert_image(self, image_lib, source, description=None, config=None):
  88. """
  89. insert an image.
  90. :param image_lib: image lib
  91. :type image_lib: string
  92. :param source: video source
  93. :type source: string
  94. :param description: description for this request
  95. :type description: string
  96. :return: **BceResponse**
  97. """
  98. body = {
  99. 'source': source
  100. }
  101. if description is not None:
  102. body['description'] = description
  103. return self._send_request(http_methods.PUT, str.encode('/v2/imagelib/' + image_lib),
  104. headers={b'Content-Type': b'application/json'},
  105. body=json.dumps(body),
  106. config=config)
  107. @required(source=(bytes, str))
  108. def search_image_by_image(self, image_lib, source, description=None, config=None):
  109. """
  110. search video by video.
  111. :param video_lib: video lib
  112. :type video_lib: string
  113. :param source: video source
  114. :type source: string
  115. :param description: description for this request
  116. :type description: string
  117. :return: **BceResponse**
  118. """
  119. body = {
  120. 'source': source
  121. }
  122. if description is not None:
  123. body['description'] = description
  124. return self._send_request(http_methods.POST, str.encode('/v2/imagelib/' + image_lib),
  125. headers={b'Content-Type': b'application/json'},
  126. params={b'searchByImage': b''},
  127. body=json.dumps(body),
  128. config=config)
  129. @staticmethod
  130. def _merge_config(self, config):
  131. if config is None:
  132. return self.config
  133. else:
  134. new_config = copy.copy(self.config)
  135. new_config.merge_non_none_values(config)
  136. return new_config
  137. def _send_request(
  138. self, http_method, path,
  139. body=None, headers=None, params=None,
  140. config=None,
  141. body_parser=None):
  142. config = self._merge_config(self, config)
  143. if body_parser is None:
  144. body_parser = handler.parse_json
  145. return bce_http_client.send_request(
  146. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  147. http_method, path, body, headers, params)