| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 4 |  |  | 4 |  | 3492 | use strict; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 199 |  | 
| 2 | 4 |  |  | 4 |  | 28 | use warnings; | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 373 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | package WebService::SendInBlue; | 
| 5 |  |  |  |  |  |  | { | 
| 6 |  |  |  |  |  |  | $WebService::SendInBlue::VERSION = '0.004'; | 
| 7 |  |  |  |  |  |  | } | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 4 |  |  | 4 |  | 712 | use HTTP::Request; | 
|  | 4 |  |  |  |  | 30577 |  | 
|  | 4 |  |  |  |  | 230 |  | 
| 10 | 4 |  |  | 4 |  | 1154 | use LWP::UserAgent; | 
|  | 4 |  |  |  |  | 29691 |  | 
|  | 4 |  |  |  |  | 151 |  | 
| 11 | 4 |  |  | 4 |  | 30 | use JSON; | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 36 |  | 
| 12 | 4 |  |  | 4 |  | 3084 | use Data::Dumper; | 
|  | 4 |  |  |  |  | 16849 |  | 
|  | 4 |  |  |  |  | 366 |  | 
| 13 | 4 |  |  | 4 |  | 539 | use IO::Socket::INET; | 
|  | 4 |  |  |  |  | 17615 |  | 
|  | 4 |  |  |  |  | 52 |  | 
| 14 | 4 |  |  | 4 |  | 5599 | use URI::Query; | 
|  | 4 |  |  |  |  | 24721 |  | 
|  | 4 |  |  |  |  | 3392 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # ABSTRACT: Perl API to SendInBlue rest api | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | our $API_BASE_URI = 'https://api.sendinblue.com/v2.0/'; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub new { | 
| 21 | 2 |  |  | 2 | 0 | 642 | my ($class, %args) = @_; | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 2 | 100 |  |  |  | 21 | die "api_key is mandatory" unless $args{'api_key'}; | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 1 |  | 50 |  |  | 11 | my $debug = $args{'debug'} || $ENV{'SENDINBLUE_DEBUG'} || 0; | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 1 |  |  |  |  | 10 | return bless { api_key => $args{'api_key'}, debug => $debug }, $class; | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | sub lists { | 
| 31 | 0 |  |  | 0 | 0 |  | my ($self, %args) = @_; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 0 |  |  |  |  |  | return $self->_make_request("list", 'GET', params => \%args); | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | sub lists_users { | 
| 37 | 0 |  |  | 0 | 0 |  | my ($self, %args) = @_; | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 0 |  |  |  |  |  | $args{'listids'} = delete $args{'lists_ids'}; | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 0 |  |  |  |  |  | return $self->_make_request("list/display", 'POST', params => \%args); | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | sub campaigns { | 
| 45 | 0 |  |  | 0 | 0 |  | my ($self, %args) = @_; | 
| 46 | 0 |  |  |  |  |  | return $self->_make_request("campaign/detailsv2", 'GET', params => \%args); | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | sub campaign_details { | 
| 50 | 0 |  |  | 0 | 0 |  | my ($self, $campaign_id) = @_; | 
| 51 | 0 |  |  |  |  |  | return $self->_make_request(sprintf("campaign/%s/detailsv2", $campaign_id), 'GET'); | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | sub campaign_recipients { | 
| 55 | 0 |  |  | 0 | 0 |  | my ($self, $campaign_id, $notify_url, $type) = @_; | 
| 56 | 0 |  |  |  |  |  | my %params = ( type => $type, notify_url => $notify_url ); | 
| 57 | 0 |  |  |  |  |  | return $self->_make_request(sprintf("campaign/%s/recipients", $campaign_id), 'POST', params => \%params); | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | sub campaign_recipients_file_url{ | 
| 61 | 0 |  |  | 0 | 0 |  | my ($self, $campaign_id, $type) = @_; | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 0 |  |  |  |  |  | my $inbox = $self->ua->post("http://api.webhookinbox.com/create/"); | 
| 64 | 0 | 0 |  |  |  |  | die "Inbox request failed" unless $inbox->is_success; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 0 |  |  |  |  |  | $self->log($inbox->decoded_content); | 
| 67 | 0 |  |  |  |  |  | sleep(1); | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 0 |  |  |  |  |  | my $inbox_data = decode_json($inbox->decoded_content); | 
| 70 | 0 |  |  |  |  |  | my $inbox_url  = $inbox_data->{'base_url'}; | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 0 |  |  |  |  |  | my $req = $self->campaign_recipients( $campaign_id, $inbox_url."/in/", $type ); | 
| 73 | 0 | 0 |  |  |  |  | return $req unless $req->{'code'} eq 'success'; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 0 |  |  |  |  |  | my $process_id = $req->{'data'}->{'process_id'}; | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 0 |  |  |  |  |  | my $max_wait = 10; | 
| 78 | 0 |  |  |  |  |  | for (my $i=0; $i <= $max_wait; $i++) { | 
| 79 |  |  |  |  |  |  | # Get inbox items | 
| 80 | 0 |  |  |  |  |  | my $items = $self->ua->get($inbox_url."/items/?order=-created&max=20"); | 
| 81 | 0 | 0 |  |  |  |  | die "Inbox request failed" unless $items->is_success; | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 0 |  |  |  |  |  | $self->log($items->decoded_content); | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 0 |  |  |  |  |  | my $items_data = decode_json($items->decoded_content); | 
| 86 | 0 |  |  |  |  |  | for my $i (@{$items_data->{'items'}}) { | 
|  | 0 |  |  |  |  |  |  | 
| 87 | 0 |  |  |  |  |  | my %data = URI::Query->new($i->{'body'})->hash; | 
| 88 | 0 |  |  |  |  |  | $self->log(Dumper(\%data)); | 
| 89 |  |  |  |  |  |  |  | 
| 90 | 0 | 0 |  |  |  |  | next unless $data{'proc_success'} == $process_id; | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 0 |  |  |  |  |  | return { 'code' => 'success', 'data' => $data{'url'} }; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 0 |  |  |  |  |  | sleep(10); | 
| 96 |  |  |  |  |  |  | } | 
| 97 | 0 |  |  |  |  |  | die "Unable to wait more for the export file url"; | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | sub smtp_statistics { | 
| 101 | 0 |  |  | 0 | 0 |  | my ($self, %args) = @_; | 
| 102 | 0 |  |  |  |  |  | return $self->_make_request("statistics", 'POST', params => \%args); | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | sub processes { | 
| 106 | 0 |  |  | 0 | 0 |  | my ($self, %args) = @_; | 
| 107 | 0 |  |  |  |  |  | return $self->_make_request("process", 'GET', params => \%args); | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | sub _make_request { | 
| 111 | 0 |  |  | 0 |  |  | my ($self, $uri, $method, %args) = @_; | 
| 112 |  |  |  |  |  |  |  | 
| 113 | 0 |  |  |  |  |  | my $req = HTTP::Request->new(); | 
| 114 |  |  |  |  |  |  |  | 
| 115 | 0 |  |  |  |  |  | $req->header('api-key' => $self->{'api_key'}); | 
| 116 | 0 |  |  |  |  |  | $req->header('api_key' => $self->{'api_key'}); | 
| 117 | 0 |  |  |  |  |  | $req->method($method); | 
| 118 | 0 |  |  |  |  |  | $req->uri($API_BASE_URI.$uri); | 
| 119 |  |  |  |  |  |  |  | 
| 120 | 0 | 0 |  |  |  |  | if ( $args{'params'} ) { | 
| 121 | 0 |  |  |  |  |  | $req->content(encode_json($args{'params'})); | 
| 122 | 0 |  |  |  |  |  | $self->log(encode_json($args{'params'})); | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 | 0 |  |  |  |  |  | my $resp = $self->ua->request($req); | 
| 126 |  |  |  |  |  |  |  | 
| 127 | 0 |  |  |  |  |  | $self->log(Dumper($resp->content)); | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 0 |  |  |  |  |  | my $json = decode_json($resp->content()); | 
| 130 |  |  |  |  |  |  |  | 
| 131 | 0 |  |  |  |  |  | $self->log(Dumper($json)); | 
| 132 | 0 |  |  |  |  |  | return $json; | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | sub ua { | 
| 136 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 0 |  |  |  |  |  | return LWP::UserAgent->new(); | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | sub log { | 
| 142 | 0 |  |  | 0 | 0 |  | my ($self, $line) = @_; | 
| 143 |  |  |  |  |  |  |  | 
| 144 | 0 | 0 |  |  |  |  | return unless $self->{'debug'}; | 
| 145 |  |  |  |  |  |  |  | 
| 146 | 0 |  |  |  |  |  | print STDERR "[".ref($self)."] $line\n"; | 
| 147 |  |  |  |  |  |  | } | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | 1; |