response.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright 2014-present MongoDB, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Represent a response from the server."""
  15. class Response(object):
  16. __slots__ = ('_data', '_address', '_request_id', '_duration',
  17. '_from_command', '_docs')
  18. def __init__(self, data, address, request_id, duration, from_command,
  19. docs):
  20. """Represent a response from the server.
  21. :Parameters:
  22. - `data`: A network response message.
  23. - `address`: (host, port) of the source server.
  24. - `request_id`: The request id of this operation.
  25. - `duration`: The duration of the operation.
  26. - `from_command`: if the response is the result of a db command.
  27. """
  28. self._data = data
  29. self._address = address
  30. self._request_id = request_id
  31. self._duration = duration
  32. self._from_command = from_command
  33. self._docs = docs
  34. @property
  35. def data(self):
  36. """Server response's raw BSON bytes."""
  37. return self._data
  38. @property
  39. def address(self):
  40. """(host, port) of the source server."""
  41. return self._address
  42. @property
  43. def request_id(self):
  44. """The request id of this operation."""
  45. return self._request_id
  46. @property
  47. def duration(self):
  48. """The duration of the operation."""
  49. return self._duration
  50. @property
  51. def from_command(self):
  52. """If the response is a result from a db command."""
  53. return self._from_command
  54. @property
  55. def docs(self):
  56. """The decoded document(s)."""
  57. return self._docs
  58. class PinnedResponse(Response):
  59. __slots__ = ('_socket_info', '_more_to_come')
  60. def __init__(self, data, address, socket_info, request_id, duration,
  61. from_command, docs, more_to_come):
  62. """Represent a response to an exhaust cursor's initial query.
  63. :Parameters:
  64. - `data`: A network response message.
  65. - `address`: (host, port) of the source server.
  66. - `socket_info`: The SocketInfo used for the initial query.
  67. - `pool`: The Pool from which the SocketInfo came.
  68. - `request_id`: The request id of this operation.
  69. - `duration`: The duration of the operation.
  70. - `from_command`: If the response is the result of a db command.
  71. - `docs`: List of documents.
  72. - `more_to_come`: Bool indicating whether cursor is ready to be
  73. exhausted.
  74. """
  75. super(PinnedResponse, self).__init__(data,
  76. address,
  77. request_id,
  78. duration,
  79. from_command, docs)
  80. self._socket_info = socket_info
  81. self._more_to_come = more_to_come
  82. @property
  83. def socket_info(self):
  84. """The SocketInfo used for the initial query.
  85. The server will send batches on this socket, without waiting for
  86. getMores from the client, until the result set is exhausted or there
  87. is an error.
  88. """
  89. return self._socket_info
  90. @property
  91. def more_to_come(self):
  92. """If true, server is ready to send batches on the socket until the
  93. result set is exhausted or there is an error."""
  94. return self._more_to_come