Skip to content

Commit 2d11de3

Browse files
authored
Add option to toggle API format version (#1644)
* Add option to toggle API format version * Fix tests * Use phpunit mock * Force trusty in builds * Fix style
1 parent 9b293e9 commit 2d11de3

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
language: php
2+
dist: trusty
23

34
services:
45
- memcached
@@ -44,4 +45,3 @@ before_script:
4445
script:
4546
- vendor/bin/phpunit
4647
- if [[ "$RUN_PHP_CS" == "true" ]]; then vendor/bin/phpcs src --standard=style/ruleset.xml -np; fi
47-

src/Google/Client.php

+23
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ public function __construct(array $config = array())
142142
// Service class used in Google_Client::verifyIdToken.
143143
// Explicitly pass this in to avoid setting JWT::$leeway
144144
'jwt' => null,
145+
146+
// Setting api_format_v2 will return more detailed error messages
147+
// from certain APIs.
148+
'api_format_v2' => false
145149
],
146150
$config
147151
);
@@ -796,6 +800,13 @@ public function execute(RequestInterface $request, $expectedClass = null)
796800
. $this->getLibraryVersion()
797801
);
798802

803+
if ($this->config['api_format_v2']) {
804+
$request = $request->withHeader(
805+
'X-GOOG-API-FORMAT-VERSION',
806+
2
807+
);
808+
}
809+
799810
// call the authorize method
800811
// this is where most of the grunt work is done
801812
$http = $this->authorize();
@@ -1056,6 +1067,18 @@ public function getHttpClient()
10561067
return $this->http;
10571068
}
10581069

1070+
/**
1071+
* Set the API format version.
1072+
*
1073+
* `true` will use V2, which may return more useful error messages.
1074+
*
1075+
* @param bool $value
1076+
*/
1077+
public function setApiFormatV2($value)
1078+
{
1079+
$this->config['api_format_v2'] = (bool) $value;
1080+
}
1081+
10591082
protected function createDefaultHttpClient()
10601083
{
10611084
$options = ['exceptions' => false];

tests/Google/ClientTest.php

+25-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
*/
2020

2121
use GuzzleHttp\Client;
22-
use GuzzleHttp\Event\RequestEvents;
23-
use Psr\Http\Message\Request;
22+
use GuzzleHttp\Psr7\Request;
23+
use GuzzleHttp\Psr7\Response;
24+
use Prophecy\Argument;
25+
use Psr\Http\Message\RequestInterface;
2426

2527
class Google_ClientTest extends BaseTest
2628
{
@@ -684,4 +686,25 @@ public function testTokenCallback()
684686

685687
$this->assertTrue($called);
686688
}
689+
690+
public function testExecuteWithFormat()
691+
{
692+
$this->onlyGuzzle6();
693+
694+
$client = new Google_Client([
695+
'api_format_v2' => true
696+
]);
697+
698+
$guzzle = $this->getMock('GuzzleHttp\Client');
699+
$guzzle->expects($this->once())
700+
->method('send')
701+
->with($this->callback(function (RequestInterface $request) {
702+
return $request->getHeaderLine('X-GOOG-API-FORMAT-VERSION') === '2';
703+
}))->will($this->returnValue(new Response(200, [], null)));
704+
705+
$client->setHttpClient($guzzle);
706+
707+
$request = new Request('POST', 'http://foo.bar/');
708+
$client->execute($request);
709+
}
687710
}

0 commit comments

Comments
 (0)