private function getResumeUri() { $result = null; $body = $this->request->getPostBody(); if ($body) { $headers = array('content-type' => 'application/json; charset=UTF-8', 'content-length' => Postman_Google_Utils::getStrLen($body), 'x-upload-content-type' => $this->mimeType, 'x-upload-content-length' => $this->size, 'expect' => ''); $this->request->setRequestHeaders($headers); } $response = $this->client->getIo()->makeRequest($this->request); $location = $response->getResponseHeader('location'); $code = $response->getResponseHttpCode(); if (200 == $code && true == $location) { return $location; } $message = $code; $body = @json_decode($response->getResponseBody()); if (!empty($body->error->errors)) { $message .= ': '; foreach ($body->error->errors as $error) { $message .= "{$error->domain}, {$error->message};"; } $message = rtrim($message, ';'); } $error = "Failed to start the resumable upload (HTTP {$message})"; $this->client->getLogger()->error($error); throw new Postman_Google_Exception($error); }
public function testIsResponseCacheable() { $client = $this->getClient(); $resp = new Postman_Google_Http_Request('http://localhost', 'POST'); $result = Postman_Google_Http_CacheParser::isResponseCacheable($resp); $this->assertFalse($result); // The response has expired, and we don't have an etag for // revalidation. $resp = new Postman_Google_Http_Request('http://localhost', 'GET'); $resp->setResponseHttpCode('200'); $resp->setResponseHeaders(array('Cache-Control' => 'max-age=3600, must-revalidate', 'Expires' => 'Fri, 30 Oct 1998 14:19:41 GMT', 'Date' => 'Mon, 29 Jun 1998 02:28:12 GMT', 'Last-Modified' => 'Mon, 29 Jun 1998 02:28:12 GMT')); $result = Postman_Google_Http_CacheParser::isResponseCacheable($resp); $this->assertFalse($result); // Verify cacheable responses. $resp = new Postman_Google_Http_Request('http://localhost', 'GET'); $resp->setResponseHttpCode('200'); $resp->setResponseHeaders(array('Cache-Control' => 'max-age=3600, must-revalidate', 'Expires' => 'Fri, 30 Oct 2013 14:19:41 GMT', 'Date' => 'Mon, 29 Jun 2011 02:28:12 GMT', 'Last-Modified' => 'Mon, 29 Jun 2011 02:28:12 GMT', 'ETag' => '3e86-410-3596fbbc')); $result = Postman_Google_Http_CacheParser::isResponseCacheable($resp); $this->assertTrue($result); // Verify that responses to HEAD requests are cacheable. $resp = new Postman_Google_Http_Request('http://localhost', 'HEAD'); $resp->setResponseHttpCode('200'); $resp->setResponseBody(null); $resp->setResponseHeaders(array('Cache-Control' => 'max-age=3600, must-revalidate', 'Expires' => 'Fri, 30 Oct 2013 14:19:41 GMT', 'Date' => 'Mon, 29 Jun 2011 02:28:12 GMT', 'Last-Modified' => 'Mon, 29 Jun 2011 02:28:12 GMT', 'ETag' => '3e86-410-3596fbbc')); $result = Postman_Google_Http_CacheParser::isResponseCacheable($resp); $this->assertTrue($result); // Verify that Vary: * cannot get cached. $resp = new Postman_Google_Http_Request('http://localhost', 'GET'); $resp->setResponseHttpCode('200'); $resp->setResponseHeaders(array('Cache-Control' => 'max-age=3600, must-revalidate', 'Expires' => 'Fri, 30 Oct 2013 14:19:41 GMT', 'Date' => 'Mon, 29 Jun 2011 02:28:12 GMT', 'Last-Modified' => 'Mon, 29 Jun 2011 02:28:12 GMT', 'Vary' => 'foo', 'ETag' => '3e86-410-3596fbbc')); $result = Postman_Google_Http_CacheParser::isResponseCacheable($resp); $this->assertFalse($result); // Verify 201s cannot get cached. $resp = new Postman_Google_Http_Request('http://localhost', 'GET'); $resp->setResponseHttpCode('201'); $resp->setResponseBody(null); $resp->setResponseHeaders(array('Cache-Control' => 'max-age=3600, must-revalidate', 'Expires' => 'Fri, 30 Oct 2013 14:19:41 GMT', 'Last-Modified' => 'Mon, 29 Jun 2011 02:28:12 GMT', 'ETag' => '3e86-410-3596fbbc')); $result = Postman_Google_Http_CacheParser::isResponseCacheable($resp); $this->assertFalse($result); // Verify pragma: no-cache. $resp = new Postman_Google_Http_Request('http://localhost', 'GET'); $resp->setResponseHttpCode('200'); $resp->setResponseHeaders(array('Expires' => 'Wed, 11 Jan 2012 04:03:37 GMT', 'Date' => 'Wed, 11 Jan 2012 04:03:37 GMT', 'Pragma' => 'no-cache', 'Cache-Control' => 'private, max-age=0, must-revalidate, no-transform', 'ETag' => '3e86-410-3596fbbc')); $result = Postman_Google_Http_CacheParser::isResponseCacheable($resp); $this->assertFalse($result); // Verify Cache-Control: no-store. $resp = new Postman_Google_Http_Request('http://localhost', 'GET'); $resp->setResponseHttpCode('200'); $resp->setResponseHeaders(array('Expires' => 'Wed, 11 Jan 2012 04:03:37 GMT', 'Date' => 'Wed, 11 Jan 2012 04:03:37 GMT', 'Cache-Control' => 'no-store', 'ETag' => '3e86-410-3596fbbc')); $result = Postman_Google_Http_CacheParser::isResponseCacheable($resp); $this->assertFalse($result); // Verify that authorized responses are not cacheable. $resp = new Postman_Google_Http_Request('http://localhost', 'GET'); $resp->setRequestHeaders(array('Authorization' => 'Bearer Token')); $resp->setResponseHttpCode('200'); $resp->setResponseHeaders(array('Cache-Control' => 'max-age=3600, must-revalidate', 'Expires' => 'Fri, 30 Oct 2013 14:19:41 GMT', 'Last-Modified' => 'Mon, 29 Jun 2011 02:28:12 GMT', 'ETag' => '3e86-410-3596fbbc')); $result = Postman_Google_Http_CacheParser::isResponseCacheable($resp); $this->assertFalse($result); }
public function execute() { $body = ''; /** @var Postman_Google_Http_Request $req */ foreach ($this->requests as $key => $req) { $body .= "--{$this->boundary}\n"; $body .= $req->toBatchString($key) . "\n"; $this->expected_classes["response-" . $key] = $req->getExpectedClass(); } $body = rtrim($body); $body .= "\n--{$this->boundary}--"; $url = $this->base_path . '/batch'; $httpRequest = new Postman_Google_Http_Request($url, 'POST'); $httpRequest->setRequestHeaders(array('Content-Type' => 'multipart/mixed; boundary=' . $this->boundary)); $httpRequest->setPostBody($body); $response = $this->client->getIo()->makeRequest($httpRequest); return $this->parseResponse($response); }
public function sign(Postman_Google_Http_Request $request) { if (!$this->token) { // No token, so nothing to do. return $request; } $this->client->getLogger()->debug('App Identity authentication'); // Add the OAuth2 header to the request $request->setRequestHeaders(array('Authorization' => 'Bearer ' . $this->token['access_token'])); return $request; }
public function authCache($io, $client) { $url = "http://www.googleapis.com/protected/resource"; // Create a cacheable request/response, but it should not be cached. $cacheReq = new Postman_Google_Http_Request($url, "GET"); $cacheReq->setRequestHeaders(array("Accept" => "*/*", "Authorization" => "Bearer Foo")); $cacheReq->setResponseBody("{\"a\": \"foo\"}"); $cacheReq->setResponseHttpCode(200); $cacheReq->setResponseHeaders(array("Cache-Control" => "private", "ETag" => "\"this-is-an-etag\"", "Expires" => "Sun, 22 Jan 2022 09:00:56 GMT", "Date: Sun, 1 Jan 2012 09:00:56 GMT", "Content-Type" => "application/json; charset=UTF-8")); $result = $io->setCachedRequest($cacheReq); $this->assertFalse($result); }
/** * TODO(ianbarber): This function needs simplifying. * @param $name * @param $arguments * @param $expected_class - optional, the expected class name * @return Postman_Google_Http_Request|expected_class * @throws Postman_Google_Exception */ public function call($name, $arguments, $expected_class = null) { if (!isset($this->methods[$name])) { $this->client->getLogger()->error('Service method unknown', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name)); throw new Postman_Google_Exception("Unknown function: " . "{$this->serviceName}->{$this->resourceName}->{$name}()"); } $method = $this->methods[$name]; $parameters = $arguments[0]; // postBody is a special case since it's not defined in the discovery // document as parameter, but we abuse the param entry for storing it. $postBody = null; if (isset($parameters['postBody'])) { if ($parameters['postBody'] instanceof Postman_Google_Model) { // In the cases the post body is an existing object, we want // to use the smart method to create a simple object for // for JSONification. $parameters['postBody'] = $parameters['postBody']->toSimpleObject(); } else { if (is_object($parameters['postBody'])) { // If the post body is another kind of object, we will try and // wrangle it into a sensible format. $parameters['postBody'] = $this->convertToArrayAndStripNulls($parameters['postBody']); } } $postBody = json_encode($parameters['postBody']); unset($parameters['postBody']); } // TODO(ianbarber): optParams here probably should have been // handled already - this may well be redundant code. if (isset($parameters['optParams'])) { $optParams = $parameters['optParams']; unset($parameters['optParams']); $parameters = array_merge($parameters, $optParams); } if (!isset($method['parameters'])) { $method['parameters'] = array(); } $method['parameters'] = array_merge($method['parameters'], $this->stackParameters); foreach ($parameters as $key => $val) { if ($key != 'postBody' && !isset($method['parameters'][$key])) { $this->client->getLogger()->error('Service parameter unknown', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'parameter' => $key)); throw new Postman_Google_Exception("({$name}) unknown parameter: '{$key}'"); } } foreach ($method['parameters'] as $paramName => $paramSpec) { if (isset($paramSpec['required']) && $paramSpec['required'] && !isset($parameters[$paramName])) { $this->client->getLogger()->error('Service parameter missing', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'parameter' => $paramName)); throw new Postman_Google_Exception("({$name}) missing required param: '{$paramName}'"); } if (isset($parameters[$paramName])) { $value = $parameters[$paramName]; $parameters[$paramName] = $paramSpec; $parameters[$paramName]['value'] = $value; unset($parameters[$paramName]['required']); } else { // Ensure we don't pass nulls. unset($parameters[$paramName]); } } $servicePath = $this->service->servicePath; $this->client->getLogger()->info('Service Call', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'arguments' => $parameters)); $url = Postman_Google_Http_REST::createRequestUri($servicePath, $method['path'], $parameters); $httpRequest = new Postman_Google_Http_Request($url, $method['httpMethod'], null, $postBody); $httpRequest->setBaseComponent($this->client->getBasePath()); if ($postBody) { $contentTypeHeader = array(); $contentTypeHeader['content-type'] = 'application/json; charset=UTF-8'; $httpRequest->setRequestHeaders($contentTypeHeader); $httpRequest->setPostBody($postBody); } $httpRequest = $this->client->getAuth()->sign($httpRequest); $httpRequest->setExpectedClass($expected_class); if (isset($parameters['data']) && ($parameters['uploadType']['value'] == 'media' || $parameters['uploadType']['value'] == 'multipart')) { // If we are doing a simple media upload, trigger that as a convenience. $mfu = new Postman_Google_Http_MediaFileUpload($this->client, $httpRequest, isset($parameters['mimeType']) ? $parameters['mimeType']['value'] : 'application/octet-stream', $parameters['data']['value']); } if ($this->client->shouldDefer()) { // If we are in batch or upload mode, return the raw request. return $httpRequest; } return $this->client->execute($httpRequest); }
/** * Include an accessToken in a given apiHttpRequest. * @param Postman_Google_Http_Request $request * @return Postman_Google_Http_Request * @throws Postman_Google_Auth_Exception */ public function sign(Postman_Google_Http_Request $request) { // add the developer key to the request before signing it if ($this->client->getClassConfig($this, 'developer_key')) { $request->setQueryParam('key', $this->client->getClassConfig($this, 'developer_key')); } // Cannot sign the request without an OAuth access token. if (null == $this->token && null == $this->assertionCredentials) { return $request; } // Check if the token is set to expire in the next 30 seconds // (or has already expired). if ($this->isAccessTokenExpired()) { if ($this->assertionCredentials) { $this->refreshTokenWithAssertion(); } else { $this->client->getLogger()->debug('OAuth2 access token expired'); if (!array_key_exists('refresh_token', $this->token)) { $error = "The OAuth 2.0 access token has expired," . " and a refresh token is not available. Refresh tokens" . " are not returned for responses that were auto-approved."; $this->client->getLogger()->error($error); throw new Postman_Google_Auth_Exception($error); } $this->refreshToken($this->token['refresh_token']); } } $this->client->getLogger()->debug('OAuth2 authentication'); // Add the OAuth2 header to the request $request->setRequestHeaders(array('Authorization' => 'Bearer ' . $this->token['access_token'])); return $request; }
/** * Check if an already cached request must be revalidated, and if so update * the request with the correct ETag headers. * @param Postman_Google_Http_Request $cached A previously cached response. * @param Postman_Google_Http_Request $request The outbound request. * return bool If the cached object needs to be revalidated, false if it is * still current and can be re-used. */ protected function checkMustRevalidateCachedRequest($cached, $request) { if (Postman_Google_Http_CacheParser::mustRevalidate($cached)) { $addHeaders = array(); if ($cached->getResponseHeader('etag')) { // [13.3.4] If an entity tag has been provided by the origin server, // we must use that entity tag in any cache-conditional request. $addHeaders['If-None-Match'] = $cached->getResponseHeader('etag'); } elseif ($cached->getResponseHeader('date')) { $addHeaders['If-Modified-Since'] = $cached->getResponseHeader('date'); } $request->setRequestHeaders($addHeaders); return true; } else { return false; } }