API-Handle

 view release on metacpan or  search on metacpan

lib/API/Handle/Google/DFP.pm  view on Meta::CPAN

package API::Handle::Google::DFP;
{
  $API::Handle::Google::DFP::VERSION = '0.02';
}
use Moose;
use namespace::autoclean;
use HTTP::Request;
use Carp;
use Data::Dumper;
use feature ':5.10';

with 'API::Handle';
has _config => (
	is => 'rw'
	, isa => 'Nour::Config'
	, handles => {
		  config => 'config'
		, merge_config => 'merge'
		, write_config => 'write'
	}
	, required => 1
	, lazy => 1
	, default => sub {
		my $self = shift;
		require Nour::Config;
		 return new Nour::Config (
			 -base => 'config/google/dfp'
		 );
	}
);

# This is where we configure how the user-agent transforms
# outgoing and incoming requests and responses.
# perldoc LWP::UserAgent.

around BUILD => sub {
	my ( $next, $self, @args, $prev ) = @_;

	# Put code that pre-empts API::Handle::BUILD before this $prev line.
	$prev = $self->$next( @args );
	# Put code that depends on API::Handle::BUILD after this $prev line.

	my $conf = $self->config;
	my $time = time;

	# Uncomment this to view loaded configuration.
	# $self->dumper( 'config', $conf );

	if ( $conf->{auth}{token}{access_token} and $conf->{auth}{token}{expires_at} and $conf->{auth}{token}{expires_at} > $time ) {
		$self->ua->default_header( 'Authorization' => "$conf->{auth}{token}{token_type} $conf->{auth}{token}{access_token}" );
	}
	elsif ( $conf->{auth}{token}{refresh_token} ) {
		my $req = new HTTP::Request;

		$req->headers->header( 'Content-Type' => 'application/x-www-form-urlencoded' );
		$req->method( 'POST' );
		$req->uri( $conf->{auth}{uri}{token} );

		my $data = $self->_encode( type => 'form', data => {
			grant_type => 'refresh_token'
			, client_id => $conf->{auth}{client}{id}
			, client_secret => $conf->{auth}{client}{secret}
			, refresh_token => $conf->{auth}{token}{refresh_token}
		} );

		$req->content( $data );

		my $res = $self->ua->request( $req );

		if ( $res->code == 200 ) {
			my $data = $self->_decode( type => 'json', data => $res->content );
			$data->{expires_at} = time + $data->{expires_in};
			$self->merge_config( $conf->{auth}->{token}, $data );
			$self->write_config( 'config/google/dfp/auth/private/token.yml', $conf->{auth}->{token} );
		}
	}

	if ( $conf->{auth}{token}{access_token} and $conf->{auth}{token}{expires_at} and $conf->{auth}{token}{expires_at} > $time ) {
		$self->ua->default_header( 'Authorization' => "$conf->{auth}{token}{token_type} $conf->{auth}{token}{access_token}" );
	}
	else {
		carp 'no access token';
	}

	# Setup match-spec vars for request_prepare.
	my ( $scheme, $host, $path ) = $self->uri =~ /^(https?):\/\/([^\/]+)(\/.+)$/;

	# Add request wrapper.
	$self->ua->add_handler(
		request_prepare => sub {
			my ( $req, $ua, $h ) = @_;



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