Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/Google/Ads/GoogleAds/Lib/V20/AdsAssistantHeaderMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Ads\GoogleAds\Lib\V20;

use Google\ApiCore\Call;
use Google\ApiCore\AgentHeader;
use Google\ApiCore\Middleware\MiddlewareInterface;

/**
* Appends ads assistant metadata to the x-goog-api-client header.
*/
class AdsAssistantHeaderMiddleware implements MiddlewareInterface
{
private $nextHandler;
private $adsAssistant;

public function __construct(callable $nextHandler, string $adsAssistant)
{
$this->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);
}
}
15 changes: 11 additions & 4 deletions src/Google/Ads/GoogleAds/Lib/V20/GoogleAdsGapicClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Comment thread
ajs424 marked this conversation as resolved.

private $developerToken = null;
private $loginCustomerId = null;
private $linkedCustomerId = null;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
}

/**
Expand All @@ -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
Expand Down
52 changes: 52 additions & 0 deletions src/Google/Ads/GoogleAds/Lib/V21/AdsAssistantHeaderMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Ads\GoogleAds\Lib\V21;

use Google\ApiCore\Call;
use Google\ApiCore\AgentHeader;
use Google\ApiCore\Middleware\MiddlewareInterface;

/**
* Appends ads assistant metadata to the x-goog-api-client header.
*/
class AdsAssistantHeaderMiddleware implements MiddlewareInterface
{
private $nextHandler;
private $adsAssistant;

public function __construct(callable $nextHandler, string $adsAssistant)
{
$this->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);
}
}
13 changes: 10 additions & 3 deletions src/Google/Ads/GoogleAds/Lib/V21/GoogleAdsGapicClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
}

/**
Expand All @@ -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
Expand Down
52 changes: 52 additions & 0 deletions src/Google/Ads/GoogleAds/Lib/V22/AdsAssistantHeaderMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Ads\GoogleAds\Lib\V22;

use Google\ApiCore\Call;
use Google\ApiCore\AgentHeader;
use Google\ApiCore\Middleware\MiddlewareInterface;

/**
* Appends ads assistant metadata to the x-goog-api-client header.
*/
class AdsAssistantHeaderMiddleware implements MiddlewareInterface
{
private $nextHandler;
private $adsAssistant;

public function __construct(callable $nextHandler, string $adsAssistant)
{
$this->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);
}
}
13 changes: 10 additions & 3 deletions src/Google/Ads/GoogleAds/Lib/V22/GoogleAdsGapicClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
}

/**
Expand All @@ -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
Expand Down
52 changes: 52 additions & 0 deletions src/Google/Ads/GoogleAds/Lib/V23/AdsAssistantHeaderMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Ads\GoogleAds\Lib\V23;

use Google\ApiCore\Call;
use Google\ApiCore\AgentHeader;
use Google\ApiCore\Middleware\MiddlewareInterface;

/**
* Appends ads assistant metadata to the x-goog-api-client header.
*/
class AdsAssistantHeaderMiddleware implements MiddlewareInterface
{
private $nextHandler;
private $adsAssistant;

public function __construct(callable $nextHandler, string $adsAssistant)
{
$this->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);
}
}
14 changes: 11 additions & 3 deletions src/Google/Ads/GoogleAds/Lib/V23/GoogleAdsGapicClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand All @@ -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);
}
}

/**
Expand All @@ -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
Expand Down
Loading