← All changes
|
includes/sdk/google/google/gax/src/ClientStream.php
+32
-4
1.0.1
→
1.4.1
View file →
| @@ -29,31 +29,40 @@ | ||
| 29 | 29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | 31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | 32 | */ |
| 33 | -namespace Dudlewebs\WPMCS\Google\ApiCore; | |
| 33 | +namespace Dudlewebs\WPMCS\GCP\Google\ApiCore; | |
| 34 | 34 | |
| 35 | -use Dudlewebs\WPMCS\Google\Rpc\Code; | |
| 36 | -use Dudlewebs\WPMCS\Grpc\ClientStreamingCall; | |
| 35 | +use Dudlewebs\WPMCS\GCP\Google\Auth\Logging\LoggingTrait; | |
| 36 | +use Dudlewebs\WPMCS\GCP\Google\Auth\Logging\RpcLogEvent; | |
| 37 | +use Dudlewebs\WPMCS\GCP\Google\Protobuf\Internal\Message; | |
| 38 | +use Dudlewebs\WPMCS\GCP\Google\Rpc\Code; | |
| 39 | +use Dudlewebs\WPMCS\GCP\Grpc\ClientStreamingCall; | |
| 40 | +use Dudlewebs\WPMCS\GCP\Psr\Log\LoggerInterface; | |
| 37 | 41 | /** |
| 38 | 42 | * ClientStream is the response object from a gRPC client streaming API call. |
| 39 | 43 | */ |
| 40 | 44 | class ClientStream |
| 41 | 45 | { |
| 46 | + use LoggingTrait; | |
| 42 | 47 | private $call; |
| 48 | + private null|LoggerInterface $logger; | |
| 43 | 49 | /** |
| 44 | 50 | * ClientStream constructor. |
| 45 | 51 | * |
| 46 | 52 | * @param ClientStreamingCall $clientStreamingCall The gRPC client streaming call object |
| 47 | 53 | * @param array $streamingDescriptor |
| 54 | + * @param null|LoggerInterface $logger A PSR-3 compliant logger. | |
| 48 | 55 | */ |
| 49 | 56 | public function __construct( |
| 50 | 57 | // @phpstan-ignore-line |
| 51 | 58 | ClientStreamingCall $clientStreamingCall, |
| 52 | - array $streamingDescriptor = [] | |
| 59 | + array $streamingDescriptor = [], | |
| 60 | + null|LoggerInterface $logger = null | |
| 53 | 61 | ) |
| 54 | 62 | { |
| 55 | 63 | $this->call = $clientStreamingCall; |
| 64 | + $this->logger = $logger; | |
| 56 | 65 | } |
| 57 | 66 | /** |
| 58 | 67 | * Write request to the server. |
| 59 | 68 | * |
| @@ -60,8 +69,16 @@ | ||
| 60 | 69 | * @param mixed $request The request to write |
| 61 | 70 | */ |
| 62 | 71 | public function write($request) |
| 63 | 72 | { |
| 73 | + // In some cases, $request can be a string | |
| 74 | + if ($this->logger && $request instanceof Message) { | |
| 75 | + $requestEvent = new RpcLogEvent(); | |
| 76 | + $requestEvent->payload = $request->serializeToJsonString(); | |
| 77 | + $requestEvent->processId = (int) \getmypid(); | |
| 78 | + $requestEvent->requestId = \crc32((string) \spl_object_id($this) . \getmypid()); | |
| 79 | + $this->logRequest($requestEvent); | |
| 80 | + } | |
| 64 | 81 | $this->call->write($request); |
| 65 | 82 | } |
| 66 | 83 | /** |
| 67 | 84 | * Read the response from the server, completing the streaming call. |
| @@ -72,8 +89,19 @@ | ||
| 72 | 89 | public function readResponse() |
| 73 | 90 | { |
| 74 | 91 | list($response, $status) = $this->call->wait(); |
| 75 | 92 | if ($status->code == Code::OK) { |
| 93 | + if ($this->logger) { | |
| 94 | + $responseEvent = new RpcLogEvent(); | |
| 95 | + $responseEvent->headers = $status->metadata; | |
| 96 | + $responseEvent->status = $status->code; | |
| 97 | + $responseEvent->processId = (int) \getmypid(); | |
| 98 | + $responseEvent->requestId = \crc32((string) \spl_object_id($this) . \getmypid()); | |
| 99 | + if ($response instanceof Message) { | |
| 100 | + $response->serializeToJsonString(); | |
| 101 | + } | |
| 102 | + $this->logResponse($responseEvent); | |
| 103 | + } | |
| 76 | 104 | return $response; |
| 77 | 105 | } else { |
| 78 | 106 | throw ApiException::createFromStdClass($status); |
| 79 | 107 | } |