File Coverage

blib/lib/Google/Ads/AdWords/Serializer.pm
Criterion Covered Total %
statement 33 33 100.0
branch n/a
condition n/a
subroutine 11 11 100.0
pod n/a
total 44 44 100.0


line stmt bran cond sub pod time code
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::Serializer;
16              
17 2     2   903 use strict;
  2         4  
  2         55  
18 2     2   10 use warnings;
  2         5  
  2         49  
19 2     2   9 use utf8;
  2         3  
  2         13  
20 2     2   56 use version;
  2         36  
  2         16  
21              
22 2     2   140 use base qw(SOAP::WSDL::Serializer::XSD);
  2         5  
  2         378  
23              
24             # The following needs to be on one line because CPAN uses a particularly hacky
25             # eval() to determine module versions.
26 2     2   1312 use Google::Ads::Common::Constants;
  2         4  
  2         40  
27 2     2   8 use Google::Ads::AdWords::Constants; our $VERSION = ${Google::Ads::AdWords::Constants::VERSION};
  2         4  
  2         72  
28 2     2   241 use Google::Ads::AdWords::Logging;
  2         4  
  2         58  
29              
30 2     2   16 use Class::Std::Fast;
  2         5  
  2         19  
31 2     2   224 use SOAP::WSDL::Factory::Serializer;
  2         5  
  2         52  
32              
33             # A list of fields that need to be scrubbed before logging due to sensitive
34             # content.
35 2     2   9 use constant SCRUBBED_FIELDS => qw(developerToken httpAuthorizationHeader);
  2         4  
  2         1043  
36              
37             # Class attributes used to hook this class with the AdWords client.
38             my %client_of : ATTR(:name :default<>);
39              
40             # Invoked by SOAP::WSDL to serialize outgoing SOAP requests.
41             sub serialize {
42             my $self = shift;
43             my $client = $self->get_client();
44             my $request = $self->SUPER::serialize(@_);
45             utf8::is_utf8 $request and utf8::encode $request;
46              
47             my $sanitized_request = __scrub_request($request);
48              
49             my $auth_handler = $client->_get_auth_handler();
50              
51             # The request will be logged when the response comes back in Deserializer.
52             $client->set_last_soap_request($sanitized_request);
53              
54             return $request;
55             }
56              
57             # Invoked by SOAP::WSDL to serialize outgoing SOAP header, AdWords header
58             # information is injected at this time.
59             sub serialize_header {
60             my $self = shift;
61             my $client = $self->get_client();
62             my $client_header = $client->_get_header();
63             my $adwords_header = $_[1];
64              
65             $adwords_header->set_clientCustomerId($client_header->{clientCustomerId});
66             $adwords_header->set_developerToken($client_header->{developerToken});
67             $adwords_header->set_userAgent($client_header->{userAgent});
68             $adwords_header->set_validateOnly($client_header->{validateOnly});
69             if ($adwords_header->can("set_partialFailure")) {
70             $adwords_header->set_partialFailure($client_header->{partialFailure});
71             }
72              
73             # Serialize the header.
74             my $header = $self->SUPER::serialize_header(@_);
75              
76             # Hack the header inner elements to correctly include the namespaces.
77             my $xmlns =
78             "https://adwords.google.com/api/adwords/cm/" . $client->get_version;
79             $header =~ s///;
80             $header =~ s///;
81             $header =~ s///;
82             $header =~ s///;
83             $header =~ s///;
84              
85             return $header;
86             }
87              
88             # Private method to redact sensitive information from requests before logging.
89             sub __scrub_request {
90             my ($request) = @_;
91             my $scrubbed_request = $request;
92             foreach my $header (SCRUBBED_FIELDS) {
93             $scrubbed_request =~
94             s!<$header([^>]*)>.+?!<$header$1>REDACTED!;
95             }
96             return $scrubbed_request;
97             }
98              
99             return 1;
100              
101             =pod
102              
103             =head1 NAME
104              
105             Google::Ads::AdWords::Serializer
106              
107             =head1 DESCRIPTION
108              
109             Google::Ads::AdWords::Deserializer extends the
110             L module. Above the
111             normal functionality of
112             L, this module
113             implements hooks into
114             L to inject AdWords
115             API Header parameters as well as hooks into
116             L to log SOAP
117             request XML.
118              
119             =head1 METHODS
120              
121             =head2 serialize
122              
123             A method automatically invoked by SOAP::WSDL when an outgoing request needs to
124             be serialized into SOAP XML. SOAP XML request is logged by this method.
125              
126             =head3 Parameters
127              
128             The SOAP request.
129              
130             =head3 Returns
131              
132             The SOAP XML string representing the request.
133              
134             =head2 serialize_header
135              
136             A method automatically invoked by SOAP::WSDL when an outgoing request header
137             needs to be serialized into SOAP XML. At this time API headers are injected in
138             the message.
139              
140             =head3 Parameters
141              
142             The SOAP request.
143              
144             =head3 Returns
145              
146             The SOAP XML string representing the request.
147              
148             =head2 __scrub_request (Private)
149              
150             Scrubs sensitive information from the request before it's logged.
151              
152             =head3 Parameters
153              
154             The serialized SOAP request XML string.
155              
156             =head3 Returns
157              
158             A redacted version of the string.
159              
160             =head1 LICENSE AND COPYRIGHT
161              
162             Copyright 2011 Google Inc.
163              
164             Licensed under the Apache License, Version 2.0 (the "License");
165             you may not use this file except in compliance with the License.
166             You may obtain a copy of the License at
167              
168             http://www.apache.org/licenses/LICENSE-2.0
169              
170             Unless required by applicable law or agreed to in writing, software
171             distributed under the License is distributed on an "AS IS" BASIS,
172             WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
173             See the License for the specific language governing permissions and
174             limitations under the License.
175              
176             =head1 REPOSITORY INFORMATION
177              
178             $Rev: $
179             $LastChangedBy: $
180             $Id: $
181              
182             =cut