Google-Ads-AdWords-Client

 view release on metacpan or  search on metacpan

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

        # Injecting our own transport.

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

use constant DEFAULT_VALIDATE_ONLY => "false";

# Default OAuth scope for AdWords
use constant DEFAULT_OAUTH_SCOPE => "https://www.googleapis.com/auth/adwords";

# 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 = (
  AccountLabelService             => "mcm",
  AdCustomizerFeedService         => "cm",
  AdGroupAdService                => "cm",
  AdGroupBidModifierService       => "cm",
  AdGroupCriterionService         => "cm",
  AdGroupExtensionSettingService  => "cm",
  AdGroupFeedService              => "cm",
  AdGroupService                  => "cm",
  AdParamService                  => "cm",

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

to a given request.

=head1 ATTRIBUTES

=head2 client_id

The client id against which the call was made if available.

=head2 server

The server endpoint.

=head2 service_name

The name of the service that was called.

=head2 method_name

The method name of the service that was called.

=head2 response_time

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

    my $client_user_agent = $client->get_user_agent() || "";
    $self->{_user_agent} =
      $client_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 {



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