diff --git a/src/Google/Ads/GoogleAds/Lib/V20/AdsAssistantHeaderMiddleware.php b/src/Google/Ads/GoogleAds/Lib/V20/AdsAssistantHeaderMiddleware.php new file mode 100644 index 0000000000..5b3b426003 --- /dev/null +++ b/src/Google/Ads/GoogleAds/Lib/V20/AdsAssistantHeaderMiddleware.php @@ -0,0 +1,52 @@ +nextHandler = $nextHandler; + $this->adsAssistant = $adsAssistant; + } + + public function __invoke(Call $call, array $options) + { + // Check if the agent header exists to avoid errors + if (isset($options['headers'][AgentHeader::AGENT_HEADER_KEY][0])) { + $options['headers'][AgentHeader::AGENT_HEADER_KEY][0] .= sprintf( + ' gaada/%s', + $this->adsAssistant + ); + } + + $next = $this->nextHandler; + return $next($call, $options); + } +} \ No newline at end of file diff --git a/src/Google/Ads/GoogleAds/Lib/V20/GoogleAdsGapicClientTrait.php b/src/Google/Ads/GoogleAds/Lib/V20/GoogleAdsGapicClientTrait.php index 54b21e5a6f..f1d2ea0a08 100644 --- a/src/Google/Ads/GoogleAds/Lib/V20/GoogleAdsGapicClientTrait.php +++ b/src/Google/Ads/GoogleAds/Lib/V20/GoogleAdsGapicClientTrait.php @@ -38,7 +38,7 @@ trait GoogleAdsGapicClientTrait private static $UNARY_MIDDLEWARES = 'unary-middlewares'; private static $STREAMING_MIDDLEWARES = 'streaming-middlewares'; private static $ADS_ASSISTANT_HEADER_NAME = 'google-ads-api-assistant'; - + private $developerToken = null; private $loginCustomerId = null; private $linkedCustomerId = null; @@ -79,9 +79,6 @@ protected function modifyClientOptions(array &$options) /** * Adds a FixedHeaderMiddleware to a callable. - * - * @param callable $callable the callable to add to - * @return callable the modified callable */ private function addFixedHeaderMiddleware(callable &$callable) { @@ -115,6 +112,11 @@ protected function modifyUnaryCallable(callable &$callable) /** @var GoogleAdsMiddlewareAbstract $unaryMiddleware */ $callable = $unaryMiddleware->withNextHandler($callable); } + + // Prepend the AdsAssistantHeaderMiddleware + if (!is_null($this->adsAssistant)) { + $callable = new AdsAssistantHeaderMiddleware($callable, $this->adsAssistant); + } } /** @@ -129,6 +131,11 @@ protected function modifyStreamingCallable(callable &$callable) /** @var GoogleAdsMiddlewareAbstract $streamingMiddleware */ $callable = $streamingMiddleware->withNextHandler($callable); } + + // Prepend the AdsAssistantHeaderMiddleware + if (!is_null($this->adsAssistant)) { + $callable = new AdsAssistantHeaderMiddleware($callable, $this->adsAssistant); + } } public function getResponseMetadata(): ?GoogleAdsResponseMetadata diff --git a/src/Google/Ads/GoogleAds/Lib/V21/AdsAssistantHeaderMiddleware.php b/src/Google/Ads/GoogleAds/Lib/V21/AdsAssistantHeaderMiddleware.php new file mode 100644 index 0000000000..3a00c685d4 --- /dev/null +++ b/src/Google/Ads/GoogleAds/Lib/V21/AdsAssistantHeaderMiddleware.php @@ -0,0 +1,52 @@ +nextHandler = $nextHandler; + $this->adsAssistant = $adsAssistant; + } + + public function __invoke(Call $call, array $options) + { + // Check if the agent header exists to avoid errors + if (isset($options['headers'][AgentHeader::AGENT_HEADER_KEY][0])) { + $options['headers'][AgentHeader::AGENT_HEADER_KEY][0] .= sprintf( + ' gaada/%s', + $this->adsAssistant + ); + } + + $next = $this->nextHandler; + return $next($call, $options); + } +} \ No newline at end of file diff --git a/src/Google/Ads/GoogleAds/Lib/V21/GoogleAdsGapicClientTrait.php b/src/Google/Ads/GoogleAds/Lib/V21/GoogleAdsGapicClientTrait.php index 6252ac90a7..53e27faf17 100644 --- a/src/Google/Ads/GoogleAds/Lib/V21/GoogleAdsGapicClientTrait.php +++ b/src/Google/Ads/GoogleAds/Lib/V21/GoogleAdsGapicClientTrait.php @@ -79,9 +79,6 @@ protected function modifyClientOptions(array &$options) /** * Adds a FixedHeaderMiddleware to a callable. - * - * @param callable $callable the callable to add to - * @return callable the modified callable */ private function addFixedHeaderMiddleware(callable &$callable) { @@ -115,6 +112,11 @@ protected function modifyUnaryCallable(callable &$callable) /** @var GoogleAdsMiddlewareAbstract $unaryMiddleware */ $callable = $unaryMiddleware->withNextHandler($callable); } + + // Prepend the AdsAssistantHeaderMiddleware + if (!is_null($this->adsAssistant)) { + $callable = new AdsAssistantHeaderMiddleware($callable, $this->adsAssistant); + } } /** @@ -129,6 +131,11 @@ protected function modifyStreamingCallable(callable &$callable) /** @var GoogleAdsMiddlewareAbstract $streamingMiddleware */ $callable = $streamingMiddleware->withNextHandler($callable); } + + // Prepend the AdsAssistantHeaderMiddleware + if (!is_null($this->adsAssistant)) { + $callable = new AdsAssistantHeaderMiddleware($callable, $this->adsAssistant); + } } public function getResponseMetadata(): ?GoogleAdsResponseMetadata diff --git a/src/Google/Ads/GoogleAds/Lib/V22/AdsAssistantHeaderMiddleware.php b/src/Google/Ads/GoogleAds/Lib/V22/AdsAssistantHeaderMiddleware.php new file mode 100644 index 0000000000..ff9f3bdc7b --- /dev/null +++ b/src/Google/Ads/GoogleAds/Lib/V22/AdsAssistantHeaderMiddleware.php @@ -0,0 +1,52 @@ +nextHandler = $nextHandler; + $this->adsAssistant = $adsAssistant; + } + + public function __invoke(Call $call, array $options) + { + // Check if the agent header exists to avoid errors + if (isset($options['headers'][AgentHeader::AGENT_HEADER_KEY][0])) { + $options['headers'][AgentHeader::AGENT_HEADER_KEY][0] .= sprintf( + ' gaada/%s', + $this->adsAssistant + ); + } + + $next = $this->nextHandler; + return $next($call, $options); + } +} \ No newline at end of file diff --git a/src/Google/Ads/GoogleAds/Lib/V22/GoogleAdsGapicClientTrait.php b/src/Google/Ads/GoogleAds/Lib/V22/GoogleAdsGapicClientTrait.php index a32f4dce01..439f5e20c6 100644 --- a/src/Google/Ads/GoogleAds/Lib/V22/GoogleAdsGapicClientTrait.php +++ b/src/Google/Ads/GoogleAds/Lib/V22/GoogleAdsGapicClientTrait.php @@ -82,9 +82,6 @@ protected function modifyClientOptions(array &$options) /** * Adds a FixedHeaderMiddleware to a callable. - * - * @param callable $callable the callable to add to - * @return callable the modified callable */ private function addFixedHeaderMiddleware(callable &$callable) { @@ -121,6 +118,11 @@ protected function modifyUnaryCallable(callable &$callable) /** @var GoogleAdsMiddlewareAbstract $unaryMiddleware */ $callable = $unaryMiddleware->withNextHandler($callable); } + + // Prepend the AdsAssistantHeaderMiddleware + if (!is_null($this->adsAssistant)) { + $callable = new AdsAssistantHeaderMiddleware($callable, $this->adsAssistant); + } } /** @@ -135,6 +137,11 @@ protected function modifyStreamingCallable(callable &$callable) /** @var GoogleAdsMiddlewareAbstract $streamingMiddleware */ $callable = $streamingMiddleware->withNextHandler($callable); } + + // Prepend the AdsAssistantHeaderMiddleware + if (!is_null($this->adsAssistant)) { + $callable = new AdsAssistantHeaderMiddleware($callable, $this->adsAssistant); + } } public function getResponseMetadata(): ?GoogleAdsResponseMetadata diff --git a/src/Google/Ads/GoogleAds/Lib/V23/AdsAssistantHeaderMiddleware.php b/src/Google/Ads/GoogleAds/Lib/V23/AdsAssistantHeaderMiddleware.php new file mode 100644 index 0000000000..e16b4affac --- /dev/null +++ b/src/Google/Ads/GoogleAds/Lib/V23/AdsAssistantHeaderMiddleware.php @@ -0,0 +1,52 @@ +nextHandler = $nextHandler; + $this->adsAssistant = $adsAssistant; + } + + public function __invoke(Call $call, array $options) + { + // Check if the agent header exists to avoid errors + if (isset($options['headers'][AgentHeader::AGENT_HEADER_KEY][0])) { + $options['headers'][AgentHeader::AGENT_HEADER_KEY][0] .= sprintf( + ' gaada/%s', + $this->adsAssistant + ); + } + + $next = $this->nextHandler; + return $next($call, $options); + } +} \ No newline at end of file diff --git a/src/Google/Ads/GoogleAds/Lib/V23/GoogleAdsGapicClientTrait.php b/src/Google/Ads/GoogleAds/Lib/V23/GoogleAdsGapicClientTrait.php index 666cc71511..cdfbcd11eb 100644 --- a/src/Google/Ads/GoogleAds/Lib/V23/GoogleAdsGapicClientTrait.php +++ b/src/Google/Ads/GoogleAds/Lib/V23/GoogleAdsGapicClientTrait.php @@ -82,9 +82,6 @@ protected function modifyClientOptions(array &$options) /** * Adds a FixedHeaderMiddleware to a callable. - * - * @param callable $callable the callable to add to - * @return callable the modified callable */ private function addFixedHeaderMiddleware(callable &$callable) { @@ -103,6 +100,7 @@ private function addFixedHeaderMiddleware(callable &$callable) if (!is_null($this->adsAssistant)) { $headers[self::$ADS_ASSISTANT_HEADER_NAME] = [$this->adsAssistant]; } + $callable = new FixedHeaderMiddleware($callable, $headers); } return $callable; @@ -120,6 +118,11 @@ protected function modifyUnaryCallable(callable &$callable) /** @var GoogleAdsMiddlewareAbstract $unaryMiddleware */ $callable = $unaryMiddleware->withNextHandler($callable); } + + // Prepend the AdsAssistantHeaderMiddleware + if (!is_null($this->adsAssistant)) { + $callable = new AdsAssistantHeaderMiddleware($callable, $this->adsAssistant); + } } /** @@ -134,6 +137,11 @@ protected function modifyStreamingCallable(callable &$callable) /** @var GoogleAdsMiddlewareAbstract $streamingMiddleware */ $callable = $streamingMiddleware->withNextHandler($callable); } + + // Prepend the AdsAssistantHeaderMiddleware + if (!is_null($this->adsAssistant)) { + $callable = new AdsAssistantHeaderMiddleware($callable, $this->adsAssistant); + } } public function getResponseMetadata(): ?GoogleAdsResponseMetadata diff --git a/tests/Google/Ads/GoogleAds/Lib/V20/AdsAssistantHeaderMiddlewareTest.php b/tests/Google/Ads/GoogleAds/Lib/V20/AdsAssistantHeaderMiddlewareTest.php new file mode 100644 index 0000000000..f176d59424 --- /dev/null +++ b/tests/Google/Ads/GoogleAds/Lib/V20/AdsAssistantHeaderMiddlewareTest.php @@ -0,0 +1,48 @@ +createMock(Call::class); + $options = [ + 'headers' => [ + AgentHeader::AGENT_HEADER_KEY => [$initialHeader] + ] + ]; + + // 4. Run it + $result = $middleware($call, $options); + + // 5. Assertions + $this->assertTrue($nextHandlerCalled, 'The next handler was not called.'); + $this->assertEquals("final-result", $result); + + $actualHeader = $capturedOptions['headers'][AgentHeader::AGENT_HEADER_KEY][0]; + $this->assertEquals($expectedHeader, $actualHeader); + $this->assertStringContainsString('gaada/test-assistant-123', $actualHeader); + } +} \ No newline at end of file diff --git a/tests/Google/Ads/GoogleAds/Lib/V21/AdsAssistantHeaderMiddlewareTest.php b/tests/Google/Ads/GoogleAds/Lib/V21/AdsAssistantHeaderMiddlewareTest.php new file mode 100644 index 0000000000..f176d59424 --- /dev/null +++ b/tests/Google/Ads/GoogleAds/Lib/V21/AdsAssistantHeaderMiddlewareTest.php @@ -0,0 +1,48 @@ +createMock(Call::class); + $options = [ + 'headers' => [ + AgentHeader::AGENT_HEADER_KEY => [$initialHeader] + ] + ]; + + // 4. Run it + $result = $middleware($call, $options); + + // 5. Assertions + $this->assertTrue($nextHandlerCalled, 'The next handler was not called.'); + $this->assertEquals("final-result", $result); + + $actualHeader = $capturedOptions['headers'][AgentHeader::AGENT_HEADER_KEY][0]; + $this->assertEquals($expectedHeader, $actualHeader); + $this->assertStringContainsString('gaada/test-assistant-123', $actualHeader); + } +} \ No newline at end of file diff --git a/tests/Google/Ads/GoogleAds/Lib/V22/AdsAssistantHeaderMiddlewareTest.php b/tests/Google/Ads/GoogleAds/Lib/V22/AdsAssistantHeaderMiddlewareTest.php new file mode 100644 index 0000000000..f176d59424 --- /dev/null +++ b/tests/Google/Ads/GoogleAds/Lib/V22/AdsAssistantHeaderMiddlewareTest.php @@ -0,0 +1,48 @@ +createMock(Call::class); + $options = [ + 'headers' => [ + AgentHeader::AGENT_HEADER_KEY => [$initialHeader] + ] + ]; + + // 4. Run it + $result = $middleware($call, $options); + + // 5. Assertions + $this->assertTrue($nextHandlerCalled, 'The next handler was not called.'); + $this->assertEquals("final-result", $result); + + $actualHeader = $capturedOptions['headers'][AgentHeader::AGENT_HEADER_KEY][0]; + $this->assertEquals($expectedHeader, $actualHeader); + $this->assertStringContainsString('gaada/test-assistant-123', $actualHeader); + } +} \ No newline at end of file diff --git a/tests/Google/Ads/GoogleAds/Lib/V23/AdsAssistantHeaderMiddlewareTest.php b/tests/Google/Ads/GoogleAds/Lib/V23/AdsAssistantHeaderMiddlewareTest.php new file mode 100644 index 0000000000..f176d59424 --- /dev/null +++ b/tests/Google/Ads/GoogleAds/Lib/V23/AdsAssistantHeaderMiddlewareTest.php @@ -0,0 +1,48 @@ +createMock(Call::class); + $options = [ + 'headers' => [ + AgentHeader::AGENT_HEADER_KEY => [$initialHeader] + ] + ]; + + // 4. Run it + $result = $middleware($call, $options); + + // 5. Assertions + $this->assertTrue($nextHandlerCalled, 'The next handler was not called.'); + $this->assertEquals("final-result", $result); + + $actualHeader = $capturedOptions['headers'][AgentHeader::AGENT_HEADER_KEY][0]; + $this->assertEquals($expectedHeader, $actualHeader); + $this->assertStringContainsString('gaada/test-assistant-123', $actualHeader); + } +} \ No newline at end of file