| 1 | # Copyright 2019, Google LLC | ||||||||||||||||||||||||||||
| 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 | # The base class for all Google Ads API services, e.g. CampaignService, | ||||||||||||||||||||||||||||
| 16 | # AdGroupService, etc. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | package Google::Ads::GoogleAds::BaseService; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 4 | ▶ | use version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # The following needs to be on one line because CPAN uses a particularly hacky | ||||||||||||||||||||||||||||
| 25 | # eval() to determine module versions. | ||||||||||||||||||||||||||||
| 26 | 4 | ▶ | use Google::Ads::GoogleAds::Constants; our $VERSION = ${Google::Ads::GoogleAds::Constants::VERSION}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 4 | ▶ | use Google::Ads::GoogleAds::Logging::GoogleAdsLogger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 4 | ▶ | use Google::Ads::GoogleAds::Utils::GoogleAdsHelper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 4 | ▶ | use Google::Ads::GoogleAds::GoogleAdsException; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 4 | ▶ | use Class::Std::Fast; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 4 | ▶ | use LWP::UserAgent::Determined; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 4 | ▶ | use HTTP::Status qw(:constants); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 4 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 4 | ▶ | use URI::Query; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 4 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 4 | ▶ | use constant GET => "GET"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 4 | ▶ | use constant POST => "POST"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # Class::Std-style attributes. Need to be kept in the same line. | ||||||||||||||||||||||||||||
| 42 | # These need to go in the same line for older Perl interpreters to understand. | ||||||||||||||||||||||||||||
| 43 | 4 | my %api_client_of : ATTR(:name<api_client> :default<>); | |||||||||||||||||||||||||||
| 44 | 4 | my %__lwp_agent_of : ATTR(:name<__lwp_agent> :default<>); | |||||||||||||||||||||||||||
| 45 | 4 | my %__json_coder_of : ATTR(:name<__json_coder> :default<>); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Automatically called by Class::Std after the values for all the attributes | ||||||||||||||||||||||||||||
| 48 | # have been populated but before the constructor returns the new object. | ||||||||||||||||||||||||||||
| 49 | 4 | ▶ | sub START { | ||||||||||||||||||||||||||
|
Subroutine
START: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | 4 | my ($self, $ident) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 4 | ▶ | $__lwp_agent_of{$ident} ||= LWP::UserAgent::Determined->new(); | ||||||||||||||||||||||||||
|
Condition66%2/3
$__lwp_agent_of{$ident} ||= "LWP::UserAgent::Determined"->new
MC/DC0%0/2
$__lwp_agent_of{$ident} ||= "LWP::UserAgent::Determined"->new
$__lwp_agent_of{$ident}
"LWP::UserAgent::Determined"->new
Truth table66%2/3
A ||= B
A= $__lwp_agent_of{$ident}B= "LWP::UserAgent::Determined"->new
| |||||||||||||||||||||||||||||
| 53 | # The 'pretty' attribute should be enabled for more readable form in the log. | ||||||||||||||||||||||||||||
| 54 | # The 'convert_blessed' attributed should be enabled to convert blessed objects. | ||||||||||||||||||||||||||||
| 55 | 4 | ▶ | $__json_coder_of{$ident} ||= JSON::XS->new->utf8->pretty->convert_blessed; | ||||||||||||||||||||||||||
|
Condition33%1/3
$__json_coder_of{$ident} ||= "JSON::XS"->new->utf8->pretty->convert_blessed
MC/DC0%0/2
$__json_coder_of{$ident} ||= "JSON::XS"->new->utf8->pretty->convert_blessed
$__json_coder_of{$ident}
"JSON::XS"->new->utf8->pretty->convert_blessed
Truth table33%1/3
A ||= B
A= $__json_coder_of{$ident}B= "JSON::XS"->new->utf8->pretty->convert_blessed
| |||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # Sends a HTTP request to Google Ads API server and handles the response. | ||||||||||||||||||||||||||||
| 59 | 1 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: called CC=23 SCAR=40.7
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 1 | my ($self, $http_method, $request_path, $request_body, $response_type, | |||||||||||||||||||||||||||
| 61 | $content_callback) | ||||||||||||||||||||||||||||
| 62 | = @_; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | my $api_client = $self->get_api_client(); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | ############################################################################## | ||||||||||||||||||||||||||||
| 67 | # Step 1: Prepare for the request URL and request content. | ||||||||||||||||||||||||||||
| 68 | ############################################################################## | ||||||||||||||||||||||||||||
| 69 | 1 | ▶ | if ($http_method eq GET) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 70 | # HTTP GET request scenarios: | ||||||||||||||||||||||||||||
| 71 | # GET: v26/customers:listAccessibleCustomers | ||||||||||||||||||||||||||||
| 72 | # GET: v26/{+resourceName} | ||||||||||||||||||||||||||||
| 73 | # GET: v26/{+resourceName}:listResults | ||||||||||||||||||||||||||||
| 74 | # GET: v26/customers/{+customerId}/paymentsAccounts | ||||||||||||||||||||||||||||
| 75 | # GET: v26/customers/{+customerId}/merchantCenterLinks | ||||||||||||||||||||||||||||
| 76 | 0 | $request_path = expand_path_template($request_path, $request_body); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # GET: When the $request_body is a hash reference, use the path parameters | ||||||||||||||||||||||||||||
| 79 | # in the hash to expand the $request_path, and add all the other key-value | ||||||||||||||||||||||||||||
| 80 | # pairs to the URL query parameters if there is any. e.g. | ||||||||||||||||||||||||||||
| 81 | # | ||||||||||||||||||||||||||||
| 82 | # GET: CampaignExperimentService.list_async_errors | ||||||||||||||||||||||||||||
| 83 | # GET: CampaignDraftService.list_async_errors | ||||||||||||||||||||||||||||
| 84 | # GET: BatchJobService.list_results | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if (ref $request_body and (keys %$request_body) > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $request_body and keys %$request_body > 0
MC/DC0%0/2
ref $request_body and keys %$request_body > 0
ref $request_body
keys %$request_body > 0
Truth table0%0/3
A and B
A= ref $request_bodyB= keys %$request_body > 0
| |||||||||||||||||||||||||||||
| 86 | 0 | $request_path .= ("?" . URI::Query->new($request_body)); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } elsif ($http_method eq POST) { | ||||||||||||||||||||||||||||
| 89 | # HTTP POST request scenarios: | ||||||||||||||||||||||||||||
| 90 | # POST: v26/geoTargetConstants:suggest | ||||||||||||||||||||||||||||
| 91 | # POST: v26/googleAdsFields:search | ||||||||||||||||||||||||||||
| 92 | # POST: v26/customers/{+customerId}/googleAds:search | ||||||||||||||||||||||||||||
| 93 | # POST: v26/customers/{+customerId}/campaigns:mutate | ||||||||||||||||||||||||||||
| 94 | # POST: v26/{+keywordPlan}:generateForecastMetrics | ||||||||||||||||||||||||||||
| 95 | # POST: v26/{+campaignDraft}:promote | ||||||||||||||||||||||||||||
| 96 | # POST: v26/{+resourceName}:addOperations | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # POST: Retain the 'customerId' variable in the $request_body hash | ||||||||||||||||||||||||||||
| 99 | # reference after the $request_path is expanded. | ||||||||||||||||||||||||||||
| 100 | 1 | ▶ | my $customer_id = $request_body->{customerId} if ref $request_body; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 1 | $request_path = expand_path_template($request_path, $request_body); | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 1 | ▶ | $request_body->{customerId} = $customer_id if defined $customer_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | } else { | ||||||||||||||||||||||||||||
| 106 | # Other HTTP request scenarios: | ||||||||||||||||||||||||||||
| 107 | # DELETE: v26/{+name} for OperationService | ||||||||||||||||||||||||||||
| 108 | 0 | $request_path = expand_path_template($request_path, $request_body); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Generate the request URL from the API service address and the request path. | ||||||||||||||||||||||||||||
| 112 | 1 | my $request_url = $api_client->get_service_address() . $request_path; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 1 | my $json_coder = $self->get___json_coder(); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Encode the JSON request content for POST request. | ||||||||||||||||||||||||||||
| 117 | 1 | my $request_content = undef; | |||||||||||||||||||||||||||
| 118 | 1 | ▶ | if ($http_method eq POST) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 1 | ▶ | $request_content = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | defined $request_body | ||||||||||||||||||||||||||||
| 121 | ? $json_coder->encode($request_body) | ||||||||||||||||||||||||||||
| 122 | : '{}'; | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | ############################################################################## | ||||||||||||||||||||||||||||
| 126 | # Step 2: Send the authorized HTTP request and handle the HTTP response. | ||||||||||||||||||||||||||||
| 127 | ############################################################################## | ||||||||||||||||||||||||||||
| 128 | 1 | my $auth_handler = $api_client->_get_auth_handler(); | |||||||||||||||||||||||||||
| 129 | 1 | ▶ | if (!$auth_handler) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | $api_client->get_die_on_faults() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | ? die(Google::Ads::GoogleAds::Constants::NO_AUTH_HANDLER_SETUP_MESSAGE) | ||||||||||||||||||||||||||||
| 132 | : warn(Google::Ads::GoogleAds::Constants::NO_AUTH_HANDLER_SETUP_MESSAGE); | ||||||||||||||||||||||||||||
| 133 | 0 | return; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1 | my $http_headers = $self->_get_http_headers(); | |||||||||||||||||||||||||||
| 137 | 1 | my $http_request = | |||||||||||||||||||||||||||
| 138 | $auth_handler->prepare_request($http_method, $request_url, $http_headers, | ||||||||||||||||||||||||||||
| 139 | $request_content); | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 1 | ▶ | utf8::is_utf8 $http_request and utf8::encode $http_request; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 1 | my $lwp_agent = $self->get___lwp_agent(); | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # Set up HTTP timeout, retry and proxy for the lwp agent. | ||||||||||||||||||||||||||||
| 146 | 1 | my $http_timeout = $api_client->get_http_timeout(); | |||||||||||||||||||||||||||
| 147 | 1 | ▶ | $lwp_agent->timeout( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | $http_timeout | ||||||||||||||||||||||||||||
| 149 | ? $http_timeout | ||||||||||||||||||||||||||||
| 150 | : Google::Ads::GoogleAds::Constants::DEFAULT_HTTP_TIMEOUT | ||||||||||||||||||||||||||||
| 151 | ); | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 1 | my $http_retry_timing = $api_client->get_http_retry_timing(); | |||||||||||||||||||||||||||
| 154 | 1 | ▶ | $lwp_agent->timing( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | $http_retry_timing | ||||||||||||||||||||||||||||
| 156 | ? $http_retry_timing | ||||||||||||||||||||||||||||
| 157 | : Google::Ads::GoogleAds::Constants::DEFAULT_HTTP_RETRY_TIMING | ||||||||||||||||||||||||||||
| 158 | ); | ||||||||||||||||||||||||||||
| 159 | # Retry for status codes 503 & 504 to be parity with gRPC. | ||||||||||||||||||||||||||||
| 160 | 1 | $lwp_agent->codes_to_determinate( | |||||||||||||||||||||||||||
| 161 | {HTTP_SERVICE_UNAVAILABLE => 1, HTTP_GATEWAY_TIMEOUT => 1}); | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 1 | my $proxy = $api_client->get_proxy(); | |||||||||||||||||||||||||||
| 164 | 1 | ▶ | $proxy | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | ? $lwp_agent->proxy(['http', 'https'], $proxy) | ||||||||||||||||||||||||||||
| 166 | : $lwp_agent->env_proxy; | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # Keep track of the last sent HTTP request. | ||||||||||||||||||||||||||||
| 169 | 1 | $api_client->set_last_request($http_request); | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # Send HTTP request with optional content callback handler (for search stream). | ||||||||||||||||||||||||||||
| 172 | 1 | my $http_response = undef; | |||||||||||||||||||||||||||
| 173 | 1 | ▶ | if ($content_callback) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 1 | $http_response = $lwp_agent->request($http_request, $content_callback); | |||||||||||||||||||||||||||
| 175 | # The callback handler is not invoked when the response status is error. | ||||||||||||||||||||||||||||
| 176 | 1 | ▶ | if ($http_response->is_error) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | # The error response content returned by the search stream interface is | ||||||||||||||||||||||||||||
| 178 | # enclosed in square brackets, deviating from normal GoogleAdsException. | ||||||||||||||||||||||||||||
| 179 | # Remove the leading and trailing square brackets in the response content, | ||||||||||||||||||||||||||||
| 180 | # to make it operable in the subsequent steps (logging, exception handling). | ||||||||||||||||||||||||||||
| 181 | 1 | $http_response->content($http_response->decoded_content =~ s/^\[|\]$//gr); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } else { | ||||||||||||||||||||||||||||
| 184 | 0 | $http_response = $lwp_agent->request($http_request); | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | # Keep track of the last received HTTP response. | ||||||||||||||||||||||||||||
| 188 | 1 | $api_client->set_last_response($http_response); | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 1 | my $response_content = $http_response->decoded_content(); | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | ############################################################################## | ||||||||||||||||||||||||||||
| 193 | # Step 3: Log the one-line summary and the traffic detail. Error may occur | ||||||||||||||||||||||||||||
| 194 | # when the response content is not in JSON format. | ||||||||||||||||||||||||||||
| 195 | ############################################################################## | ||||||||||||||||||||||||||||
| 196 | 1 | eval { | |||||||||||||||||||||||||||
| 197 | 1 | Google::Ads::GoogleAds::Logging::GoogleAdsLogger::log_summary($http_request, | |||||||||||||||||||||||||||
| 198 | $http_response); | ||||||||||||||||||||||||||||
| 199 | 1 | Google::Ads::GoogleAds::Logging::GoogleAdsLogger::log_detail($http_request, | |||||||||||||||||||||||||||
| 200 | $http_response); | ||||||||||||||||||||||||||||
| 201 | }; | ||||||||||||||||||||||||||||
| 202 | 1 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 0 | ▶ | $api_client->get_die_on_faults() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | ? die($response_content . "\n") | ||||||||||||||||||||||||||||
| 205 | : warn($response_content . "\n"); | ||||||||||||||||||||||||||||
| 206 | 0 | return; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | ############################################################################## | ||||||||||||||||||||||||||||
| 210 | # Step 4: Return the decoded object or exception from the response content. | ||||||||||||||||||||||||||||
| 211 | ############################################################################## | ||||||||||||||||||||||||||||
| 212 | 1 | ▶ | my $response_body = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | $response_content ? $json_coder->decode($response_content) : {}; | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 1 | ▶ | if ($http_response->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | # Bless the JSON format response to the response type class. | ||||||||||||||||||||||||||||
| 217 | 0 | ▶ | bless $response_body, $response_type if $response_type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 0 | return $response_body; | |||||||||||||||||||||||||||
| 219 | } else { | ||||||||||||||||||||||||||||
| 220 | 1 | ▶ | $api_client->get_die_on_faults() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | ? die_with_code(1, $response_content) | ||||||||||||||||||||||||||||
| 222 | : warn($response_content); | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 1 | return Google::Ads::GoogleAds::GoogleAdsException->new($response_body); | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | # Protected method to generate the appropriate REST request headers. | ||||||||||||||||||||||||||||
| 229 | 1 | ▶ | sub _get_http_headers { | ||||||||||||||||||||||||||
|
Subroutine
_get_http_headers: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 230 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 1 | my $api_client = $self->get_api_client(); | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 1 | my $headers = [ | |||||||||||||||||||||||||||
| 235 | "Content-Type", | ||||||||||||||||||||||||||||
| 236 | "application/json; charset=utf-8", | ||||||||||||||||||||||||||||
| 237 | "user-agent", | ||||||||||||||||||||||||||||
| 238 | $api_client->get_user_agent(), | ||||||||||||||||||||||||||||
| 239 | "x-goog-api-client", | ||||||||||||||||||||||||||||
| 240 | join(' ', | ||||||||||||||||||||||||||||
| 241 | Google::Ads::GoogleAds::Constants::DEFAULT_USER_AGENT, | ||||||||||||||||||||||||||||
| 242 | "gccl/" . Google::Ads::GoogleAds::BaseService->VERSION, | ||||||||||||||||||||||||||||
| 243 | "rest/" . $LWP::UserAgent::Determined::VERSION)]; | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | # Add the developer-token header if provided. | ||||||||||||||||||||||||||||
| 246 | 1 | my $developer_token = $api_client->get_developer_token(); | |||||||||||||||||||||||||||
| 247 | 1 | ▶ | push @$headers, ("developer-token", $developer_token) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | if $developer_token; | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 1 | my $login_customer_id = $api_client->get_login_customer_id(); | |||||||||||||||||||||||||||
| 251 | 1 | ▶ | push @$headers, ("login-customer-id", $login_customer_id =~ s/-//gr) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | if $login_customer_id; | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 1 | my $linked_customer_id = $api_client->get_linked_customer_id(); | |||||||||||||||||||||||||||
| 255 | 1 | ▶ | push @$headers, ("linked-customer-id", $linked_customer_id =~ s/-//gr) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | if $linked_customer_id; | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 1 | return $headers; | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 4 | 1; | |||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | =pod | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | =head1 NAME | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | Google::Ads::GoogleAds::BaseService | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | The abstract base class for all Google Ads API services, e.g. CampaignService, | ||||||||||||||||||||||||||||
| 272 | AdGroupService, etc. | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | use Google::Ads::GoogleAds::Client; | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | my $api_client = Google::Ads::GoogleAds::Client->new(); | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | my $campaign_service = $api_client->CampaignService(); | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | Each service instance is initialized by L<Google::Ads::GoogleAds::Client>, and | ||||||||||||||||||||||||||||
| 285 | these attributes are set automatically. | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | Alternatively, there is a get_ and set_ method associated with each attribute | ||||||||||||||||||||||||||||
| 288 | for retrieving or setting them dynamically. | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | my %api_client_of : ATTR(:name<api_client> :default<>); | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | =head2 api_client | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | A reference to the L<Google::Ads::GoogleAds::Client>, holding the API credentials | ||||||||||||||||||||||||||||
| 295 | and configurations. | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | =head1 METHODS | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | =head2 call | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | Sends REST HTTP requests to Google Ads API server and handles the responses. | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | =head3 Parameters | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | =over | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | =item * | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | I<http_method>: The HTTP request method, e.g. GET, POST. | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | =item * | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | I<request_path>: The relative request URL which may contain wildcards to expand, | ||||||||||||||||||||||||||||
| 314 | e.g. {+resourceName}, {+customerId}. | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | =item * | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | I<request_body>: A Perl object representing the HTTP request payload, which will | ||||||||||||||||||||||||||||
| 319 | be used to expand the {+resourceName} or any other expression in the request path | ||||||||||||||||||||||||||||
| 320 | and encoded into JSON string for a HTTP POST request. | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | =item * | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | I<response_type>: The class name of the expected response. An instance of this class | ||||||||||||||||||||||||||||
| 325 | will be returned if the request succeeds. | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | =item * | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | I<content_callback>: The optional streaming content callback method. | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | =back | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | =head3 Returns | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | An instance of the class defined by the C<response_type> parameter, or a | ||||||||||||||||||||||||||||
| 336 | L<Google::Ads::GoogleAds::GoogleAdsException> object if an error has occurred at | ||||||||||||||||||||||||||||
| 337 | the server side by default. However if the C<die_on_faults> flag is set to true | ||||||||||||||||||||||||||||
| 338 | in L<Google::Ads::GoogleAds::Client>, the service will issue a die() with error | ||||||||||||||||||||||||||||
| 339 | message on API errors. | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | =head2 _get_http_headers | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | Prepare the basic HTTP request headers including Content-Type, user-agent, | ||||||||||||||||||||||||||||
| 344 | developer-token, login-customer-id, linked-customer-id - if needed. The headers | ||||||||||||||||||||||||||||
| 345 | will be consolidated with access token in the method of | ||||||||||||||||||||||||||||
| 346 | L<Google::Ads::GoogleAds::Common::OAuth2BaseHandler/prepare_request>. | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | =head3 Returns | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | The basic HTTP headers including Content-Type, user-agent, developer-token, | ||||||||||||||||||||||||||||
| 351 | login-customer-id, linked-customer-id - if needed. | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | Copyright 2019 Google LLC | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||
| 358 | you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||
| 359 | You may obtain a copy of the License at | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||
| 364 | distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||
| 365 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||
| 366 | See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||
| 367 | limitations under the License. | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | =head1 REPOSITORY INFORMATION | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | $Rev: $ | ||||||||||||||||||||||||||||
| 372 | $LastChangedBy: $ | ||||||||||||||||||||||||||||
| 373 | $Id: $ | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | =cut | ||||||||||||||||||||||||||||