lib/Google/Ads/AdWords/Deserializer.pm

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