GOOGLE-ADWORDS-PERL-CLIENT

 view release on metacpan or  search on metacpan

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

          $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 } )
          }
        );

        # Injecting our own transport.
        $service_port->set_transport( $self->get_transport() );

        if ( $ENV{HTTP_PROXY} ) {
          $service_port->get_transport()->proxy( ['http'], $ENV{HTTP_PROXY} );
        }
        if ( $ENV{HTTPS_PROXY} ) {
          $service_port->get_transport()->proxy( ['https'], $ENV{HTTPS_PROXY} );
        }

        $self->get_services()->{$method_name} = $service_port;
        return sub {
          return $self->get_services()->{$method_name};
        };
      }
    }
  }
}

# Protected method to retrieve the proper enabled authorization handler.
sub _get_auth_handler {
  my $self = shift;

  # Check if we have cached the enabled auth_handler.
  if ( $self->get___enabled_auth_handler() ) {
    return $self->get___enabled_auth_handler();
  }

  my $auth_handlers = $self->get_auth_handlers();

  foreach my $handler_id (AUTH_HANDLERS_ORDER) {
    if ( $auth_handlers->{$handler_id}->is_auth_enabled() ) {
      $self->set___enabled_auth_handler( $auth_handlers->{$handler_id} );
      last;
    }
  }

  return $self->get___enabled_auth_handler();
}

# Private method to setup IO::Socket::SSL and Crypt::SSLeay variables
# for certificate and hostname validation.
sub __setup_SSL {
  my ( $self, $ca_path, $ca_file ) = @_;
  if ( $ca_path || $ca_file ) {
    $ENV{HTTPS_CA_DIR}  = $ca_path;
    $ENV{HTTPS_CA_FILE} = $ca_file;
    eval {
      require IO::Socket::SSL;
      require Net::SSLeay;
      IO::Socket::SSL::set_ctx_defaults(
        verify_mode         => Net::SSLeay->VERIFY_PEER(),
        SSL_verifycn_scheme => "www",
        ca_file             => $ca_file,
        ca_path             => $ca_path
      );
    };
  }
}

# Private method to parse values in a properties file.
sub __parse_properties_file {
  my ($properties_file) = @_;
  my %properties;

  # glob() to expand any metacharacters.
  ($properties_file) = glob($properties_file);

  if ( open( PROP_FILE, $properties_file ) ) {

    # The data in the file should be in the following format:
    #   key1=value1
    #   key2=value2
    while ( my $line = <PROP_FILE> ) {
      chomp($line);

      # Skip comments.
      next if ( $line =~ /^#/ || $line =~ /^\s*$/ );
      my ( $key, $value ) = split( /=/, $line, 2 );
      $properties{$key} = $value;
    }
    close(PROP_FILE);
  }
  else {
    die("Couldn't open properties file $properties_file for reading: $!\n");
  }
  return %properties;
}

# Protected method to generate the appropriate SOAP request header.
sub _get_header {
  my ($self) = @_;

  # Always prepend the module identifier to the user agent.
  my $user_agent = sprintf(
    "%s (AwApi-Perl/%s, Common-Perl/%s, SOAP-WSDL/%s, "
      . "libwww-perl/%s, perl/%s)",
    $self->get_user_agent() || $0,
    ${Google::Ads::AdWords::Constants::VERSION},
    ${Google::Ads::Common::Constants::VERSION},
    ${SOAP::WSDL::VERSION},
    ${LWP::UserAgent::VERSION},
    $]
  );

  my $headers = {
    userAgent      => $user_agent,
    developerToken => $self->get_developer_token(),
    validateOnly   => $self->get_validate_only(),
    partialFailure => $self->get_partial_failure()
  };

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

=head2 get_die_on_faults

=head3 Returns

A true or false value indicating whether the Google::Ads::AdWords::Client
instance is set to die() on SOAP faults.

=head2 {ServiceName}

The client object contains a method for every service provided by the API.
So for example it can invoked as $client->AdGroupService() and it will return
an object of type
L<Google::Ads::AdWords::v201402::AdGroupService::AdGroupServiceInterfacePort>
when using version v201402 of the API.
For a list of all available services please refer to
http://developers.google.com/adwords/api/docs and for examples on
how to invoke the services please refer to scripts in the examples folder.

=head2 get_oauth_2_applications_handler

Returns the OAuth2 for Web/Installed applications authorization handler
attached to the client, for programmatically setting/overriding its properties.

$client->get_oauth_2_applications_handler()->set_client_id('client-id');
$client->get_oauth_2_applications_handler()->set_client_secret('client-secret');
$client->get_oauth_2_applications_handler()->set_access_token('access-token');
$client->get_oauth_2_applications_handler()->set_refresh_token('refresh-token');
$client->get_oauth_2_applications_handler()->set_access_type('access-type');
$client->get_oauth_2_applications_handler()->set_approval_prompt('prompt');
$client->get_oauth_2_applications_handler()->set_redirect_uri('redirect-url');

Refer to L<Google::Ads::AdWords::OAuth2ApplicationsHandler> for more details.

=head2 get_oauth_2_service_accounts_handler

Returns the OAuth2 authorization handler attached to the client, for
programmatically setting/overriding its specific properties.

$client->get_oauth_2_service_accounts_handler()->set_client_id('email');
$client->get_oauth_2_service_accounts_handler()->set_email_address('email');
$client->get_oauth_2_service_accounts_handler()->
    set_delegated_email_address('delegated-email');
$client->get_oauth_2_service_accounts_handler()->
    set_pem_file('path-to-certificate');
$client->get_oauth_2_service_accounts_handler()->set_display_name('email');

Refer to L<Google::Ads::AdWords::OAuth2ApplicationsHandler> for more details.

=head2 get_auth_token_handler

Returns the ClientLogin authorization handler attached to the client, for
manully setting its specific properties.

$client->get_auth_token_handler()->set_email('email');
$client->get_auth_token_handler()->set_password('email');

Refer to L<Google::Ads::AdWords::AuthTokenHandler> for more details.

=head2 __setup_SSL (Private)

Setups IO::Socket::SSL and Crypt::SSLeay enviroment variables to work with
SSL certificate validation.

=head3 Parameters

The path to the certificate authorites folder and the path to the certificate
authorites file. Either can be null.

=head3 Returns

Nothing.

=head2 __parse_properties_file (Private)

=head3 Parameters

The path to a properties file on disk. The data in the file should be in the
following format:

 key1=value1
 key2=value2

=head3 Returns

A hash corresponding to the keys and values in the properties file.

=head3 Exceptions

die()s with an error message if the properties file could not be read.

=head2 _get_header (Protected)

Used by the L<Google::Ads::AdWords::Serializer> class to get a valid request
header corresponding to the current credentials in this
Google::Ads::AdWords::Client instance.

=head3 Returns

A hash reference with credentials corresponding to the values needed to be
included in the request header.

=head2 _auth_handler (Protected)

Retrieves the active AuthHandler. All handlers are checked in the order
OAuth2 Applications -> OAuth2 Service Accounts -> AuthToken, given preference of
OAuth2 over AuthToken.

=head3 Returns

An implementation of L<Google::Ads::Common::AuthHandlerInterface>.

=head1 LICENSE AND COPYRIGHT

Copyright 2011 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0



( run in 0.681 second using v1.01-cache-2.11-cpan-39bf76dae61 )