setCommonDefaults($config); $config += ['authHttpHandler' => null, 'serializer' => new Serializer(), 'grpcOptions' => []]; $this->authHttpHandler = $config['authHttpHandler'] ?: HttpHandlerFactory::build(); $this->serializer = $config['serializer']; $this->grpcOptions = $config['grpcOptions']; } /** * Deliver the request. * * @param callable $request The request to execute. * @param array $args The arguments for the request. * @param array $options [optional] { * Request options. * * @type float $requestTimeout Seconds to wait before timing out the * request. **Defaults to** `60`. * @type int $retries Number of retries for a failed request. * **Defaults to** `3`. * @type callable $grpcRetryFunction Sets the conditions for whether or * not a request should attempt to retry. Function signature should * match: `function (\Exception $ex) : bool`. * @type array $grpcOptions gRPC specific configuration options. * } * @return array * @throws Exception\ServiceException */ public function send(callable $request, array $args, array $options = []) { $retries = $options['retries'] ?? $this->retries; $retryFunction = $options['grpcRetryFunction'] ?? function (\Exception $ex) { $statusCode = $ex->getCode(); return \in_array($statusCode, $this->grpcRetryCodes); }; $grpcOptions = $options['grpcOptions'] ?? $this->grpcOptions; $timeout = $options['requestTimeout'] ?? $this->requestTimeout; $backoff = new ExponentialBackoff($retries, $retryFunction); if (!isset($grpcOptions['retrySettings'])) { $retrySettings = ['retriesEnabled' => \false]; if ($timeout) { $retrySettings['noRetriesRpcTimeoutMillis'] = $timeout * 1000; } $grpcOptions['retrySettings'] = $retrySettings; } $optionalArgs =& $args[\count($args) - 1]; $optionalArgs += $grpcOptions; try { return $this->handleResponse($backoff->execute($request, $args)); } catch (\Exception $ex) { if ($ex instanceof ApiException) { throw $this->convertToGoogleException($ex); } throw $ex; } } }