lib/Google/Ads/AdWords/Deserializer.pm

filter: Statementstmt 100% 18 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 100% 24 / 24 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Google/Ads/AdWords/Constants.pm ↑ Summary lib/Google/Ads/AdWords/Reports/ReportingConfiguration.pm »
1 # Copyright 2011, 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::AdWords::Deserializer;
16  
17 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 15 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 15 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 15 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 15 ▶use base qw(SOAP::WSDL::Deserializer::XSD);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # The following needs to be on one line because CPAN uses a particularly hacky
26 # eval() to determine module versions.
27 use Google::Ads::AdWords::Constants; our $VERSION = ${Google::Ads::AdWords::Constants::VERSION};
28 use Google::Ads::AdWords::Logging;
29 use Google::Ads::AdWords::RequestStats;
30 use Google::Ads::SOAP::Deserializer::MessageParser;
31  
32 # Class attributes used to hook this class with the AdWords client
33 my %client_of : ATTR(:name<client> :default<>);
34  
35 # Invoked by SOAP::WSDL to deserialize incoming SOAP responses.
36 sub deserialize {
37 my $self = shift;
38 my ($response_xml) = @_;
39 my $client = $self->get_client();
40 utf8::is_utf8 $response_xml and utf8::encode $response_xml;
41  
42 my $request_message =
43 sprintf("Outgoing request:\n%s", $client->get_last_soap_request());
44 my $response_message = sprintf("Incoming response:\n%s", $response_xml);
45 $client->set_last_soap_response($response_xml);
46  
47 my $response_header =
48 $self->__get_element_content($response_xml, "ResponseHeader");
49  
50 my $request_stats;
51 if ($response_header) {
52 my $request_id =
53 $self->__get_element_content($response_header, "requestId");
54 my $service_name =
55 $self->__get_element_content($response_header, "serviceName");
56 my $method_name =
57 $self->__get_element_content($response_header, "methodName");
58 my $operations =
59 $self->__get_element_content($response_header, "operations");
60 my $response_time =
61 $self->__get_element_content($response_header, "responseTime");
62  
63 my $auth_handler = $client->_get_auth_handler();
64 $request_stats = Google::Ads::AdWords::RequestStats->new({
65 client_id => $client->get_client_id(),
66 server => $client->get_alternate_url(),
67 service_name => $service_name,
68 method_name => $method_name,
69 response_time => $response_time,
70 request_id => $request_id,
71 operations => $operations
72 });
73 }
74  
75 $response_xml =~ s!<soap:Header>.*</soap:Header>!<soap:Header></soap:Header>!;
76 my @response = $self->_deserialize($response_xml);
77 my $is_fault = $response[0]->isa("SOAP::WSDL::SOAP::Typelib::Fault");
78  
79 if ($request_stats) {
80 $request_stats->set_is_fault($is_fault);
81 if ($is_fault) {
82 $request_stats->set_fault_message($response[0]->get_faultstring());
83 Google::Ads::AdWords::Logging::get_awapi_logger->logwarn($request_stats);
84 } else {
85 Google::Ads::AdWords::Logging::get_awapi_logger->info($request_stats);
86 }
87 $client->_push_new_request_stats($request_stats);
88 }
89  
90 if ($is_fault) {
91 Google::Ads::AdWords::Logging::get_soap_logger->info($request_message);
92 Google::Ads::AdWords::Logging::get_soap_logger->info($response_message);
93 if ($self->get_client->get_die_on_faults) {
94 die(
95 sprintf("A fault was returned by the server:\n%s\n",
96 $response[0]->get_faultstring()));
97 }
98 } else {
99 Google::Ads::AdWords::Logging::get_soap_logger->debug($request_message);
100 Google::Ads::AdWords::Logging::get_soap_logger->debug($response_message);
101 }
102  
103 # Unwrapping the response if contains an rval no value for the user to see the
104 # outer response object.
105 if ($response[0] && $response[0]->can('get_rval')) {
106 $response[0] = $response[0]->get_rval();
107 }
108  
109 return @response;
110 }
111  
112 sub _deserialize {
113 my ($self, $content) = @_;
114  
115 my $parser = Google::Ads::SOAP::Deserializer::MessageParser->new({
116 strict => $self->get_strict()
117 });
118 $parser->class_resolver($self->get_class_resolver());
119 eval { $parser->parse_string($content, $self->get_client()) };
120 if ($@) {
121 return $self->generate_fault({
122 code => 'SOAP-ENV:Server',
123 role => 'urn:localhost',
124 message => "Error deserializing message: $@. \n" .
125 "Message was: \n$content"
126 });
127 }
128 return ($parser->get_data(), $parser->get_header());
129 }
130  
131 # Retrieves the content of a given XML element.
132 sub __get_element_content {
133 my ($self, $xml, $element_name) = @_;
134  
135 my $regex =
136 "<(?:[^:>]+:)?${element_name}(?:\\s[^>]*)?>(.+?)" .
137 "</(?:[^:>]+:)?${element_name}(?:\\s[^>]*)?>";
138  
139 $xml =~ /$regex/ and my $content = $1;
140  
141 return $content;
142 }
143  
144 # Invoked by SOAP::WSDL when deserialize die()s.
145 sub generate_fault {
146 my ($self, $args) = @_;
147 Google::Ads::AdWords::Logging::get_soap_logger->info($args->{message});
148 die($args->{message});
149 }
150  
151 return 1;
152  
153 =pod
154  
155 =head1 NAME
156  
157 Google::Ads::AdWords::Deserializer
158  
159 =head1 DESCRIPTION
160  
161 Google::Ads::AdWords::Deserializer extends the
162 L<SOAP::WSDL::Deserializer::XSD|SOAP::WSDL::Deserializer::XSD> module.
163 The default deserializer used by <SOAP::WSDL|SOAP::WSDL>.
164 Above the normal functionality of
165 L<SOAP::WSDL::Deserializer::XSD|SOAP::WSDL::Deserializer::XSD>,
166 this module implements hooks into
167 L<Google::Ads::AdWords::Logging|Google::Ads::AdWords::Logging> to simplify
168 logging and keeping track of all the information in the AdWords API response
169 headers.
170  
171 =head1 METHODS
172  
173 =head2 deserialize
174  
175 A method automatically invoked by SOAP::WSDL when an incoming SOAP XML response
176 needs to be deserialized.
177  
178 =head3 Parameters
179  
180 The SOAP XML response string.
181  
182 =head3 Returns
183  
184 A L<SOAP::WSDL|SOAP::WSDL> object representing the SOAP response. Most of the
185 API calls return their result wrapped within an <rval> tag, that gets unwrapped
186 and the inner object is returned instead in those cases.
187  
188 =head3 Exceptions
189  
190 If deserialization fails or the SOAP response contains a
191 L<SOAP::WSDL::SOAP::Typelib::Fault11> object and
192 Google::Ads::AdWords::Client::get_die_on_faults() is set to true, then a die
193 call is triggered.
194  
195 If L<Google::Ads::AdWords::Client::get_die_on_faults()> is set to false then the
196 deserialized message will be passed back, containing a
197 L<SOAP::WSDL::SOAP::Typelib::Fault11> object.
198  
199 =head2 generate_fault
200  
201 A method automatically invoked on deserialization if an error occurred and
202 L<Google::Ads::AdWords::Client::get_die_on_faults()> is set to true.
203  
204 =head3 Parameters
205  
206 A L<SOAP::WSDL::SOAP::Typelib::Fault11> object.
207  
208 =head3 Exceptions
209  
210 Always die()s with the value of the input parameter's message.
211  
212 =head1 LICENSE AND COPYRIGHT
213  
214 Copyright 2011 Google Inc.
215  
216 Licensed under the Apache License, Version 2.0 (the "License");
217 you may not use this file except in compliance with the License.
218 You may obtain a copy of the License at
219  
220 http://www.apache.org/licenses/LICENSE-2.0
221  
222 Unless required by applicable law or agreed to in writing, software
223 distributed under the License is distributed on an "AS IS" BASIS,
224 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
225 See the License for the specific language governing permissions and
226 limitations under the License.
227  
228 =head1 REPOSITORY INFORMATION
229  
230 $Rev: $
231 $LastChangedBy: $
232 $Id: $
233  
234 =cut
« lib/Google/Ads/AdWords/Constants.pm ↑ Summary lib/Google/Ads/AdWords/Reports/ReportingConfiguration.pm »