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