« lib/Google/Ads/Common/ReportDownloadError.pm
↑ Summary
lib/Google/Ads/SOAP/Deserializer/MessageParser.pm »
| 1 | # Copyright 2015, Google Inc. All Rights Reserved. | ||
| 2 | # | ||
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | # you may not use this file except in compliance with the License. | ||
| 5 | # You may obtain a copy of the License at | ||
| 6 | # | ||
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | # | ||
| 9 | # Unless required by applicable law or agreed to in writing, software | ||
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | # See the License for the specific language governing permissions and | ||
| 13 | # limitations under the License. | ||
| 14 | |||
| 15 | package Google::Ads::Common::ReportDownloadHandler; | ||
| 16 | |||
| 17 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 2 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 2 | ▶ | use version; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | |||
| 22 | # The following needs to be on one line because CPAN uses a particularly hacky | ||
| 23 | # eval() to determine module versions. | ||
| 24 | 2 | ▶ | use Google::Ads::AdWords::Constants; our $VERSION = ${Google::Ads::AdWords::Constants::VERSION}; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 25 | |||
| 26 | 2 | ▶ | use Google::Ads::AdWords::Logging; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | 2 | ▶ | use Google::Ads::AdWords::Reports::ReportingConfiguration; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 28 | 2 | ▶ | use Google::Ads::AdWords::RequestStats; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | 2 | ▶ | use Google::Ads::Common::ReportDownloadError; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | 2 | ▶ | use Google::Ads::Common::Utilities::AdsUtilityRegistry; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 31 | |||
| 32 | 2 | ▶ | use Class::Std::Fast; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 33 | |||
| 34 | 2 | ▶ | use File::stat; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 35 | 2 | ▶ | use HTTP::Request; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 36 | 2 | ▶ | use HTTP::Status qw(:constants); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 37 | 2 | ▶ | use Log::Log4perl qw(:levels); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 38 | 2 | ▶ | use LWP::UserAgent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 39 | 2 | ▶ | use MIME::Base64; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 40 | 2 | ▶ | use POSIX; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 41 | 2 | ▶ | use Time::HiRes qw(gettimeofday tv_interval); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 42 | 2 | ▶ | use URI::Escape; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 43 | 2 | ▶ | use XML::Simple; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 44 | |||
| 45 | 2 | ▶ | use constant SCRUBBED_HEADERS => qw(DeveloperToken Authorization); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 46 | |||
| 47 | 1 | my %client_of : ATTR(:name<client> :default<>); | |
| 48 | 1 | my %__user_agent_of : ATTR(:name<__user_agent> :default<>); | |
| 49 | 1 | my %__http_request_of : ATTR(:name<__http_request> :default<>); | |
| 50 | 1 | my %download_format_of : ATTR(:name<download_format> :default<>); | |
| 51 | |||
| 52 | # Returns the report contents as a string. If the report fails then returns | ||
| 53 | # a ReportDownloadError. | ||
| 54 | sub get_as_string { | ||
| 55 | my ($self) = @_; | ||
| 56 | |||
| 57 | Google::Ads::Common::Utilities::AdsUtilityRegistry->add_ads_utilities( | ||
| 58 | "ReportDownloaderString"); | ||
| 59 | |||
| 60 | my $user_agent = $self->get___user_agent(); | ||
| 61 | |||
| 62 | $self->__set_gzip_header(); | ||
| 63 | my $start_time = [gettimeofday()]; | ||
| 64 | |||
| 65 | my $response = $user_agent->request($self->get___http_request()); | ||
| 66 | $response = $self->__check_response($response, $start_time); | ||
| 67 | if (ref $response eq "Google::Ads::Common::ReportDownloadError") { | ||
| 68 | return $response; | ||
| 69 | } | ||
| 70 | return $response->decoded_content(); | ||
| 71 | } | ||
| 72 | |||
| 73 | # Saves the report response to a file. If the report fails then returns | ||
| 74 | # a ReportDownloadError. Otherwise, returns the HTTPResponse. | ||
| 75 | sub save { | ||
| 76 | my ($self, $file_path) = @_; | ||
| 77 | if (!$file_path) { | ||
| 78 | warn 'No file path provided'; | ||
| 79 | return undef; | ||
| 80 | } | ||
| 81 | |||
| 82 | Google::Ads::Common::Utilities::AdsUtilityRegistry->add_ads_utilities( | ||
| 83 | "ReportDownloaderFile"); | ||
| 84 | |||
| 85 | my $gzip_support = $self->__set_gzip_header(); | ||
| 86 | my $request = $self->get___http_request(); | ||
| 87 | my $format = $self->get_download_format(); | ||
| 88 | my $start_time = [gettimeofday()]; | ||
| 89 | my $response; | ||
| 90 | 1 | ▶ | ($file_path) = glob($file_path); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 91 | if (!$gzip_support) { | ||
| 92 | # If not gzip support then we can stream directly to a file. | ||
| 93 | $response = $self->get___user_agent()->request($request, $file_path); | ||
| 94 | $response = $self->__check_response($response, $start_time); | ||
| 95 | } else { | ||
| 96 | my $mode = ">:utf8"; | ||
| 97 | if ($format =~ /^GZIPPED|PDF/) { | ||
| 98 | # Binary format can't dump as UTF8. | ||
| 99 | $mode = ">"; | ||
| 100 | } | ||
| 101 | open(FH, $mode, $file_path) | ||
| 102 | or warn "Can't write to '$file_path': $!"; | ||
| 103 | $response = $self->get___user_agent()->request($request); | ||
| 104 | $response = $self->__check_response($response, $start_time); | ||
| 105 | if (ref $response eq "Google::Ads::Common::ReportDownloadError") { | ||
| 106 | return $response; | ||
| 107 | } | ||
| 108 | # Need to decode in a file. | ||
| 109 | print FH $response->decoded_content(); | ||
| 110 | close FH; | ||
| 111 | } | ||
| 112 | return $response; | ||
| 113 | } | ||
| 114 | |||
| 115 | # Use this method to process results as a stream. For each chunk of data | ||
| 116 | # returned, the content_callback will be invoked with two arguments: | ||
| 117 | # $data - the chunk of data | ||
| 118 | # $response - the HTTP::Response | ||
| 119 | # If the report fails then returns a ReportDownloadError. Otherwise, returns | ||
| 120 | # the HTTP::Response object. | ||
| 121 | sub process_contents { | ||
| 122 | my ($self, $content_callback) = @_; | ||
| 123 | |||
| 124 | Google::Ads::Common::Utilities::AdsUtilityRegistry->add_ads_utilities( | ||
| 125 | "ReportDownloaderStream"); | ||
| 126 | |||
| 127 | # Do not set the gzip header. If it is set then $content_callback will | ||
| 128 | # get compressed data and we don't want clients to have to deal with | ||
| 129 | # inflating the data. | ||
| 130 | my $request = $self->get___http_request(); | ||
| 131 | my $user_agent = $self->get___user_agent(); | ||
| 132 | my $start_time = [gettimeofday()]; | ||
| 133 | my $response = $user_agent->request($request, $content_callback); | ||
| 134 | $response = $self->__check_response($response, $start_time); | ||
| 135 | return $response; | ||
| 136 | } | ||
| 137 | |||
| 138 | # Checks the response's status code. If OK, then returns the HTTPResponse. | ||
| 139 | # Otherwise, returns a new ReportDownloadError. | ||
| 140 | sub __check_response { | ||
| 141 | my ($self, $response, $start_time) = @_; | ||
| 142 | my $is_successful = 0; | ||
| 143 | my $report_download_error; | ||
| 144 | my $return_val; | ||
| 145 | |||
| 146 | if ($response->code == HTTP_OK) { | ||
| 147 | $is_successful = 1; | ||
| 148 | $return_val = $response; | ||
| 149 | } else { | ||
| 150 | if ($response->code == HTTP_BAD_REQUEST) { | ||
| 151 | $report_download_error = $self->__extract_xml_error($response); | ||
| 152 | } else { | ||
| 153 | $report_download_error = Google::Ads::Common::ReportDownloadError->new({ | ||
| 154 | response_code => $response->code, | ||
| 155 | response_message => $response->message | ||
| 156 | }); | ||
| 157 | } | ||
| 158 | $return_val = $report_download_error; | ||
| 159 | } | ||
| 160 | # Log request and response information before returning the result. | ||
| 161 | $self->__log_report_request_response($response, $is_successful, | ||
| 162 | $report_download_error, tv_interval($start_time)); | ||
| 163 | return $return_val; | ||
| 164 | } | ||
| 165 | |||
| 166 | # Sets the header and updates the user agent for gzip support if the | ||
| 167 | # environment supports gzip compression. | ||
| 168 | sub __set_gzip_header { | ||
| 169 | my ($self) = @_; | ||
| 170 | my $user_agent = $self->get___user_agent(); | ||
| 171 | |||
| 172 | my $can_accept = HTTP::Message::decodable; | ||
| 173 | my $gzip_support = $can_accept =~ /gzip/i; | ||
| 174 | |||
| 175 | # Setting HTTP user-agent and gzip compression. | ||
| 176 | $user_agent->default_header("Accept-Encoding" => scalar $can_accept); | ||
| 177 | |||
| 178 | my $user_agent_description = $self->get_client()->_get_header()->{userAgent}; | ||
| 179 | # Set the header for gzip support. | ||
| 180 | $user_agent_description = sprintf("%s%s", $user_agent_description, | ||
| 181 | ($gzip_support ? " gzip" : "")); | ||
| 182 | $user_agent->agent($user_agent_description); | ||
| 183 | return $gzip_support; | ||
| 184 | } | ||
| 185 | |||
| 186 | # Returns a new ReportDownloadError containing the error details of the | ||
| 187 | # failed HTTP::Response. | ||
| 188 | sub __extract_xml_error { | ||
| 189 | my ($self, $response) = @_; | ||
| 190 | my $ref = | ||
| 191 | XML::Simple->new()->XMLin($response->decoded_content(), ForceContent => 1); | ||
| 192 | |||
| 193 | return Google::Ads::Common::ReportDownloadError->new({ | ||
| 194 | response_code => $response->code, | ||
| 195 | response_message => $response->message, | ||
| 196 | type => $ref->{ApiError}->{type}->{content}, | ||
| 197 | field_path => $ref->{ApiError}->{fieldPath}->{content} | ||
| 198 | ? $ref->{ApiError}->{fieldPath}->{content} | ||
| 199 | : "", | ||
| 200 | trigger => $ref->{ApiError}->{trigger}->{content} | ||
| 201 | ? $ref->{ApiError}->{trigger}->{content} | ||
| 202 | : "" | ||
| 203 | }); | ||
| 204 | } | ||
| 205 | |||
| 206 | # Logs the report request, response, and stats. | ||
| 207 | sub __log_report_request_response { | ||
| 208 | my ($self, $response, $is_successful, $error_message, $elapsed_seconds) = @_; | ||
| 209 | |||
| 210 | my $client = $self->get_client(); | ||
| 211 | my $request = $self->get___http_request(); | ||
| 212 | |||
| 213 | # Always log the request stats to the AdWordsAPI logger. | ||
| 214 | my $auth_handler = $client->_get_auth_handler(); | ||
| 215 | |||
| 216 | my $request_stats = Google::Ads::AdWords::RequestStats->new({ | ||
| 217 | server => $client->get_alternate_url(), | ||
| 218 | client_id => $client->get_client_id(), | ||
| 219 | service_name => $request->uri, | ||
| 220 | method_name => $request->method, | ||
| 221 | is_fault => !$is_successful, | ||
| 222 | response_time => int(($elapsed_seconds * 1000) + 0.5), | ||
| 223 | fault_message => (!$is_successful) ? $response->message : "" | ||
| 224 | }); | ||
| 225 | $client->_push_new_request_stats($request_stats); | ||
| 226 | Google::Ads::AdWords::Logging::get_awapi_logger->info($request_stats); | ||
| 227 | |||
| 228 | # Log the request. | ||
| 229 | if ($request) { | ||
| 230 | # Log the full request: | ||
| 231 | # To WARN if the request failed OR | ||
| 232 | # To INFO if the request succeeded | ||
| 233 | my $request_string = $request->as_string("\n"); | ||
| 234 | # Remove sensitive information from the log message. | ||
| 235 | foreach my $header (SCRUBBED_HEADERS) { | ||
| 236 | $request_string =~ s!(\n$header):(.*)\n!$1: REDACTED\n!; | ||
| 237 | } | ||
| 238 | my $log_message = sprintf( | ||
| 239 | "Outgoing request:\n%s", | ||
| 240 | $request_string | ||
| 241 | ); | ||
| 242 | Google::Ads::AdWords::Logging::get_soap_logger->log( | ||
| 243 | $is_successful ? $INFO : $WARN, $log_message); | ||
| 244 | } | ||
| 245 | |||
| 246 | # Log the response. | ||
| 247 | if ($response) { | ||
| 248 | # Log: | ||
| 249 | # To WARN if the request failed OR | ||
| 250 | # To INFO (status and message only) | ||
| 251 | my $response_string = $response->headers_as_string("\n"); | ||
| 252 | # Remove sensitive information from the log message. | ||
| 253 | foreach my $header (SCRUBBED_HEADERS) { | ||
| 254 | $response_string =~ s!(\n$header):(.*)\n!$1: REDACTED\n!; | ||
| 255 | } | ||
| 256 | my $log_message = sprintf( | ||
| 257 | "Incoming %s report response with status code %s and message '%s'\n%s" . | ||
| 258 | "REDACTED REPORT DATA", | ||
| 259 | $is_successful ? 'successful' : 'failed', | ||
| 260 | $response->code, $response->message, | ||
| 261 | $response_string | ||
| 262 | ); | ||
| 263 | |||
| 264 | if ($is_successful) { | ||
| 265 | Google::Ads::AdWords::Logging::get_soap_logger->info($log_message); | ||
| 266 | } else { | ||
| 267 | if (ref $error_message eq "Google::Ads::Common::ReportDownloadError") { | ||
| 268 | $log_message = $log_message . | ||
| 269 | sprintf( | ||
| 270 | ": An error has occurred of type '%s', triggered by '%s'", | ||
| 271 | $error_message->get_type(), | ||
| 272 | $error_message->get_trigger()); | ||
| 273 | } elsif ($error_message) { | ||
| 274 | $log_message = $log_message . ': ' . $error_message; | ||
| 275 | } | ||
| 276 | Google::Ads::AdWords::Logging::get_soap_logger->logwarn($log_message); | ||
| 277 | } | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | 1 | 1; | |
| 282 | |||
| 283 | =pod | ||
| 284 | |||
| 285 | =head1 NAME | ||
| 286 | |||
| 287 | Google::Ads::Common::ReportDownloadHandler | ||
| 288 | |||
| 289 | =head1 DESCRIPTION | ||
| 290 | |||
| 291 | Represents a report response from the AdWords API. | ||
| 292 | |||
| 293 | |||
| 294 | =head2 PROPERTIES | ||
| 295 | |||
| 296 | The following properties may be accessed using get_PROPERTY methods: | ||
| 297 | |||
| 298 | =over | ||
| 299 | |||
| 300 | =item * client | ||
| 301 | |||
| 302 | A reference to a Google::Ads::AdWords::Client. | ||
| 303 | |||
| 304 | =item * __user_agent (Private) | ||
| 305 | |||
| 306 | A reference to an LWP::UserAgent. | ||
| 307 | |||
| 308 | =item * __http_request (Private) | ||
| 309 | |||
| 310 | A reference to an HTTP::Request. | ||
| 311 | |||
| 312 | =item * download_format | ||
| 313 | |||
| 314 | The download format of the request. | ||
| 315 | |||
| 316 | =back | ||
| 317 | |||
| 318 | |||
| 319 | =head1 METHODS | ||
| 320 | |||
| 321 | =head2 new | ||
| 322 | |||
| 323 | Constructor. The following data structure may be passed to new(): | ||
| 324 | |||
| 325 | { # Google::Ads::Common::ReportDownloadHandler | ||
| 326 | client => $response, # A ref to a Google::Ads::AdWords::Client object | ||
| 327 | __user_agent => $user_agent, # A ref to an LWP::UserAgent | ||
| 328 | __http_request => $request, # A ref to an HTTP::Request object | ||
| 329 | download_format => $download_format, # The download format for the request | ||
| 330 | }, | ||
| 331 | |||
| 332 | =head1 METHODS | ||
| 333 | |||
| 334 | =head2 get_as_string | ||
| 335 | |||
| 336 | Issues the report request to the AdWords API and returns the report contents | ||
| 337 | as a string. | ||
| 338 | |||
| 339 | =head3 Returns | ||
| 340 | |||
| 341 | The report contents as a string if the request is successful. Otherwise, returns | ||
| 342 | a L<Google::Ads::Common::ReportDownloadError>. Check for failures by evaluating | ||
| 343 | the return value in a boolean context, where a | ||
| 344 | L<Google::Ads::Common::ReportDownloadError> will always evaluate to false. | ||
| 345 | |||
| 346 | =head3 Exceptions | ||
| 347 | |||
| 348 | Returns a L<Google::Ads::Common::ReportDownloadError> if the report request | ||
| 349 | fails. | ||
| 350 | |||
| 351 | =head2 save | ||
| 352 | |||
| 353 | Issues the report request to the AdWords API and saves the report contents | ||
| 354 | to a file. | ||
| 355 | |||
| 356 | =head3 Parameters | ||
| 357 | |||
| 358 | =over | ||
| 359 | |||
| 360 | =item * | ||
| 361 | |||
| 362 | The destination file for the report contents. | ||
| 363 | |||
| 364 | =back | ||
| 365 | |||
| 366 | =head3 Returns | ||
| 367 | |||
| 368 | The report contents as a string if the request is successful. Otherwise, returns | ||
| 369 | a L<Google::Ads::Common::ReportDownloadError>. Check for failures by | ||
| 370 | evaluating the return value in a boolean context, where a | ||
| 371 | L<Google::Ads::Common::ReportDownloadError> will always evaluate to false. | ||
| 372 | |||
| 373 | =head3 Exceptions | ||
| 374 | |||
| 375 | Returns a L<Google::Ads::Common::ReportDownloadError> if the report request | ||
| 376 | fails. | ||
| 377 | |||
| 378 | =head2 process_contents | ||
| 379 | |||
| 380 | Issues the report request to the AdWords API and invokes a callback for each | ||
| 381 | chunk of content received. Use this method to process the report contents as | ||
| 382 | a stream. | ||
| 383 | |||
| 384 | =head3 Parameters | ||
| 385 | |||
| 386 | =over | ||
| 387 | |||
| 388 | =item * | ||
| 389 | |||
| 390 | A content_callback that will be invoked for each chunk of data returned | ||
| 391 | by the report request. Each invocation will be passed two arguments: | ||
| 392 | |||
| 393 | =over | ||
| 394 | |||
| 395 | =item * | ||
| 396 | |||
| 397 | The chunk of data | ||
| 398 | |||
| 399 | =item * | ||
| 400 | |||
| 401 | The HTTP::Response | ||
| 402 | |||
| 403 | =back | ||
| 404 | |||
| 405 | =back | ||
| 406 | |||
| 407 | =head3 Returns | ||
| 408 | |||
| 409 | An HTTP::Response if the request is successful. Otherwise, returns | ||
| 410 | a L<Google::Ads::Common::ReportDownloadError>. Check for failures by | ||
| 411 | evaluating the return value in a boolean context, where a | ||
| 412 | L<Google::Ads::Common::ReportDownloadError> will always evaluate to false. | ||
| 413 | |||
| 414 | =head3 Exceptions | ||
| 415 | |||
| 416 | Returns a L<Google::Ads::Common::ReportDownloadError> if the report request | ||
| 417 | fails. | ||
| 418 | |||
| 419 | =cut | ||
| 420 | |||