Skip to content

Commit

Permalink
Download: prepare for API similar to TextRender
Browse files Browse the repository at this point in the history
  • Loading branch information
stdweird committed Dec 12, 2015
1 parent f78fe60 commit b001e2c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 27 deletions.
26 changes: 20 additions & 6 deletions src/main/perl/Download.pm
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ CAF::Download - Class for downloading content from remote servers.
use CAF::Download;
my $dl = CAF::Download->new(['https://somewhere/myfile']);
print "$dl"; # stringification
$dl = CAF::TextRender->new(['https://somewhere/else']);
# return CAF::FileWriter instance (downloaded text already added)
my $fh = $dl->filewriter('/some/path');
die "Problem downloading the data" if (!defined($fh));
$fh->close();
=head1 DESCRIPTION
This class simplyfies the downloading of content located on remote servers.
Expand All @@ -79,10 +88,6 @@ Initialize the process object. Arguments:
=over
=item destination
The destination of the download, e.g. a filename.
=item urls
A array reference of urls. Urls will be tried in order, first successful
Expand All @@ -108,15 +113,19 @@ Boolean to run the setup (or not). Default/undef is to run setup.
Boolean to run the cleanup (or not). Default/undef is to run cleanup.
=item destination
The destination of the download, e.g. a filename. This is in particular required
for download methods that can write to file themself, like C<curl>.
=back
=cut

sub _initialize
{
my ($self, $destination, $urls, %opts) = @_;
my ($self, $urls, %opts) = @_;

$self->{destination} = $self->prepare_destination($destination);
$self->{urls} = $self->parse_urls($urls);

%opts = () if !%opts;
Expand All @@ -127,6 +136,11 @@ sub _initialize
$self->{cleanup} = (! defined($opts{cleanup}) || $opts{cleanup}) ? 1 : 0;
$self->debug(1, "setup $self->{setup} cleanup $self->{cleanup}");

if ($opts{destination}) {
$self->{destination} = $self->prepare_destination($opts{destination});
$self->debug(1, "download destination set to " . ($self->{destination} || '<UNDEF>'));
}

return SUCCESS;
}

Expand Down
18 changes: 13 additions & 5 deletions src/main/perl/Download/Retrieve.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ use LC::Exception qw (SUCCESS);

Readonly my $MAX_RETRIES => 1000;

=pod
=head1 NAME
CAF::Download::Retrieve - Class for retrieval for L<CAF::Download>.
=head1 DESCRIPTION
This class handles the downloading of the URLs to use within L<CAF::Download>.
=cut

=head2 Functions
=over
=item C<prepare_destination>
Expand Down Expand Up @@ -51,13 +63,9 @@ sub download
{
my ($self) = @_;

# in case the prepare_destination failed. fail attribute is set
return if (!defined($self->{destination}));

# in case the parse_urls failed. fail attribute is set
return if (!defined($self->{urls}));


my %tried; # per-url retry counter
my $tries = 0; # total tries

Expand Down
6 changes: 3 additions & 3 deletions src/main/perl/Download/URL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ _merge_url($_url_defaults, \%URL_DEFAULTS, 1);

=head1 NAME
CAF::Download::Url - Class for URL handling for L<CAF::Download>.
CAF::Download::URL - Class for URL handling for L<CAF::Download>.
=head1 DESCRIPTION
This class simplyfies handles the parsing, generation and validation
of URL to used withing L<CAF::Download>.
This class handles the parsing, generation and validation
of URL to used within L<CAF::Download>.
=cut

Expand Down
12 changes: 2 additions & 10 deletions src/test/perl/download-retrieve.t
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use Cwd;
my $obj = Test::Quattor::Object->new();
my $mock = Test::MockModule->new('CAF::Download');

my $d = CAF::Download->new("/tmp/dest", ["http://localhost"], log => $obj);
my $d = CAF::Download->new(["http://localhost"], log => $obj);

=pod
Expand All @@ -38,19 +38,11 @@ is_deeply($d->prepare_destination({x => 1}),
=cut

# test return undef with empty urls and destination
# test return undef with undefined urls
my $uniq_fail = 'yyz';
$d->{fail} = $uniq_fail;

$d->{destination} = undef;
$d->{urls} = [{}];
ok(defined($d->{urls}), 'urls attribute defined for this test');
ok(! defined($d->download()), 'download with undefined destination returns undef');
is($d->{fail}, $uniq_fail, 'download with undefined destination does not modify fail attribute');

$d->{destination} = '/a/file';
$d->{urls} = undef;
ok(defined($d->{destination}), 'destination attribute defined for this test');
ok(! defined($d->download()), 'download with undefined urls returns undef');
is($d->{fail}, $uniq_fail, 'download with undefined urls does not modify fail attribute');

Expand Down
2 changes: 1 addition & 1 deletion src/test/perl/download-url.t
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ is(set_url_defaults()->{krb5}->{realm}, 'TEST.ORG', 'new default value is set on
#
# Init instance for method testing
#
my $d = CAF::Download->new("/tmp/dest", ["http://localhost"]);
my $d = CAF::Download->new(["http://localhost"]);
isa_ok($d, 'CAF::Download', 'is a CAF::Download instance');

=pod
Expand Down
6 changes: 4 additions & 2 deletions src/test/perl/download.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ Test all methods for C<CAF::Download>
=cut


my $d = CAF::Download->new("/tmp/dest", ["http://localhost"], log => $obj);
my $d = CAF::Download->new(["http://localhost"], log => $obj);
isa_ok($d, 'CAF::Download', 'is a CAF::Download instance');
is($d->{setup}, 1, "default setup is 1");
is($d->{cleanup}, 1, "default cleanup is 1");
ok(! exists($d->{destination}), 'no destination specified, attribute does not exist');

$d = CAF::Download->new("/tmp/dest", ["http://localhost"], setup => 0, cleanup => 0, log => $obj);
$d = CAF::Download->new(["http://localhost"], destination => "/tmp/dest", setup => 0, cleanup => 0, log => $obj);
isa_ok($d, 'CAF::Download', 'is a CAF::Download instance');
is($d->{setup}, 0, "setup disabled / set to 0");
is($d->{cleanup}, 0, "cleanup disabled / set to 0");
is($d->{destination}, "/tmp/dest", "destination attribute set");

done_testing();

0 comments on commit b001e2c

Please sign in to comment.