API-Drip-Request

 view release on metacpan or  search on metacpan

lib/API/Drip/Request.pm  view on Meta::CPAN

package API::Drip::Request;

use v5.14;
use strict;
use warnings;

use Params::ValidationCompiler qw( validation_for );
use Types::Standard qw( Str HashRef CodeRef);
use YAML;
use File::Spec;
use File::HomeDir;
use Readonly;
use Carp;
use LWP::UserAgent;
use HTTP::Request::Common;
use JSON;
use URI;
use Data::Printer;

Readonly our %DEFAULTS => (
    DRIP_TOKEN => undef,
    DRIP_ID    => undef,
    DRIP_URI   => 'https://api.getdrip.com/v2',
    DRIP_AGENT => 'API::Drip',
    DRIP_DEBUG => 0,
);

=head1 NAME

API::Drip::Request - Perl interface to api.getdrip.com

=head1 VERSION

Version 0.05

=cut

our $VERSION = '0.05';

=head1 SYNOPSIS

    use API::Drip::Request;

    my $drip = API::Drip::Request->new();

    $drip->do_request( POST => 'subscribers', { subscribers => [ { email => 'foo@example.com', ... }] } );

=head1 DESCRIPTION

Low-level perl interface to the Drip API as specified at https://www.getdrip.com/docs/rest-api

All of the methods in this module will throw exceptions on error. 

=head1 SUBROUTINES/METHODS

=head2 new()

Creates the API::Drip::Request object.  See L</"CONFIGURATION"> for accepted parameters.

Also accepts: 

=over 

=item debugger

A codref that should accept a list of diagnostic strings and log them somewhere
useful for debugging purposes.  Only used when DRIP_DEBUG is true.   

=back

=cut

my $config_validator = validation_for(
    params => {
        DRIP_CLIENT_CONF => { type => Str(), optional => 1 },
        map { $_ => { type => Str(), optional => 1 } } keys %DEFAULTS,
        debugger => { type => CodeRef(), optional => 1 },
    }
);

sub new {
    my $class = shift;
    my %OPT = $config_validator->(@_);

    my $self = _load_conf( \%OPT );

    # At this point, all configuration values should be set
    foreach my $key ( keys %DEFAULTS ) {
        confess "Missing configuration $key" unless defined $self->{$key};
    }

    $self->{debugger} = _mk_debugger( $self, %OPT );

    bless $self, $class;
    return $self;
}

sub _mk_debugger {
    my ($self, %OPT)  = @_;

    unless ($self->{DRIP_DEBUG}) { return sub {}; }
    if ( $OPT{debugger} ) { return $OPT{debugger} }

    return sub { warn join "\n", map { ref($_) ? np $_ : $_ } @_ };
}

=head2 do_request

Accepts the following positional parameters:

=over



( run in 0.519 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )