GOOGLE-ADWORDS-PERL-CLIENT

 view release on metacpan or  search on metacpan

lib/Google/Ads/AdWords/AuthTokenHandler.pm  view on Meta::CPAN

use base qw(Google::Ads::Common::AuthTokenHandler);

# The following needs to be on one line because CPAN uses a particularly hacky
# eval() to determine module versions.
use Google::Ads::AdWords::Constants; our $VERSION = ${Google::Ads::AdWords::Constants::VERSION};

use Class::Std::Fast;

# Class methods from Google::Ads::Common::AuthTokenHandler
sub prepare_request {
  my ($self, $endpoint, $http_headers, $envelope) = @_;

  my $version = $self->get_api_client()->get_version();

  if ($version gt Google::Ads::AdWords::Constants::LAST_SUPPORTED_CLIENT_LOGIN_VERSION) {
      my $message = "ClientLogin is not supported in " . $version .
          " of the AdWords API. Please refer to the ClientLogin to OAuth2" .
          " migration guide at" .
          " https://developers.google.com/adwords/api/docs/guides/clientlogin-to-oauth2-migration-guide" .
          " for more information.";
      $self->get_api_client()->get_die_on_faults() ?
          die($message) :
          warn($message);
  }

  my $xmlns = "https://adwords.google.com/api/adwords/cm/" . $version;
  my $header = "<authToken xmlns=\"$xmlns\">" . $self->__get_auth_token() .
      "</authToken>";

  $envelope =~ s/(<RequestHeader [^>]+>)/$1${header}/;

  return HTTP::Request->new('POST', $endpoint, $http_headers, $envelope);
}

sub _service {
  return "adwords";
}

1;

=pod

lib/Google/Ads/AdWords/Client.pm  view on Meta::CPAN

      # Google::Ads::AdWords::$version if not we warn and return nothing.
      my $module_name = "Google::Ads::AdWords::${version}::${method_name}"
        . "::${method_name}InterfacePort";
      eval("require $module_name");
      if ($@) {
        warn("Module $module_name was not found.");
        return;
      }
      else {

        # Generating the service endpoint url of the form
        # https://{server_url}/{group_name(cm/job/info/o)}/{version}/{service}.
        my $server_url =
          $self->get_alternate_url() =~ /\/$/
          ? substr( $self->get_alternate_url(), 0, -1 )
          : $self->get_alternate_url();
        my $service_to_group_name =
          $Google::Ads::AdWords::Constants::SERVICE_TO_GROUP{$method_name};
        if ( !$service_to_group_name ) {
          die(
            "Service " . $method_name . " is not configured in the library." );
        }
        my $endpoint_url =
          sprintf( Google::Ads::AdWords::Constants::PROXY_FORMAT_STRING,
          $server_url, $service_to_group_name, $self->get_version(),
          $method_name );

        # If a suitable module is found, instantiate it and store it in
        # instance-specific storage to emulate a singleton.
        my $service_port = $module_name->new(
          {

            # Setting the server endpoint of the service.
            proxy => [$endpoint_url],

            # Associating our custom serializer.
            serializer =>
              Google::Ads::AdWords::Serializer->new( { client => $self } ),

            # Associating our custom deserializer.
            deserializer =>
              Google::Ads::AdWords::Deserializer->new( { client => $self } )
          }
        );

lib/Google/Ads/AdWords/Constants.pm  view on Meta::CPAN

use constant DEFAULT_ALTERNATE_URL => "https://adwords.google.com";

# Default validation header value passed to the servers.
use constant DEFAULT_VALIDATE_ONLY => "false";

# Maximum number of request stats to keep in memory, any overflow will result
# on droppping older requests stats out of memory.
use constant MAX_NUM_OF_REQUEST_STATS => 500;

# Mapping of services to namespace group, required to figure out the service
# url endpoints.
our %SERVICE_TO_GROUP = (
  AdExtensionOverrideService => "cm",
  AdGroupAdService => "cm",
  AdGroupBidModifierService => "cm",
  AdGroupCriterionService => "cm",
  AdGroupFeedService => "cm",
  AdGroupService => "cm",
  AdParamService => "cm",
  AdwordsUserListService => "rm",
  AlertService => "mcm",

lib/Google/Ads/Common/AuthHandlerInterface.pm  view on Meta::CPAN

# Initializes the handler with a given set of properties and the API client
# object.
sub initialize {
  my ($self, $api_client, $properties) = @_;
  die "Needs to be implemented by subclass";
}

# Method that given an HTTP:Request prepares it with the relevant
# authorization data (i.e. headers, protected resource url, etc).
sub prepare_request {
  my ($self, $endpoint, $http_headers, $envelope) = @_;
  die "Needs to be implemented by subclass";
}

# Returns true if the handler can prepare request with the appropiate
# authorization info.
sub is_auth_enabled {
  my ($self) = @_;
  die "Needs to be implemented by subclass";
}

lib/Google/Ads/Common/AuthHandlerInterface.pm  view on Meta::CPAN


Constructs a L<HTTP::Request> valid to send an authorized request to the API.
Implementors will attach authorization headers to the request at this phase.

=head3 Parameters

=over

=item *

I<endpoint>: URL to the resource to access.

=item *

I<http_headers>: a map of HTTP headers to be included in the request.

=item *

I<envelope>: a string with the payload to be send in the request.

=back

lib/Google/Ads/Common/HTTPTransport.pm  view on Meta::CPAN

    $self->default_header('Accept-Encoding' => scalar $can_accept);
    $self->{_user_agent} = $client->get_user_agent() .
         ($can_accept =~ /gzip/i ? " gzip" : "");
  }

  return $self->{_client};
}

sub send_receive {
  my ($self, %parameters) = @_;
  my ($envelope, $soap_action, $endpoint, $encoding, $content_type) =
      @parameters{qw(envelope action endpoint encoding content_type)};

  my $auth_handler = $self->client->_get_auth_handler();

  if (!$auth_handler) {
    $self->{_client}->get_die_on_faults() ?
        die(Google::Ads::Common::Constants::NO_AUTH_HANDLER_IS_SETUP_MESSAGE) :
        warn(Google::Ads::Common::Constants::NO_AUTH_HANDLER_IS_SETUP_MESSAGE);
    return;
  }

  # Overriding the default LWP user agent.
  $self->agent($self->{_user_agent});

  $encoding = defined($encoding) ? lc($encoding) : 'utf-8';

  $content_type = "text/xml; charset=$encoding"
      if not defined($content_type);

  my $headers = ["Content-Type", "$content_type", "SOAPAction", $soap_action];
  my $request = $auth_handler->prepare_request($endpoint, $headers, $envelope);
  my $response = $self->request( $request );

  $self->code( $response->code);
  $self->message( $response->message);
  $self->is_success($response->is_success);
  $self->status($response->status_line);

  return $response->decoded_content();
}

lib/Google/Ads/Common/HTTPTransport.pm  view on Meta::CPAN

=head2 client

Holds an instance of the API client, which will use to retrieve the current
L<Google::Ads::Common::AuthHandlerInterface>.

=head1 METHODS

=head2 send_receive

Overrides L<SOAP::WSDL::Transport::HTTP> send_receive method to change the
endpoint in case OAuth is enabled, uses the L<Google::Ads::AdWords::Client>
configured OAuth handler to leverage all the OAuth signing logic.

=head3 Parameters

The same as the L<SOAP::WSDL::Transport::HTTP> send_receive method.

=head1 LICENSE AND COPYRIGHT

Copyright 2011 Google Inc.

lib/Google/Ads/Common/OAuth2BaseHandler.pm  view on Meta::CPAN

  my $ident = ident $self;

  $api_client_of{$ident} = $api_client;
  $client_id_of{$ident} = $properties->{oAuth2ClientId} ||
      $client_id_of{$ident};
  $access_token_of{$ident} = $properties->{oAuth2AccessToken} ||
      $access_token_of{$ident};
}

sub prepare_request {
  my ($self, $endpoint, $http_headers, $envelope) = @_;

  my $access_token = $self->get_access_token();

  if (!$access_token) {
    my $api_client = $self->get_api_client();
    my $err_msg = "Unable to prepare a request, authorization info is " .
      "incomplete or invalid.";
    $api_client->get_die_on_faults() ? die($err_msg) : warn($err_msg);
    return;
  }

  push @{$http_headers}, ("Authorization", "Bearer ${access_token}");

  return HTTP::Request->new('POST', $endpoint, $http_headers, $envelope);
}

sub is_auth_enabled {
  my ($self) = @_;

  return $self->get_access_token();
}

# Custom getters and setters for the access token with logic to auto-refresh.
sub get_access_token {

lib/Google/Ads/Common/ReportUtils.pm  view on Meta::CPAN

=over

=item *

The report_definition parameter is either:
  - the id of a pre-defined report to download
  - a C<ReportDefinition> object to be defined and download on the fly
  - a hash with an AWQL query and format. i.e. { query => 'query',
    format => 'format' }

In the case of a plain id then the regular download endpoint will be used to
download a pre-stored definition, otherwise the versioned download url endpoint
(based on the version of the given C<Client> object) will be used.

=item *

The client parameter is an instance of a valid L<Google::AdWords::Client>.

=item *

The file_path is an optional parameter that if given the subroutine will write
out the report to the given file path.



( run in 1.586 second using v1.01-cache-2.11-cpan-2b1a40005be )