ClientStream.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * Copyright 2016 Google LLC
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. namespace Google\ApiCore;
  33. use Google\Rpc\Code;
  34. use Grpc\ClientStreamingCall;
  35. /**
  36. * ClientStream is the response object from a gRPC client streaming API call.
  37. */
  38. class ClientStream
  39. {
  40. private $call;
  41. /**
  42. * ClientStream constructor.
  43. *
  44. * @param ClientStreamingCall $clientStreamingCall The gRPC client streaming call object
  45. * @param array $streamingDescriptor
  46. */
  47. public function __construct( // @phpstan-ignore-line
  48. ClientStreamingCall $clientStreamingCall,
  49. array $streamingDescriptor = []
  50. ) {
  51. $this->call = $clientStreamingCall;
  52. }
  53. /**
  54. * Write request to the server.
  55. *
  56. * @param mixed $request The request to write
  57. */
  58. public function write($request)
  59. {
  60. $this->call->write($request);
  61. }
  62. /**
  63. * Read the response from the server, completing the streaming call.
  64. *
  65. * @throws ApiException
  66. * @return mixed The response object from the server
  67. */
  68. public function readResponse()
  69. {
  70. list($response, $status) = $this->call->wait();
  71. if ($status->code == Code::OK) {
  72. return $response;
  73. } else {
  74. throw ApiException::createFromStdClass($status);
  75. }
  76. }
  77. /**
  78. * Write all data in $dataArray and read the response from the server, completing the streaming
  79. * call.
  80. *
  81. * @param mixed[] $requests An iterator of request objects to write to the server
  82. * @return mixed The response object from the server
  83. */
  84. public function writeAllAndReadResponse(array $requests)
  85. {
  86. foreach ($requests as $request) {
  87. $this->write($request);
  88. }
  89. return $this->readResponse();
  90. }
  91. /**
  92. * Return the underlying gRPC call object
  93. *
  94. * @return \Grpc\ClientStreamingCall|mixed
  95. */
  96. public function getClientStreamingCall()
  97. {
  98. return $this->call;
  99. }
  100. }