4343use Google \ApiCore \Transport \GrpcTransport ;
4444use Google \ApiCore \Transport \RestTransport ;
4545use Google \ApiCore \Transport \TransportInterface ;
46+ use Google \ApiCore \Options \CallOptions ;
47+ use Google \ApiCore \Options \ClientOptions ;
48+ use Google \ApiCore \Options \TransportOptions ;
4649use Google \Auth \CredentialsLoader ;
4750use Google \Auth \FetchAuthTokenInterface ;
4851use Google \LongRunning \Operation ;
@@ -363,45 +366,50 @@ private function setClientOptions(array $options)
363366 'libName ' ,
364367 'libVersion ' ,
365368 ]);
366-
367- $ clientConfig = $ options ['clientConfig ' ];
368- if (is_string ($ clientConfig )) {
369- $ clientConfig = json_decode (file_get_contents ($ clientConfig ), true );
369+ if ($ this ->isNewClientSurface ()) {
370+ // cast to ClientOptions for new surfaces only
371+ $ options = new ClientOptions ($ options );
372+ } elseif (is_string ($ options ['clientConfig ' ])) {
373+ // perform validation for V1 surfaces which is done in the
374+ // ClientOptions class for v2 surfaces.
375+ $ options ['clientConfig ' ] = json_decode (
376+ file_get_contents ($ options ['clientConfig ' ]),
377+ true
378+ );
379+ self ::validateFileExists ($ options ['descriptorsConfigPath ' ]);
370380 }
371381 $ this ->serviceName = $ options ['serviceName ' ];
372382 $ this ->retrySettings = RetrySettings::load (
373383 $ this ->serviceName ,
374- $ clientConfig ,
384+ $ options [ ' clientConfig ' ] ,
375385 $ options ['disableRetries ' ]
376386 );
377387
388+ $ headerInfo = [
389+ 'libName ' => $ options ['libName ' ],
390+ 'libVersion ' => $ options ['libVersion ' ],
391+ 'gapicVersion ' => $ options ['gapicVersion ' ],
392+ ];
378393 // Edge case: If the client has the gRPC extension installed, but is
379394 // a REST-only library, then the grpcVersion header should not be set.
380395 if ($ this ->transport instanceof GrpcTransport) {
381- $ options ['grpcVersion ' ] = phpversion ('grpc ' );
382- unset($ options ['restVersion ' ]);
396+ $ headerInfo ['grpcVersion ' ] = phpversion ('grpc ' );
383397 } elseif ($ this ->transport instanceof RestTransport
384398 || $ this ->transport instanceof GrpcFallbackTransport) {
385- unset($ options ['grpcVersion ' ]);
386- $ options ['restVersion ' ] = Version::getApiCoreVersion ();
399+ $ headerInfo ['restVersion ' ] = Version::getApiCoreVersion ();
387400 }
388-
401+ $ this ->agentHeader = AgentHeader::buildAgentHeader ($ headerInfo );
402+
389403 // Set "client_library_name" depending on client library surface being used
390404 $ userAgentHeader = sprintf (
391405 'gcloud-php-%s/%s ' ,
392406 $ this ->isNewClientSurface () ? 'new ' : 'legacy ' ,
393407 $ options ['gapicVersion ' ]
394408 );
395- $ this ->agentHeader = AgentHeader::buildAgentHeader (
396- $ this ->pluckArray ([
397- 'libName ' ,
398- 'libVersion ' ,
399- 'gapicVersion '
400- ], $ options )
401- );
402409 $ this ->agentHeader ['User-Agent ' ] = [$ userAgentHeader ];
403410
404411 self ::validateFileExists ($ options ['descriptorsConfigPath ' ]);
412+
405413 $ descriptors = require ($ options ['descriptorsConfigPath ' ]);
406414 $ this ->descriptors = $ descriptors ['interfaces ' ][$ this ->serviceName ];
407415
@@ -449,15 +457,15 @@ private function createCredentialsWrapper($credentials, array $credentialsConfig
449457 /**
450458 * @param string $apiEndpoint
451459 * @param string $transport
452- * @param array $transportConfig
460+ * @param TransportOptions| array $transportConfig
453461 * @param callable $clientCertSource
454462 * @return TransportInterface
455463 * @throws ValidationException
456464 */
457465 private function createTransport (
458466 string $ apiEndpoint ,
459467 $ transport ,
460- array $ transportConfig ,
468+ $ transportConfig ,
461469 callable $ clientCertSource = null
462470 ) {
463471 if (!is_string ($ transport )) {
@@ -475,7 +483,12 @@ private function createTransport(
475483 ));
476484 }
477485 $ configForSpecifiedTransport = $ transportConfig [$ transport ] ?? [];
478- $ configForSpecifiedTransport ['clientCertSource ' ] = $ clientCertSource ;
486+ if (is_array ($ configForSpecifiedTransport )) {
487+ $ configForSpecifiedTransport ['clientCertSource ' ] = $ clientCertSource ;
488+ } else {
489+ $ configForSpecifiedTransport ->setClientCertSource ($ clientCertSource );
490+ $ configForSpecifiedTransport = $ configForSpecifiedTransport ->toArray ();
491+ }
479492 switch ($ transport ) {
480493 case 'grpc ' :
481494 // Setting the user agent for gRPC requires special handling
@@ -715,6 +728,7 @@ private function startCall(
715728 int $ callType = Call::UNARY_CALL ,
716729 string $ interfaceName = null
717730 ) {
731+ $ optionalArgs = $ this ->configureCallOptions ($ optionalArgs );
718732 $ callStack = $ this ->createCallStack (
719733 $ this ->configureCallConstructionOptions ($ methodName , $ optionalArgs )
720734 );
@@ -811,6 +825,19 @@ private function configureCallConstructionOptions(string $methodName, array $opt
811825 ];
812826 }
813827
828+ /**
829+ * @return array
830+ */
831+ private function configureCallOptions (array $ optionalArgs ): array
832+ {
833+ if ($ this ->isNewClientSurface ()) {
834+ // cast to CallOptions for new surfaces only
835+ return (new CallOptions ($ optionalArgs ))->toArray ();
836+ }
837+
838+ return $ optionalArgs ;
839+ }
840+
814841 /**
815842 * @param string $methodName
816843 * @param array $optionalArgs {
@@ -836,6 +863,7 @@ private function startOperationsCall(
836863 string $ interfaceName = null ,
837864 string $ operationClass = null
838865 ) {
866+ $ optionalArgs = $ this ->configureCallOptions ($ optionalArgs );
839867 $ callStack = $ this ->createCallStack (
840868 $ this ->configureCallConstructionOptions ($ methodName , $ optionalArgs )
841869 );
@@ -915,6 +943,7 @@ private function getPagedListResponseAsync(
915943 Message $ request ,
916944 string $ interfaceName = null
917945 ) {
946+ $ optionalArgs = $ this ->configureCallOptions ($ optionalArgs );
918947 $ callStack = $ this ->createCallStack (
919948 $ this ->configureCallConstructionOptions ($ methodName , $ optionalArgs )
920949 );
0 commit comments