File Coverage

blib/lib/Google/Ads/GoogleAds/Utils/SearchStreamHandler.pm
Criterion Covered Total %
statement 37 38 97.3
branch 2 6 33.3
condition 1 3 33.3
subroutine 11 11 100.0
pod 1 2 50.0
total 52 60 86.6


line stmt bran cond sub pod time code
1             # Copyright 2020, 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 handler class to process the response of stream search.
16              
17              
18             use strict;
19 2     2   93670 use warnings;
  2         5  
  2         43  
20 2     2   9 use version;
  2         2  
  2         52  
21 2     2   9  
  2         3  
  2         13  
22             # The following needs to be on one line because CPAN uses a particularly hacky
23             # eval() to determine module versions.
24             use Google::Ads::GoogleAds::Constants; our $VERSION = ${Google::Ads::GoogleAds::Constants::VERSION};
25 2     2   122  
  2         4  
  2         67  
26             use Class::Std::Fast;
27 2     2   9 use JSON::SL;
  2         4  
  2         24  
28 2     2   1218  
  2         1492  
  2         85  
29             use constant JSON_POINTER_RESULTS => "/^/results";
30 2     2   11 use constant JSON_POINTER_REQUEST_ID => "/^/requestId";
  2         4  
  2         119  
31 2     2   8  
  2         4  
  2         549  
32             # Class::Std-style attributes. Most values are read from googleads.properties file.
33             # These need to go in the same line for older Perl interpreters to understand.
34             my %service_of : ATTR(:name<service> :default<>);
35             my %request_of : ATTR(:name<request> :default<>);
36             my %json_sl_of : ATTR(:name<json_sl> :default<>);
37              
38             # Automatically called by Class::Std after the values for all the attributes
39             # have been populated but before the constructor returns the new object.
40             my ($self, $ident) = @_;
41              
42 2     2 0 261 $json_sl_of{$ident} ||= JSON::SL->new();
43             # Set the query path to "results" for the JSON::SL object.
44 2   33     105 $json_sl_of{$ident}
45             ->set_jsonpointer([JSON_POINTER_RESULTS, JSON_POINTER_REQUEST_ID]);
46 2         27 }
47              
48             # Processes the response content of stream search.
49             # The $for_each_callback is invoked as a subroutine reference for each parsed GoogleAdsRow.
50             my ($self, $for_each_callback) = @_;
51              
52             $self->get_service()->search_stream(
53 2     2 1 1151 $self->get_request(),
54             # The $content_callback subroutine defined below takes two arguments:
55             # $data - the chunk of data
56             # $response - the HTTP::Response
57             sub {
58             my ($data, $response) = @_;
59              
60             my $json_sl = $self->get_json_sl();
61 1     1   2204 # Append the returned data chunk to the JSON input stream.
62             $json_sl->feed($data);
63 1         8  
64             # The fetch() method returns the remaining decoded JSON objects specified
65 1         64 # by the JSON pointers.
66             while (my $fetched = $json_sl->fetch()) {
67             if ($fetched->{JSONPointer} eq JSON_POINTER_RESULTS) {
68             foreach my $google_ads_row (@{$fetched->{Value}}) {
69 1         8 # Call the $for_each_callback subroutine for each parsed row.
70 1 50       6 $for_each_callback->($google_ads_row) if $for_each_callback;
    0          
71 1         2 }
  1         4  
72             } elsif ($fetched->{JSONPointer} eq JSON_POINTER_REQUEST_ID) {
73 8 50       5836 # Add the request id from the JSON payload to the HTTP response header
74             # to be logged properly.
75             $response->header("request-id" => $fetched->{Value});
76             }
77             }
78 0           });
79             }
80              
81 2         13 1;
82              
83             =pod
84              
85             =head1 NAME
86              
87             Google::Ads::GoogleAds::Utils::SearchStreamHandler
88              
89             =head1 SYNOPSIS
90              
91             my $search_stream_request =
92             Google::Ads::GoogleAds::V11::Services::GoogleAdsService::SearchGoogleAdsStreamRequest
93             ->new({
94             customerId => $customer_id,
95             query => "SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id"
96             });
97              
98             my $google_ads_service = $api_client->GoogleAdsService();
99              
100             my $search_stream_handler = Google::Ads::GoogleAds::Utils::SearchStreamHandler->new({
101             service => $google_ads_service,
102             request => $search_stream_request
103             });
104              
105             $search_stream_handler->process_contents(
106             sub {
107             my $google_ads_row = shift;
108             # Do something with each GooglerAdsRow object.
109             });
110              
111             =head1 DESCRIPTION
112              
113             The handler class to process the response of stream search. The handler should be
114             constructed with a L<Google::Ads::GoogleAds::V11::Services::GoogleAdsService> and a
115             L<Google::Ads::GoogleAds::V11::Services::GoogleAdsService::SearchGoogleAdsStreamRequest>.
116              
117             my $search_stream_handler = Google::Ads::GoogleAds::Utils::SearchStreamHandler->new({
118             service => $google_ads_service,
119             request => $search_stream_request
120             });
121              
122             =head1 METHODS
123              
124             =head2 process_contents
125              
126             Processes the response content of stream search.
127              
128             =head3 Parameters
129              
130             =over
131              
132             =item *
133              
134             I<for_each_callback>: The callback subroutine which is invoked to process each
135             parsed L<Google::Ads::GoogleAds::V11::Services::GoogleAdsService::GoogleAdsRow>.
136              
137             =back
138              
139             =head1 LICENSE AND COPYRIGHT
140              
141             Copyright 2020 Google LLC
142              
143             Licensed under the Apache License, Version 2.0 (the "License");
144             you may not use this file except in compliance with the License.
145             You may obtain a copy of the License at
146              
147             http://www.apache.org/licenses/LICENSE-2.0
148              
149             Unless required by applicable law or agreed to in writing, software
150             distributed under the License is distributed on an "AS IS" BASIS,
151             WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
152             See the License for the specific language governing permissions and
153             limitations under the License.
154              
155             =head1 REPOSITORY INFORMATION
156              
157             $Rev: $
158             $LastChangedBy: $
159             $Id: $
160              
161             =cut