API-Drip-Request

 view release on metacpan or  search on metacpan

Build.PL  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use v5.14;
use strict;
 
my $builder = Module::Build->new( module_name        => 'API::Drip::Request',
                                  license            => 'perl_5',
                                  dist_author        => q{Dan Wright <Dan@DWright.Org>},
                                  dist_version_from  => 'lib/API/Drip/Request.pm',
                                  release_status     => 'stable',
                                  configure_requires => { 'Module::Build' => '0', },
                                  build_requires     => { 'Test::More' => '0', },
                                  requires           => {
                                                'Carp'                       => 0,
                                                'Data::Printer'              => 0,
                                                'File::HomeDir'              => 0,
                                                'File::Spec'                 => 0,
                                                'HTTP::Request::Common'      => 0,
                                                'JSON'                       => 0,
                                                'LWP::UserAgent'             => 0,
                                                'Params::ValidationCompiler' => 0,
                                                'Readonly'                   => 0,
                                                'Types::Standard'            => 0,
                                                'URI'                        => 0,
                                                'YAML'                       => 0,
                                  },
                                  add_to_cleanup => ['API-Drip-*'],
                                  meta_merge     => {
                                                  'meta-spec' => { version => '2',
                                                                   url     => 'https://metacpan.org/pod/CPAN::Meta::Spec',
                                                  },
                                                  resources => { bugtracker => { web => 'https://github.com/dwright/API-Drip/issues', },
                                                                 repository => { url  => 'https://github.com/dwright/API-Drip.git',
                                                                                 web  => 'https://github.com/dwright/API-Drip',
                                                                                 type => 'git',
                                                                 }
                                                  },
                                  }, );
 
$builder->create_build_script();

META.json  view on Meta::CPAN

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"license" : [
   "perl_5"
],
"meta-spec" : {
   "version" : "2"
},
"name" : "API-Drip-Request",
"prereqs" : {
   "build" : {
      "requires" : {
         "Test::More" : "0"
      }
   },
   "configure" : {
      "requires" : {
         "Module::Build" : "0"
      }
   },
   "runtime" : {
      "requires" : {
         "Carp" : "0",
         "Data::Printer" : "0",
         "File::HomeDir" : "0",
         "File::Spec" : "0",
         "HTTP::Request::Common" : "0",
         "JSON" : "0",
         "LWP::UserAgent" : "0",
         "Params::ValidationCompiler" : "0",
         "Readonly" : "0",
         "Types::Standard" : "0",

META.json  view on Meta::CPAN

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
      }
   }
},
"provides" : {
   "API::Drip::Request" : {
      "file" : "lib/API/Drip/Request.pm",
      "version" : "0.05"
   }
},
"release_status" : "stable",
"resources" : {
   "bugtracker" : {
   },
   "repository" : {
      "type" : "git",
   }
},
"version" : "0.05",

META.yml  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
---
abstract: 'Perl interface to api.getdrip.com'
author:
  - 'Dan Wright <Dan@DWright.Org>'
build_requires:
  Test::More: '0'
configure_requires:
  Module::Build: '0'
dynamic_config: 1
generated_by: 'Module::Build version 0.4224, CPAN::Meta::Converter version 2.150010'
license: perl
meta-spec:
  version: '1.4'
name: API-Drip-Request
provides:
  API::Drip::Request:
    file: lib/API/Drip/Request.pm
    version: '0.05'
requires:
  Carp: '0'
  Data::Printer: '0'
  File::HomeDir: '0'
  File::Spec: '0'
  HTTP::Request::Common: '0'
  JSON: '0'
  LWP::UserAgent: '0'
  Params::ValidationCompiler: '0'
  Readonly: '0'
  Types::Standard: '0'
  URI: '0'
  YAML: '0'
resources:
version: '0.05'
x_serialization_backend: 'CPAN::Meta::YAML version 0.018'

bin/drip_client.pl  view on Meta::CPAN

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
=over
 
=item B<getsub>
 
Get a list of all subscribers.
 
=item B<addsub>
 
Add a subscriber.   At a minimum, must also specify -email, or -id.
 
Accepts: -email, -id, -new_email, -user_id, -time_zone, -lifetime_value, -ip_address
 
=item B<delsub>
 
Delete a subscriber.   Must specify -email, or -id.
 
=item B<getwork>
 
Get a list of all workflows.
 
=item B<startwork>
 
Start a user on a workflow
 
Requires: -workflow
 
Accepts: -email, -id, -user_id, -time_zone, -prospect
 
=item B<event>
 
Record that an event happened.
 
Requires: -email or -id, -action
 
Accepts: -prospect
 
 
=back
 
=head2 Additional Parameters
 
The parameters B<-email>, B<-id>, B<-new_email>, B<-user_id>, B<-time_zone>,
B<-lifetime_vaule>, B<-ip_address>, B<-prospect>, B<-action> are documented at
L<https://www.getdrip.com/docs/rest-api> and may be used with the above
operations.
 
=over
 
=item B<workflow>
 
Integer ID of a workflow.
 
 
=back

bin/drip_client.pl  view on Meta::CPAN

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
my %OPT = ( # Add defaults here.
);
 
GetOptions( \%OPT,
    'help|h|?',
    'man',
    'verbose|verb|v+',
    'conf=s',
    'addsub', 'getsub', 'delsub', 'getwork', 'startwork', 'event',
    'workflow=i', 'action=s',
    'email=s', 'id=s', 'new_email=s', 'user_id=s', 'time_zone=s', 'lifetime_vaule=i', 'ip_address=s', 'prospect'
) or pod2usage(2);
pod2usage(1) if $OPT{help};
pod2usage(-exitval => 0, -verbose => 2) if $OPT{man};
p %OPT if $OPT{verbose};
 
my $client = API::Drip::Request->new( $OPT{conf} ? ( DRIP_CLIENT_CONF => $OPT{conf} ) : () );
 
eval {
    if ( $OPT{getsub} ) {  get_subscribers() and exit }

bin/drip_client.pl  view on Meta::CPAN

147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
exit;
 
sub record_event {
    my %OPT = @_;
 
    $OPT{email} or $OPT{id} or pod2usage( "Either -email or -id is required for -event" );
    $OPT{action} or pod2usage( "-action is required for -event" );
 
    my $content = _build_hash( %OPT, keys => [qw(email id action prospect)] );
    my $result = $client->do_request( POST => 'events', { events => [ $content ] } );
    p $result;
}
 
 
sub get_workflows {
    my %OPT = @_;
 
    my $result = $client->do_request( GET => 'workflows' );
    p $result;
}
 
 
sub start_workflow {
    my %OPT = @_;
    $OPT{workflow} or pod2usage( "Required parameter -workflow missing for -startwork" );
 
    my $subscriber = _build_hash( %OPT, keys => [qw( email id user_id time_zone prospect )] );
 
    my $endpoint = 'workflows/' . $OPT{workflow} . '/subscribers';
 
    my $result = $client->do_request( POST => $endpoint, { subscribers => [ $subscriber ] } );
    p $result;
}
 
 
sub delete_subscribers {
    my %OPT = @_;
    my $id = $OPT{email} || $OPT{id};
 
    die "email or id required in delete subscriber" unless $id;
 
    my $result = $client->do_request( DELETE => "subscribers/$id" );
    p $result;
 
}
 
sub add_subscribers {
    my %OPT = @_;
    my $subscriber = _build_hash( %OPT, keys => [qw( email id new_email user_id time_zone lifetime_value ip_address )] );
    die "email or id required in add subscriber" unless $subscriber->{email} or $subscriber->{id};
 
    my $result = $client->do_request( POST => 'subscribers', { subscribers => [ $subscriber ]});
    p $result;
}
 
sub _build_hash {
    my %OPT = @_;
    my $build = {};
    foreach my $key ( @{$OPT{keys}} ) {
        next unless defined $OPT{$key};
        $build->{$key} = $OPT{$key};
    }
    return $build;
}
 
sub get_subscribers {
    my $result = $client->do_request( GET => 'subscribers' );
    p $result;
}

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

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
=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:

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

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
=item Endpoint (requird)
 
Specifies the path of the REST enpoint you want to query.   Include everything after the account ID.   For example, "subscribers", "subscribers/$subscriber_id/campaign_subscriptions", etc...
 
=item Content (optional)
 
Perl hashref of data that will be sent along with the request.  
 
=back
 
On success, returns a Perl data structure corresponding to the data returned
from the server.    Some operations (DELETE), do not return any data and may
return undef on success.  On error, this method will die() with the
HTTP::Response object.
 
=cut
 
my $request_validator = validation_for( params => [ {type => Str()}, {type => Str()}, {type => HashRef(), optional => 1} ] );
sub do_request {
    my $self = shift;
    my ($method, $endpoint, $content) = $request_validator->(@_);

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

141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    $self->{debugger}->( 'Requesting: ' . $uri->as_string );
    my $request = HTTP::Request->new( $method => $uri->as_string, );
    if ( ref($content) ) {
        $request->content_type('application/vnd.api+json');
        $request->content( encode_json( $content ) );
    }
    $request->authorization_basic( $self->{DRIP_TOKEN}, '' );
 
    $self->{agent} //= LWP::UserAgent->new( agent => $self->{DRIP_AGENT} );
    my $result = $self->{agent}->request( $request );
 
    unless ( $result->is_success ) {
        $self->{debugger}->("Request failed", $result->content);
        die $result;
    }
 
    if ( $result->code == 204 ) {
        $self->{debugger}->("Success, no content");
        return undef;
    }
    my $decoded = eval {decode_json( $result->content )};
    if ( $@ ) {
        $self->{debugger}->('Failed to decode JSON:', $@, $result->content);
        die $result;
    }
    return $decoded;
}
 
 
=head1 CONFIGURATION
 
Configuration data may be passed in through a number of different ways, which are searched in the following order of preference:
 
=over



( run in 0.819 second using v1.01-cache-2.11-cpan-87723dcf8b7 )