/** * Downloads a report from a URL. If the filePath parameter is specified it * will be downloaded to the file at that path, otherwise it will be * downloaded to memory and be returned as a string. * @param string $downloadUrl the URL of the report download * @param string $filePath an optional path of a file to download the report * to * @return mixed if $filePath isn't specified it will return the contents of * the report, otherwise the size in bytes of the downloaded report */ public static function DownloadReport($downloadUrl, $filePath = null) { DeprecationUtils::LogDeprecatedMethodUsage('ReportUtils::DownloadReport', 'Please use ReportDownloader.php instead.'); // TODO(vtsao): This method should not be static and instantiation of this // class should be allowed so we can "inject" CurlUtils, but would break too // many things that rely on this method being static. $curlUtils = new CurlUtils(); $ch = $curlUtils->CreateSession($downloadUrl); if (isset($filePath)) { $file = fopen($filePath, 'w'); $curlUtils->SetOpt($ch, CURLOPT_FILE, $file); } else { $curlUtils->SetOpt($ch, CURLOPT_RETURNTRANSFER, 1); } $result = $curlUtils->Exec($ch); $httpCode = $curlUtils->GetInfo($ch, CURLINFO_HTTP_CODE); $error = $curlUtils->Error($ch); $downloadSize = $curlUtils->GetInfo($ch, CURLINFO_SIZE_DOWNLOAD); $curlUtils->Close($ch); if (isset($file)) { fclose($file); } if ($httpCode != 200) { $message = sprintf('Invalid report download URL: %s', $downloadUrl); throw new InvalidArgumentException($message, $httpCode); } if (isset($filePath)) { return $downloadSize; } else { return $result; } }
public function testShouldRaiseClientLoginWarning_InvalidUsingClientLogin() { $credentials = array(); $mockUser = $this->getMockBuilder('AdsUser')->disableOriginalConstructor()->getMockForAbstractClass(); $mockUser->SetOAuth2Info($credentials); $this->assertTrue(DeprecationUtils::ShouldRaiseClientLoginWarning($mockUser)); }
public function testCheckUsingClientLoginWithUnsupportedVersion_UsingOAuth2WithAnyVersion() { $credentials = array('refresh_token' => 'REFRESH_TOKEN'); $mockUser = $this->getMockBuilder('AdsUser')->disableOriginalConstructor()->getMockForAbstractClass(); $mockUser->SetOAuth2Info($credentials); DeprecationUtils::CheckUsingClientLoginWithUnsupportedVersion($mockUser, "v201311", "v201208"); DeprecationUtils::CheckUsingClientLoginWithUnsupportedVersion($mockUser, "v201311", "v201311"); DeprecationUtils::CheckUsingClientLoginWithUnsupportedVersion($mockUser, "v201311", "v201403"); }
/** * Gets the HTTP headers for the report download request. * @param AdWordsUser $user the AdWordsUser to get credentials from * @param string $url the URL the request will be made to * @param array $options the options for the download * @return array and array of strings, which are header names and values */ private static function GetHeaders($user, $url, array $options = NULL) { $headers = array(); $version = !empty($options['version']) ? $options['version'] : $user->GetDefaultVersion(); // Authorization. $authHeader = NULL; $oAuth2Info = $user->GetOAuth2Info(); $oAuth2Handler = $user->GetOAuth2Handler(); if (!empty($oAuth2Info)) { $oAuth2Info = $oAuth2Handler->GetOrRefreshAccessToken($oAuth2Info); $user->SetOAuth2Info($oAuth2Info); $authHeader = $oAuth2Handler->FormatCredentialsForHeader($oAuth2Info); } else { DeprecationUtils::CheckUsingClientLoginWithUnsupportedVersion($user, AdWordsUser::FINAL_CLIENT_LOGIN_VERSION, $version); $authHeader = sprintf(self::CLIENT_LOGIN_FORMAT, $user->GetAuthToken()); } $headers['Authorization'] = $authHeader; // Developer token. $headers['developerToken'] = $user->GetDeveloperToken(); // Target client. $email = $user->GetEmail(); $clientCustomerId = $user->GetClientCustomerId(); if (isset($clientCustomerId)) { $headers['clientCustomerId'] = $clientCustomerId; } else { if ($version < 'v201109' && isset($email)) { $headers['clientEmail'] = $email; } else { throw new ReportDownloadException('The client customer ID must be ' . 'specified for report downloads.'); } } // Flags. if (isset($options['returnMoneyInMicros'])) { DeprecationUtils::CheckUsingReturnMoneyInMicrosWithUnsupportedVersion(self::FINAL_RETURN_MONEY_IN_MICROS_VERSION, $version); $headers['returnMoneyInMicros'] = $options['returnMoneyInMicros'] ? 'true' : 'false'; } return $headers; }
/** * Gets the HTTP headers for the report download request. * @param AdWordsUser $user the AdWordsUser to get credentials from * @param string $url the URL the request will be made to * @param array $options the options for the download * @return array and array of strings, which are header names and values */ private static function GetHeaders($user, $url, array $options = NULL) { $headers = array(); $version = !empty($options['version']) ? $options['version'] : $user->GetDefaultVersion(); // Authorization. $authHeader = NULL; $oAuth2Info = $user->GetOAuth2Info(); $oAuth2Handler = $user->GetOAuth2Handler(); if (!empty($oAuth2Info)) { $oAuth2Info = $oAuth2Handler->GetOrRefreshAccessToken($oAuth2Info); $user->SetOAuth2Info($oAuth2Info); $authHeader = $oAuth2Handler->FormatCredentialsForHeader($oAuth2Info); } else { throw new ServiceException('OAuth 2.0 authentication is required.'); } $headers['Authorization'] = $authHeader; // Developer token. $headers['developerToken'] = $user->GetDeveloperToken(); // Target client. $clientCustomerId = $user->GetClientCustomerId(); if (isset($clientCustomerId)) { $headers['clientCustomerId'] = $clientCustomerId; } else { throw new ReportDownloadException('The client customer ID must be ' . 'specified for report downloads.'); } // Flags. if (isset($options['returnMoneyInMicros'])) { DeprecationUtils::CheckUsingReturnMoneyInMicrosWithUnsupportedVersion(self::FINAL_RETURN_MONEY_IN_MICROS_VERSION, $version); $headers['returnMoneyInMicros'] = $options['returnMoneyInMicros'] ? 'true' : 'false'; } return $headers; }
/** * Overrides the method SoapClient.__soapCall() to process the * response from the SOAP call. * @param string $function_name the name of the function being called * @param array $arguments the arguments to that function * @param array $options the options for the SOAP call * @param array $input_headers the optional input headers * @param array $output_headers the options output headers * @return mixed the return from the parent __soapCall * @throws SOAPFault if there was an exception making the request */ function __soapCall($function_name, $arguments, $options = NULL, $input_headers = NULL, &$output_headers = NULL) { DeprecationUtils::CheckUsingClientLogin($this->user); try { $input_headers[] = $this->GenerateSoapHeader(); $this->lastHeaders = $input_headers; $this->lastArguments = $arguments; $response = parent::__soapCall($function_name, $arguments, $options, $input_headers, $output_headers); $this->ProcessResponse($this->lastRequest, $this->__getLastResponse(), $function_name); return $response; } catch (SoapFault $e) { $this->ProcessResponse($this->lastRequest, $this->__getLastResponse(), $function_name, $e); throw $e; } }
/** * Overrides the method SoapClient.__soapCall() to process the * response from the SOAP call. * @param string $function_name the name of the function being called * @param array $arguments the arguments to that function * @param array $options the options for the SOAP call * @param array $input_headers the optional input headers * @param array $output_headers the options output headers * @return mixed the return from the parent __soapCall * @throws SOAPFault if there was an exception making the request */ function __soapCall($function_name, $arguments, $options = NULL, $input_headers = NULL, &$output_headers = NULL) { DeprecationUtils::CheckUsingClientLogin($this->user); try { $input_headers[] = $this->GenerateSoapHeader(); $this->lastHeaders = $input_headers; $this->lastArguments = $arguments; $httpHeaders = $this->GenerateHttpHeaders(); if (!empty($httpHeaders)) { // The context this soap client was originally created with. This is the // only way to modify the HTTP headers per soap call. $existingStreamContext = $this->options['stream_context']; $existingStreamContextOptions = stream_context_get_options($existingStreamContext); // Override the existing HTTP headers each time since they may have // changed. $existingStreamContextOptions['http']['header'] = implode("\r\n", array_map('AdsSoapClient::implodeHttpHeaders', array_keys($httpHeaders), $httpHeaders)); stream_context_set_option($existingStreamContext, $existingStreamContextOptions); } $response = parent::__soapCall($function_name, $arguments, $options, $input_headers, $output_headers); $this->ProcessResponse($this->lastRequest, $this->__getLastResponse(), $function_name); return $response; } catch (SoapFault $e) { $this->ProcessResponse($this->lastRequest, $this->__getLastResponse(), $function_name, $e); throw $e; } }
/** * Gets the HTTP headers for the report download request. * @param AdWordsUser $user the AdWordsUser to get credentials from * @param string $url the URL the request will be made to * @param array $options the options for the download * @return array and array of strings, which are header names and values */ private static function GetHeaders($user, $url, array $options = null) { $headers = array(); $version = !empty($options['version']) ? $options['version'] : $user->GetDefaultVersion(); // Authorization. $authHeader = null; $oAuth2Info = $user->GetOAuth2Info(); $oAuth2Handler = $user->GetOAuth2Handler(); if (!empty($oAuth2Info)) { $oAuth2Info = $oAuth2Handler->GetOrRefreshAccessToken($oAuth2Info); $user->SetOAuth2Info($oAuth2Info); $authHeader = $oAuth2Handler->FormatCredentialsForHeader($oAuth2Info); } else { throw new ServiceException('OAuth 2.0 authentication is required.'); } $headers['Authorization'] = $authHeader; // Developer token. $headers['developerToken'] = $user->GetDeveloperToken(); // Target client. $clientCustomerId = $user->GetClientCustomerId(); if (isset($clientCustomerId)) { $headers['clientCustomerId'] = $clientCustomerId; } else { throw new ReportDownloadException('The client customer ID must be ' . 'specified for report downloads.'); } // Flags. if (isset($options['skipReportHeader'])) { $headers['skipReportHeader'] = $options['skipReportHeader'] ? 'true' : 'false'; } if (isset($options['skipColumnHeader'])) { DeprecationUtils::CheckUsingReportHeadersWithUnsupportedVersion('skipColumnHeader', self::MINIMUM_SKIP_COLUMN_HEADER_VERSION, $version); $headers['skipColumnHeader'] = $options['skipColumnHeader'] ? 'true' : 'false'; } if (isset($options['skipReportSummary'])) { $headers['skipReportSummary'] = $options['skipReportSummary'] ? 'true' : 'false'; } if (isset($options['includeZeroImpressions'])) { DeprecationUtils::CheckUsingReportHeadersWithUnsupportedVersion('includeZeroImpressions', self::MINIMUM_INCLUDE_ZERO_IMPRESSIONS_HEADER_VERSION, $version); $headers['includeZeroImpressions'] = $options['includeZeroImpressions'] ? 'true' : 'false'; } return $headers; }
/** * Returns some of the objects from an account, using the service and method * name provided. * @param DfpSoapClient $service the service to use * @param string $methodName the name of the method * @param string $filterText filter text * @return array the objects retrieved from the service using the method * @throws InvalidArgumentException if <var>$methodName</var> is not a * get*ByStatement method or the filterText contains the LIMIT or * OFFSET options */ public static function GetSomeObjects(DfpSoapClient $service, $methodName, $filterText) { DeprecationUtils::LogDeprecatedMethodUsage('GetSomeObjects', 'https://github.com/googleads/googleads-php-lib/wiki/Migrating-off-of-DFP-ServiceUtils-functions'); if (!preg_match(self::GET_BY_STATEMENT_METHOD_NAME_REGEX, $methodName)) { throw new InvalidArgumentException('The method name must be in the format a "get*ByStatement".'); } $filterStatement = new Statement(); $filterStatement->query = $filterText; $page = $service->{$methodName}($filterStatement); return $page->results; }
/** * @expectedException ServiceException */ public function testCheckUsingIncludeZeroImpressionsHeaderWithUnsupportedVersion() { $this->setExpectedException('ServiceException'); DeprecationUtils::CheckUsingReportHeadersWithUnsupportedVersion('includeZeroImpressions', 'v201506', 'v201502'); }
/** * Gets the service by its service name and group. * @param $serviceName the service name * @param string $version the version of the service to get. If * <var>NULL</var>, then the default version will be used * @param string $server the server to make the request to. If * <var>NULL</var>, then the default server will be used * @param SoapClientFactory $serviceFactory the factory to create the client. * If <var>NULL</var>, then the built-in SOAP client factory will be used * @param bool $validateOnly if the service should be created in validateOnly * mode * @param bool $partialFailure if the service should be created in * partialFailure mode * @return SoapClient the instantiated service * @throws ServiceException if an error occurred when getting the service */ public function GetService($serviceName, $version = NULL, $server = NULL, SoapClientFactory $serviceFactory = NULL, $validateOnly = NULL, $partialFailure = NULL) { $this->ValidateUser(); if (!isset($serviceFactory)) { if (!isset($version)) { $version = $this->GetDefaultVersion(); } if (!isset($server)) { $server = $this->GetDefaultServer(); } $serviceFactory = new AdWordsSoapClientFactory($this, $version, $server, $validateOnly, $partialFailure); } DeprecationUtils::CheckUsingClientLoginWithUnsupportedVersion($this, self::FINAL_CLIENT_LOGIN_VERSION, $version); return parent::GetServiceSoapClient($serviceName, $serviceFactory); }
public function testCheckUsingReturnMoneyInMicrosWithUnsupportedVersion_UsingWithAnyVersion() { DeprecationUtils::CheckUsingReturnMoneyInMicrosWithUnsupportedVersion('v201402', 'v201402'); DeprecationUtils::CheckUsingReturnMoneyInMicrosWithUnsupportedVersion('v201402', 'v201309'); }
/** * @expectedException ServiceException */ public function testCheckUsingSkipColumnHeaderWithUnsupportedVersion() { $this->setExpectedException('ServiceException'); DeprecationUtils::CheckUsingSkipReportHeaderWithUnsupportedVersion('skipColumnHeader', 'v201502', 'v201409'); }