public function testRequestParameters() { $url = 'http://localhost:8080/foo/bar?foo=a&foo=b&wowee=oh+my'; $url2 = 'http://localhost:8080/foo/bar?foo=a&foo=b&wowee=oh+my&hi=there'; $request = new Google_Http_Request($url); $request->setExpectedClass("Google_Client"); $this->assertEquals(2, count($request->getQueryParams())); $request->setQueryParam("hi", "there"); $this->assertEquals($url2, $request->getUrl()); $this->assertEquals("Google_Client", $request->getExpectedClass()); $urlPath = "/foo/bar"; $request = new Google_Http_Request($urlPath); $this->assertEquals($urlPath, $request->getUrl()); $request->setBaseComponent("http://example.com"); $this->assertEquals("http://example.com" . $urlPath, $request->getUrl()); $url3a = 'http://localhost:8080/foo/bar'; $url3b = 'foo=a&foo=b&wowee=oh+my'; $url3c = 'foo=a&foo=b&wowee=oh+my&hi=there'; $request = new Google_Http_Request($url3a . "?" . $url3b, "POST"); $request->setQueryParam("hi", "there"); $request->maybeMoveParametersToBody(); $this->assertEquals($url3a, $request->getUrl()); $this->assertEquals($url3c, $request->getPostBody()); $url4 = 'http://localhost:8080/upload/foo/bar?foo=a&foo=b&wowee=oh+my&hi=there'; $request = new Google_Http_Request($url); $this->assertEquals(2, count($request->getQueryParams())); $request->setQueryParam("hi", "there"); $base = $request->getBaseComponent(); $request->setBaseComponent($base . '/upload'); $this->assertEquals($url4, $request->getUrl()); }
public function sign(Google_Http_Request $request) { $key = $this->client->getClassConfig($this, 'developer_key'); if ($key) { $request->setQueryParam('key', $key); } return $request; }
public function sign(Google_Http_Request $request) { $key = $this->client->getClassConfig($this, 'developer_key'); if ($key) { $this->client->getLogger()->debug('Simple API Access developer key authentication'); $request->setQueryParam('key', $key); } return $request; }
/** * @param $meta * @param $params * @return array|bool * @visible for testing */ private function process() { $postBody = false; $contentType = false; $meta = $this->request->getPostBody(); $meta = is_string($meta) ? json_decode($meta, true) : $meta; $uploadType = $this->getUploadType($meta); $this->request->setQueryParam('uploadType', $uploadType); $this->transformToUploadUrl(); $mimeType = $this->mimeType ? $this->mimeType : $this->request->getRequestHeader('content-type'); if (self::UPLOAD_RESUMABLE_TYPE == $uploadType) { $contentType = $mimeType; $postBody = is_string($meta) ? $meta : json_encode($meta); } else if (self::UPLOAD_MEDIA_TYPE == $uploadType) { $contentType = $mimeType; $postBody = $this->data; } else if (self::UPLOAD_MULTIPART_TYPE == $uploadType) { // This is a multipart/related upload. $boundary = $this->boundary ? $this->boundary : mt_rand(); $boundary = str_replace('"', '', $boundary); $contentType = 'multipart/related; boundary=' . $boundary; $related = "--$boundary\r\n"; $related .= "Content-Type: application/json; charset=UTF-8\r\n"; $related .= "\r\n" . json_encode($meta) . "\r\n"; $related .= "--$boundary\r\n"; $related .= "Content-Type: $mimeType\r\n"; $related .= "Content-Transfer-Encoding: base64\r\n"; $related .= "\r\n" . base64_encode($this->data) . "\r\n"; $related .= "--$boundary--"; $postBody = $related; } $this->request->setPostBody($postBody); if (isset($contentType) && $contentType) { $contentTypeHeader['content-type'] = $contentType; $this->request->setRequestHeaders($contentTypeHeader); } }
public function testRequestParameters() { $url = 'http://localhost:8080/foo/bar?foo=a&foo=b&wowee=oh+my'; $url2 = 'http://localhost:8080/foo/bar?foo=a&foo=b&wowee=oh+my&hi=there'; $request = new Google_Http_Request($this->getClient(), $url); $request->setExpectedClass("Google_Client"); $this->assertEquals(2, count($request->getQueryParams())); $request->setQueryParam("hi", "there"); $this->assertEquals($url2, $request->getUrl()); $this->assertEquals("Google_Client", $request->getExpectedClass()); $url3a = 'http://localhost:8080/foo/bar'; $url3b = 'foo=a&foo=b&wowee=oh+my'; $url3c = 'foo=a&foo=b&wowee=oh+my&hi=there'; $request = new Google_Http_Request($this->getClient(), $url3a . "?" . $url3b, "POST"); $request->setQueryParam("hi", "there"); $request->maybeMoveParametersToBody(); $this->assertEquals($url3a, $request->getUrl()); $this->assertEquals($url3c, $request->getPostBody()); }
/** * Include an accessToken in a given apiHttpRequest. * @param Google_Http_Request $request * @return Google_Http_Request * @throws Google_Auth_Exception */ public function sign(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 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; }