| 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::Common::HTTPTransport; | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | use version; | ||
| 19 | use base qw(SOAP::WSDL::Transport::HTTP); | ||
| 20 | |||
| 21 | # The following needs to be on one line because CPAN uses a particularly hacky | ||
| 22 | # eval() to determine module versions. | ||
| 23 | use Google::Ads::Common::Constants; our $VERSION = ${Google::Ads::Common::Constants::VERSION}; | ||
| 24 | |||
| 25 | sub client { | ||
| 26 | my ($self, $client) = @_; | ||
| 27 | |||
| 28 | if (defined $client) { | ||
| 29 | $self->{_client} = $client; | ||
| 30 | my $can_accept = HTTP::Message::decodable; | ||
| 31 | $self->default_header('Accept-Encoding' => scalar $can_accept); | ||
| 32 | $self->{_user_agent} = $client->get_user_agent() . | ||
| 33 | ($can_accept =~ /gzip/i ? " gzip" : ""); | ||
| 34 | } | ||
| 35 | |||
| 36 | return $self->{_client}; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub send_receive { | ||
| 40 | my ($self, %parameters) = @_; | ||
| 41 | my ($envelope, $soap_action, $endpoint, $encoding, $content_type) = | ||
| 42 | @parameters{qw(envelope action endpoint encoding content_type)}; | ||
| 43 | |||
| 44 | my $auth_handler = $self->client->_get_auth_handler(); | ||
| 45 | |||
| 46 | if (!$auth_handler) { | ||
| 47 | $self->{_client}->get_die_on_faults() ? | ||
| 48 | die(Google::Ads::Common::Constants::NO_AUTH_HANDLER_IS_SETUP_MESSAGE) : | ||
| 49 | warn(Google::Ads::Common::Constants::NO_AUTH_HANDLER_IS_SETUP_MESSAGE); | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | |||
| 53 | # Overriding the default LWP user agent. | ||
| 54 | $self->agent($self->{_user_agent}); | ||
| 55 | |||
| 56 | $encoding = defined($encoding) ? lc($encoding) : 'utf-8'; | ||
| 57 | |||
| 58 | $content_type = "text/xml; charset=$encoding" | ||
| 59 | if not defined($content_type); | ||
| 60 | |||
| 61 | my $headers = ["Content-Type", "$content_type", "SOAPAction", $soap_action]; | ||
| 62 | my $request = $auth_handler->prepare_request($endpoint, $headers, $envelope); | ||
| 63 | my $response = $self->request( $request ); | ||
| 64 | |||
| 65 | $self->code( $response->code); | ||
| 66 | $self->message( $response->message); | ||
| 67 | $self->is_success($response->is_success); | ||
| 68 | $self->status($response->status_line); | ||
| 69 | |||
| 70 | return $response->decoded_content(); | ||
| 71 | } | ||
| 72 | |||
| 73 | return 1; | ||
| 74 | |||
| 75 | =pod | ||
| 76 | |||
| 77 | =head1 NAME | ||
| 78 | |||
| 79 | Google::Ads::Common::HTTPTransport - Specialization of | ||
| 80 | L<SOAP::WSDL::Transport::HTTP> transport class with added logic to handle OAuth. | ||
| 81 | |||
| 82 | =head1 DESCRIPTION | ||
| 83 | |||
| 84 | Provides a thin transport class on top of L<SOAP::WSDL::Transport::HTTP> to add | ||
| 85 | support to pluggable authorization methods. | ||
| 86 | |||
| 87 | =head1 ATTRIBUTES | ||
| 88 | |||
| 89 | =head2 client | ||
| 90 | |||
| 91 | Holds an instance of the API client, which will use to retrieve the current | ||
| 92 | L<Google::Ads::Common::AuthHandlerInterface>. | ||
| 93 | |||
| 94 | =head1 METHODS | ||
| 95 | |||
| 96 | =head2 send_receive | ||
| 97 | |||
| 98 | Overrides L<SOAP::WSDL::Transport::HTTP> send_receive method to change the | ||
| 99 | endpoint in case OAuth is enabled, uses the L<Google::Ads::AdWords::Client> | ||
| 100 | configured OAuth handler to leverage all the OAuth signing logic. | ||
| 101 | |||
| 102 | =head3 Parameters | ||
| 103 | |||
| 104 | The same as the L<SOAP::WSDL::Transport::HTTP> send_receive method. | ||
| 105 | |||
| 106 | =head1 LICENSE AND COPYRIGHT | ||
| 107 | |||
| 108 | Copyright 2011 Google Inc. | ||
| 109 | |||
| 110 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 111 | you may not use this file except in compliance with the License. | ||
| 112 | You may obtain a copy of the License at | ||
| 113 | |||
| 114 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 115 | |||
| 116 | Unless required by applicable law or agreed to in writing, software | ||
| 117 | distributed under the License is distributed on an "AS IS" BASIS, | ||
| 118 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 119 | See the License for the specific language governing permissions and | ||
| 120 | limitations under the License. | ||
| 121 | |||
| 122 | =head1 AUTHOR | ||
| 123 | |||
| 124 | David Torres E<lt>david.t at google.comE<gt> | ||
| 125 | |||
| 126 | =head1 REPOSITORY INFORMATION | ||
| 127 | |||
| 128 | $Rev: $ | ||
| 129 | $LastChangedBy: $ | ||
| 130 | $Id: $ | ||
| 131 | |||
| 132 | =cut |