Skip to content

Commit 45cc7b6

Browse files
committed
Merge pull request #40 from ianbarber/master
Add GZIP support
2 parents 3968bcb + 0b0ffa2 commit 45cc7b6

5 files changed

Lines changed: 23 additions & 17 deletions

File tree

src/Google/Client.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Google_Client
3737
{
3838
const LIBVER = "1.0.1-alpha";
3939
const USER_AGENT_SUFFIX = "google-api-php-client/";
40+
const GZIP_UA = " (gzip)";
4041
/**
4142
* @var Google_Auth_Abstract $auth
4243
*/
@@ -107,16 +108,6 @@ public function getLibraryVersion()
107108
{
108109
return self::LIBVER;
109110
}
110-
111-
/**
112-
* Shim function until templates are updated.
113-
* @todo(ianbarber): remove this.
114-
* @deprecated
115-
*/
116-
public function addService($a, $b, $c)
117-
{
118-
return;
119-
}
120111

121112
/**
122113
* Attempt to exchange a code for an valid authentication token.
@@ -483,10 +474,16 @@ public function setDefer($defer)
483474
public function execute($request)
484475
{
485476
if ($request instanceof Google_Http_Request) {
477+
$userAgentGzipSuffix = "";
478+
if (!$this->getClassConfig("Google_Http_Request", "disable_gzip")) {
479+
$request->setRequestHeaders(array("Accept-Encoding" => "gzip"));
480+
$userAgentGzipSuffix = self::GZIP_UA;
481+
}
486482
$request->setUserAgent(
487483
$this->getApplicationName()
488484
. " " . self::USER_AGENT_SUFFIX
489485
. $this->getLibraryVersion()
486+
. $userAgentGzipSuffix
490487
);
491488
$request->maybeMoveParametersToBody();
492489
return Google_Http_REST::execute($this, $request);

src/Google/Config.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function __construct($ini_file_location = null)
4848
'classes' => array(
4949
// If you want to pass in OAuth 2.0 settings, they will need to be
5050
// structured like this.
51+
'Google_Http_Request' => array(
52+
// Disable the use of gzip on calls if set to true.
53+
'disable_gzip' => false
54+
),
5155
'Google_Auth_OAuth2' => array(
5256
// Keys for OAuth 2.0 access, see the API console at
5357
// https://developers.google.com/console

src/Google/Http/REST.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
class Google_Http_REST
3030
{
3131
/**
32-
* Executes a apiServiceRequest using a RESTful call by transforming it into
33-
* an apiHttpRequest, and executed via apiIO::authenticatedRequest().
32+
* Executes a Google_Http_Request
3433
*
3534
* @param Google_Client $client
3635
* @param Google_Http_Request $req

src/Google/IO/Stream.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
class Google_IO_Stream extends Google_IO_Abstract
2727
{
28-
28+
const ZLIB = "compress.zlib://";
2929
private static $ENTITY_HTTP_METHODS = array("POST" => null, "PUT" => null);
3030

3131
private static $DEFAULT_HTTP_CONTEXT = array(
@@ -74,7 +74,7 @@ public function makeRequest(Google_Http_Request $request)
7474
if ($requestHeaders && is_array($requestHeaders)) {
7575
$headers = "";
7676
foreach ($requestHeaders as $k => $v) {
77-
$headers .= "$k: $v\n";
77+
$headers .= "$k: $v\r\n";
7878
}
7979
$requestHttpContext["header"] = $headers;
8080
}
@@ -101,9 +101,15 @@ public function makeRequest(Google_Http_Request $request)
101101
);
102102

103103
$context = stream_context_create($options);
104+
105+
$url = $request->getUrl();
106+
107+
if (!$this->client->getClassConfig("Google_Http_Request", "disable_gzip")) {
108+
$url = self::ZLIB . $url;
109+
}
104110

105111
$response_data = file_get_contents(
106-
$request->getUrl(),
112+
$url,
107113
false,
108114
$context
109115
);

src/Google/Model.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ protected function mapTypes($array)
8484
property_exists($this, $key)) {
8585
$this->$key = $val;
8686
unset($array[$key]);
87-
// Check if property exists as camelCase, leave it in array as snake_case
88-
// in case of backwards compatibility issues
8987
} elseif (property_exists($this, $camelKey = Google_Utils::camelCase($key))) {
88+
// This checks if property exists as camelCase, leaving it in array as snake_case
89+
// in case of backwards compatibility issues.
9090
$this->$camelKey = $val;
9191
}
9292
}

0 commit comments

Comments
 (0)