Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expanded support for bounces.get #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ version = 0.001

[Prereqs]
IO::Socket::SSL = 1.44 ; version I've tested with
Mouse = 0
HTTP::Tiny = 0
XML::Simple = 0
JSON = 0
URI::Escape = 0

[PodWeaver]

Expand Down
40 changes: 40 additions & 0 deletions example/bounces.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin qw($Bin);
use lib "$Bin/../lib";
use Mail::SendGrid;
use Getopt::Long;


my %opt;
GetOptions( \%opt, 'help|h', 'username|u=s', 'password|p=s',
'start_date|s:s', 'end_date|e:s', 'days|d:i', 'limit|l:i', 'offset|s:i',
'type|t:s', 'email|e:s') or usage();
usage() if !$opt{username} or !$opt{password} or $opt{help};

my $sendgrid = Mail::SendGrid->new(
api_user => delete $opt{username},
api_key => delete $opt{password},
);

for my $bounce ($sendgrid->bounces(\%opt)) {
print "Bounce: " . $bounce->email. "\n";
}


sub usage {
print <<USAGE;
$0: bounces.pl [opts]

Options:
username (string) REQUIRED. SendGrid username (api_user)
password (string) REQUIRED. SendGrid password (api_key)
days (int) Number of days in the past to retrieve (includes today)
start_date (string) Start date of date range (YYYY-MM-DD)
send_date (string) End date of date range (YYYY-MM-DD)
limit (int) Limit number of results returned
offset (int) Beginning point in the list to retrieve from
USAGE
die 0;
}
40 changes: 33 additions & 7 deletions lib/Mail/SendGrid.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,48 @@ package Mail::SendGrid;
use Mouse 0.94;
use HTTP::Tiny 0.013;
use XML::Simple 2.18;
use JSON qw(decode_json);
use URI::Escape;
use Mail::SendGrid::Bounce;

has 'api_user' => (is => 'ro', isa => 'Str', required => 1);
has 'api_key' => (is => 'ro', isa => 'Str', required => 1);
has 'debug' => (is => 'ro', isa => 'Bool', default => 0);
has 'api_user' => (is => 'ro', isa => 'Str', required => 1);
has 'api_key' => (is => 'ro', isa => 'Str', required => 1);
has 'format' => (is => 'ro', isa => 'Str', required => 1, default => 'json');

our $VERSION = '0.01';

sub bounces
{
my $self = shift;
my $uri = 'https://sendgrid.com/api/bounces.get.xml?api_user='.$self->api_user.'&api_key='.$self->api_key.'&date=1';
my $opts = shift || {};
my $uri = "https://sendgrid.com/api/bounces.get.".$self->format.
"?api_user=".$self->api_user.'&api_key='.$self->api_key.
'&date=1';
for my $key (keys %$opts) {
my $name = uri_escape($key);
my $val = uri_escape($opts->{$key});
$uri .= '&' . $name . '=' . $val;
}
warn "URI: $uri\n" if $self->debug;
my $response = HTTP::Tiny->new->get($uri);
my $data;
my (@bounces, $bounce);
my @bounces;

if ($response->{success})
{
$data = XMLin($response->{content});
foreach my $hashref (@{ $data->{'bounce'} })
if ($self->format eq 'json')
{
$data = decode_json($response->{content});
}
elsif ($self->format eq 'xml')
{
$bounce = Mail::SendGrid::Bounce->new($hashref);
$data = XMLin($response->{content});
$data = @{ $data->{'bounce'} };
}
foreach my $entry (@$data)
{
my $bounce = Mail::SendGrid::Bounce->new($entry);
push(@bounces, $bounce) if defined($bounce);
}
}
Expand Down Expand Up @@ -65,6 +86,11 @@ with SendGrid. These are required.

This requests all outstanding bounces from SendGrid, and returns a list of Mail::SendGrid::Bounce objects.

Also takes a hashref of optional parameters, as defined by the bounces.get
method. See L<http://docs.sendgrid.com/documentation/api/web-api/webapibounces/>
for more details. The I<date> parameter is always set to 1 so the date is
always included in the response.

=head1 SEE ALSO

=over 4
Expand Down