Skip to content

Commit

Permalink
Fix file uploading for t/02
Browse files Browse the repository at this point in the history
  • Loading branch information
jadeallenx committed Dec 30, 2011
1 parent d6be869 commit b8772e9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 48 deletions.
39 changes: 17 additions & 22 deletions lib/Net/API/Gett.pm
Expand Up @@ -6,10 +6,8 @@ use warnings;
use v5.10;

use Moo;
use Sub::Quote;
use Scalar::Util qw(looks_like_number);
use File::Slurp qw(read_file);
use Carp qw(croak);
use Scalar::Util qw(looks_like_number);

use Net::API::Gett::User;
use Net::API::Gett::Share;
Expand Down Expand Up @@ -356,8 +354,14 @@ sub upload_file {

my $response = $self->request->post($endpoint, { filename => $filename });

if ( not exists $opts->{'contents'} ) {
$opts->{'contents'} = $filename;
# typo proof this - yeah I've been bitten by this!
unless ( exists $opts->{'contents'} ) {
if ( exists $opts->{'content'} ) {
$opts->{'contents'} = delete $opts->{'content'};
}
else {
$opts->{'contents'} = $filename
}
}

if ( $response ) {
Expand Down Expand Up @@ -404,28 +408,19 @@ sub _build_file {
my $self = shift;
my $file_href = shift;

my %attrs = (
filename => $file_href->{'filename'},
size => $file_href->{'size'},
created => $file_href->{'created'},
fileid => $file_href->{'fileid'},
downloads => $file_href->{'downloads'},
readystate => $file_href->{'readystate'},
url => $file_href->{'getturl'},
download => $file_href->{'downloadurl'},
sharename => $file_href->{'sharename'},
);
# filter out undefined attributes
my @attrs = grep { defined $file_href->{$_} }
qw(filename size created fileid downloads readystate url download sharename);
my @params = map { $_ => $file_href->{$_} } @attrs;

if ( exists $file_href->{'upload'} ) {
@attrs{'put_upload_url', 'post_upload_url'} = (
$file_href->{'upload'}->{'puturl'},
$file_href->{'upload'}->{'posturl'}
);
push @params, 'put_upload_url' => $file_href->{'upload'}->{'puturl'};
push @params, 'post_upload_url' => $file_href->{'upload'}->{'posturl'};
}

my $file = Net::API::Gett::File->new( %attrs );
my $file = Net::API::Gett::File->new( @params );
$file->user($self->user) if $self->has_user;

return $file;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Net/API/Gett/File.pm
Expand Up @@ -9,6 +9,7 @@ Net::API::Gett::File - Gett file object
use Moo;
use Sub::Quote;
use Carp qw(croak);
use File::Slurp qw(read_file);
use MooX::Types::MooseLike qw(Int Str);

our $VERSION = '0.02';
Expand Down Expand Up @@ -206,7 +207,7 @@ sub send_file {

return 0 unless $data;

my $response = $self->request->put($url, Content => $data);
my $response = $self->request->put($url, $data);

if ( $response ) {
return 1;
Expand Down
40 changes: 25 additions & 15 deletions lib/Net/API/Gett/User.pm
Expand Up @@ -4,6 +4,7 @@ use Moo;
use Sub::Quote;
use Carp qw(croak);
use Scalar::Util qw(looks_like_number);
use MooX::Types::MooseLike qw(Str Int);

use Net::API::Gett::Request;

Expand Down Expand Up @@ -36,7 +37,7 @@ Scalar string. Read-only. C<has_api_key> predicate.
has 'api_key' => (
is => 'ro',
predicate => 'has_api_key',
isa => quote_sub q{ die "$_[0] is not alphanumeric" unless $_[0] =~ /[a-z0-9]+/ },
isa => Str,
);

=over
Expand Down Expand Up @@ -68,7 +69,7 @@ Scalar string. Read-only. C<has_password> predicate.
has 'password' => (
is => 'ro',
predicate => 'has_password',
isa => quote_sub q{ die "$_[0] is not alphanumeric" unless $_[0] =~ /\w+/ },
isa => Str,
);

=over
Expand All @@ -83,8 +84,9 @@ Scalar string. Populated by C<login> call. C<has_access_token()> predicate.

has 'access_token' => (
is => 'rw',
writer => '_set_access_token',
predicate => 'has_access_token',
isa => quote_sub q{ die "$_[0] is not alphanumeric" unless $_[0] =~ /[\w\.-]+/ }
isa => Str,
);

=over
Expand All @@ -101,7 +103,8 @@ This value is suitable for use in a call to C<localtime()>.

has 'access_token_expiration' => (
is => 'rw',
isa => sub { die "$_[0] is not a number" unless looks_like_number $_[0] }
writer => '_set_access_token_expiration',
isa => Int,
);

=over
Expand All @@ -118,8 +121,9 @@ predicate.

has 'refresh_token' => (
is => 'rw',
writer => '_set_refresh_token',
predicate => 'has_refresh_token',
isa => sub { die "$_[0] is not alphanumeric" unless $_[0] =~ /[\w\.-]+/ }
isa => Str,
);

=over
Expand Down Expand Up @@ -168,19 +172,26 @@ Scalar integer. In bytes.

has 'userid' => (
is => 'rw',
isa => sub { croak "$_[0] isn't alphanumeric\n" unless $_[0] =~ /[\w-]+/ },
writer => '_set_userid',
isa => Str
);

has 'fullname' => (
is => 'rw',
writer => '_set_fullname',
isa => Str,
);

has 'storage_used' => (
is => 'rw',
isa => Int,
writer => '_set_storage_used',
);

has 'storage_limit' => (
is => 'rw',
isa => Int,
writer => '_set_storage_limit',
);

=head2 Account methods
Expand Down Expand Up @@ -234,7 +245,7 @@ sub login {
$self->password);
}
else {
return undef;
croak "I need either an api_key, email, and password or a refresh token to login";
}

my $response = $self->request->post('/users/login', \%hr);
Expand All @@ -243,9 +254,9 @@ sub login {
# see https://open.ge.tt/1/doc/rest#users/login for response keys

if ( $response ) {
$self->access_token( $response->{'accesstoken'} );
$self->access_token_expiration( time + $response->{'expires'} );
$self->refresh_token( $response->{'refreshtoken'} );
$self->_set_access_token( $response->{'accesstoken'} );
$self->_set_access_token_expiration( time + $response->{'expires'} );
$self->_set_refresh_token( $response->{'refreshtoken'} );
$self->_set_attrs( $response->{'user'} );
return $self;
}
Expand Down Expand Up @@ -288,11 +299,10 @@ sub _set_attrs {

return undef unless ref($uref) eq "HASH";

$self->userid($uref->{'userid'});
$self->fullname($uref->{'fullname'});
$self->email($uref->{'email'});
$self->storage_used($uref->{'storage'}->{'used'});
$self->storage_limit($uref->{'storage'}->{'limit'}),
$self->_set_userid($uref->{'userid'});
$self->_set_fullname($uref->{'fullname'});
$self->_set_storage_used($uref->{'storage'}->{'used'});
$self->_set_storage_limit($uref->{'storage'}->{'limit'}),
}

=head1 SEE ALSO
Expand Down
37 changes: 27 additions & 10 deletions t/02-use_auth.t
Expand Up @@ -2,6 +2,8 @@

use strict;
use Test::More;
use File::Temp;
use Data::Printer;

if (!eval { require Socket; Socket::inet_aton('open.ge.tt') }) {
plan skip_all => "Cannot connect to the API server";
Expand All @@ -10,41 +12,56 @@ elsif ( ! $ENV{GETT_API_KEY} || ! $ENV{GETT_EMAIL} || ! $ENV{GETT_PASSWORD} ) {
plan skip_all => "API credentials required for these tests";
}
else {
plan tests => 10;
plan tests => 11;
}

# untaint environment variables
# They will be validated for correctness in the User.pm module, so just match anything here.

my @params = map {my ($v) = $ENV{uc "GETT_$_"} =~ /\A(.*)\z/; $_ => $v} qw(api_key email password);

use Net::API::Gett;

my $gett = Net::API::Gett->new(
api_key => $ENV{GETT_API_KEY},
email => $ENV{GETT_EMAIL},
password => $ENV{GETT_PASSWORD},
);
my $gett = Net::API::Gett->new( @params );

isa_ok($gett, 'Net::API::Gett', "Gett object constructed");
isa_ok($gett->request, 'Net::API::Gett::Request', "Gett request constructed");

isa_ok($gett->user, 'Net::API::Gett::User', "Gett User object constructed");
is($gett->user->has_access_token, 1, "Has access token");
is($gett->user->has_access_token, '', "Has no access token");

$gett->user->login or die $!;

is($gett->user->has_access_token, 1, "Has access token now");

my $test_string = "Some test data. Whee!";

my $tmp = File::Temp->new();
open my $fh, ">", $tmp->filename;
print $fh $test_string;
close $fh;

# Upload a file, download its contents, then destroy the share and the file
my $file = $gett->upload_file(
filename => "test.t",
content => "t/00-load.t",
contents => $tmp->filename,
title => "perltest",
);

isa_ok($file, 'Net::API::Gett::File', "File uploaded");

is($file->filename, "test.t", "Got right filename");
is($file->size, 178, "Got right filesize");

my $content = $file->contents();

like($content, qr/use_ok/, "Got right file content");
like($content, qr/Whee/, "Got right file content");

my $share = $gett->get_share( $file->sharename );

is($share->title, "perltest", "Got right share title");

my $file1 = ($share->files)[0];

is($file1->size, length($test_string), "Got right filesize");

is($share->destroy(), 1, "Share destroyed");

0 comments on commit b8772e9

Please sign in to comment.