File Coverage

blib/lib/WebService/SendInBlue.pm
Criterion Covered Total %
statement 31 90 34.4
branch 2 14 14.2
condition 1 2 50.0
subroutine 10 21 47.6
pod 8 11 72.7
total 52 138 37.6


line stmt bran cond sub pod time code
1             package WebService::SendInBlue;
2             {
3             $WebService::SendInBlue::VERSION = '0.005';
4             }
5              
6 4     4   1856 use strict;
  4         4  
  4         105  
7 4     4   12 use warnings;
  4         4  
  4         100  
8              
9 4     4   372 use HTTP::Request;
  4         14172  
  4         96  
10 4     4   593 use LWP::UserAgent;
  4         14873  
  4         79  
11 4     4   16 use JSON;
  4         3  
  4         19  
12 4     4   1421 use Data::Dumper;
  4         9907  
  4         205  
13 4     4   437 use IO::Socket::INET;
  4         14178  
  4         37  
14 4     4   3598 use URI::Query;
  4         18343  
  4         220  
15              
16 4     4   1657 use WebService::SendInBlue::Response;
  4         8  
  4         2873  
17              
18             =head1 NAME
19              
20             WebService::SendInBlue - Perl API for https://www.sendinblue.com/ REST API
21              
22             =head1 SYNOPSIS
23             use WebService::SendInBlue;
24              
25             my $api = WebService::SendInBlue->new('api_key'=>'API_KEY')
26              
27             my $campaigns_list = $a->campaigns();
28              
29             unless ( $campaigns_list->is_success ) {
30             die "Error getting campaigns:
31             }
32              
33             for my $campaign ( @{ $campaigns_list->data()->{'campaign_records'} ) {
34             ... do something for each campaign
35             }
36            
37              
38             =head1 DESCRIPTION
39              
40             This module provides a simple API to the SendInBlue API.
41              
42             The API reference can be found here: https://apidocs.sendinblue.com/
43              
44             You will need to register and set up your account with SendInBlue, you'll
45             need an API key to use this module.
46              
47             =cut
48              
49             our $API_BASE_URI = 'https://api.sendinblue.com/v2.0/';
50              
51             =head1 CONSTRUCTOR
52              
53             =over 4
54              
55             =item new ( api_key => 'your_api_key' )
56              
57             This is the constructor for a new WebService::SendInBlue object.
58             The C is required.
59              
60             =back
61              
62             =cut
63              
64             sub new {
65 2     2 1 509 my ($class, %args) = @_;
66              
67 2 100       14 die "api_key is mandatory" unless $args{'api_key'};
68              
69 1   50     9 my $debug = $args{'debug'} || $ENV{'SENDINBLUE_DEBUG'} || 0;
70              
71 1         6 return bless { api_key => $args{'api_key'}, debug => $debug }, $class;
72             }
73              
74             =head1 METHODS
75              
76             =head2 Campaign API
77              
78             =head3 Lists
79              
80             =over 4
81              
82             =item lists ( %params )
83              
84             Retrieves lists information.
85              
86             Supported parameters: https://apidocs.sendinblue.com/list/#1
87              
88             =back
89              
90             =cut
91              
92             sub lists {
93 0     0 1   my ($self, %args) = @_;
94              
95 0           return $self->_make_request("list", 'GET', params => \%args);
96             }
97              
98             =over 4
99              
100             =item lists_users ( lists_ids => [...], %params )
101              
102             Retrieves details of all users for the given lists. C is mandatory.
103              
104             Supported parameters: L
105              
106             =back
107              
108             =cut
109              
110             sub lists_users {
111 0     0 1   my ($self, %args) = @_;
112              
113 0           $args{'listids'} = delete $args{'lists_ids'};
114              
115 0           return $self->_make_request("list/display", 'POST', params => \%args);
116             }
117              
118             =head3 Campaigns
119              
120             =over 4
121              
122             =item campaigns ( %params )
123              
124             Retrieves details of all campaigns.
125              
126             Supported parameters: L
127              
128             =back
129              
130             =cut
131              
132             sub campaigns {
133 0     0 1   my ($self, %args) = @_;
134 0           return $self->_make_request("campaign/detailsv2", 'GET', params => \%args);
135             }
136              
137             =over 4
138              
139             =item campaign_details ( $campaign_id, %params )
140              
141             Retrieve details of any particular campaign. $campaign_id is mandatory.
142              
143             Supported parameters: L
144              
145             =back
146              
147             =cut
148              
149             sub campaign_details {
150 0     0 1   my ($self, $campaign_id) = @_;
151 0           return $self->_make_request(sprintf("campaign/%s/detailsv2", $campaign_id), 'GET');
152             }
153              
154             =over 4
155              
156             =item campaign_recipients ( $campaign_id, $notify_url, $type )
157              
158             Export the recipients of a specified campaign. It returns the background process ID which on completion calls the notify URL that you have set in the input. $campaign_id, $notify_url and $type are mandatory.
159              
160             Supported parameters: L
161              
162             =back
163              
164             =cut
165              
166             sub campaign_recipients {
167 0     0 1   my ($self, $campaign_id, $notify_url, $type) = @_;
168 0           my %params = ( type => $type, notify_url => $notify_url );
169 0           return $self->_make_request(sprintf("campaign/%s/recipients", $campaign_id), 'POST', params => \%params);
170             }
171              
172             =over 4
173              
174             =item campaign_recipients_file_url ( $campaign_id, $type )
175              
176             Exports the recipients of a specified campaign and returns the remote url of the export result file.
177             This method calls the campaign_recipients, waits for the export job completion, and retrieves the url of the export file.
178             The file url is returned in the response data 'url' attribute
179              
180             Example:
181              
182             my $result = $api->campaign_recipients_file_url($campaign_id, 'all');
183             my $file_url = $result->data->{'url'};
184              
185              
186             Supported parameters: L
187              
188             =back
189              
190             =cut
191              
192             sub campaign_recipients_file_url{
193 0     0 1   my ($self, $campaign_id, $type) = @_;
194              
195 0           my $inbox = $self->ua->post("http://api.webhookinbox.com/create/");
196 0 0         die "Inbox request failed" unless $inbox->is_success;
197              
198 0           $self->log($inbox->decoded_content);
199 0           sleep(1);
200              
201 0           my $inbox_data = decode_json($inbox->decoded_content);
202 0           my $inbox_url = $inbox_data->{'base_url'};
203              
204 0           my $req = $self->campaign_recipients( $campaign_id, $inbox_url."/in/", $type );
205 0 0         return $req unless $req->{'code'} eq 'success';
206              
207 0           my $process_id = $req->{'data'}->{'process_id'};
208              
209 0           my $max_wait = 10;
210 0           for (my $i=0; $i <= $max_wait; $i++) {
211             # Get inbox items
212 0           my $items = $self->ua->get($inbox_url."/items/?order=-created&max=20");
213 0 0         die "Inbox request failed" unless $items->is_success;
214              
215 0           $self->log($items->decoded_content);
216              
217 0           my $items_data = decode_json($items->decoded_content);
218 0           for my $i (@{$items_data->{'items'}}) {
  0            
219 0           my %data = URI::Query->new($i->{'body'})->hash;
220 0           $self->log(Dumper(\%data));
221              
222 0 0         next unless $data{'proc_success'} == $process_id;
223              
224 0           return { 'code' => 'success', 'data' => $data{'url'} };
225             }
226              
227 0           sleep(10);
228             }
229 0           die "Unable to wait more for the export file url";
230             }
231              
232              
233             =head2 SMTP API
234              
235             =head3 Aggregate reports
236              
237             =over 4
238              
239             =item smtp_statistics( %params )
240              
241             Retrieves reports for the SendinBlue SMTP account
242              
243             Supported parameters: L
244              
245             =back
246              
247             =cut
248              
249             sub smtp_statistics {
250 0     0 1   my ($self, %args) = @_;
251 0           return $self->_make_request("statistics", 'POST', params => \%args);
252             }
253              
254             sub processes {
255 0     0 0   my ($self, %args) = @_;
256 0           return $self->_make_request("process", 'GET', params => \%args);
257             }
258              
259             sub _make_request {
260 0     0     my ($self, $uri, $method, %args) = @_;
261            
262 0           my $req = HTTP::Request->new();
263              
264 0           $req->header('api-key' => $self->{'api_key'});
265 0           $req->header('api_key' => $self->{'api_key'});
266 0           $req->method($method);
267 0           $req->uri($API_BASE_URI.$uri);
268              
269 0 0         if ( $args{'params'} ) {
270 0           $req->content(encode_json($args{'params'}));
271 0           $self->log(encode_json($args{'params'}));
272             }
273              
274 0           my $resp = $self->ua->request($req);
275              
276 0           $self->log(Dumper($resp->content));
277              
278 0           my $json = decode_json($resp->content());
279              
280 0           $self->log(Dumper($json));
281              
282 0           return WebService::SendInBlue::Response->new($json);
283             }
284              
285             sub ua {
286 0     0 0   my $self = shift;
287              
288 0           return LWP::UserAgent->new();
289             }
290              
291             sub log {
292 0     0 0   my ($self, $line) = @_;
293              
294 0 0         return unless $self->{'debug'};
295              
296 0           print STDERR "[".ref($self)."] $line\n";
297             }
298              
299             =head1 SEE ALSO
300              
301             For information about the SendInBlue API:
302             L
303              
304             To sign up for an account:
305             L
306              
307             =head1 LICENCE AND COPYRIGHT
308              
309             Copyright (C) 2016 Bruno Tavares. All Rights Reserved.
310              
311             This module is free software; you can redistribute it and/or modify it
312             under the same terms as Perl itself.
313              
314             =head1 AUTHOR
315              
316             Bruno Tavares
317              
318             =cut
319              
320             1;