Show
Ignore:
Timestamp:
10/21/08 21:54:59 (5 years ago)
Author:
ktat
Message:

add post_with_progress_method for POST request

Location:
lang/perl/LWP-UserAgent-ProgressBar/trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/LWP-UserAgent-ProgressBar/trunk/Changes

    r16488 r21800  
    11Revision history for Perl extension LWP-UserAgent-ProgressBar 
     2 
     30.04  Thu Oct 21 21:36:48 JST 2008 (Ktat <ktat at cpan.org>) 
     4     - add post_with_progress method for POST request.  
    25 
    360.03  Thu Jul 25 15:27:43 CEST 2008 (Marcel Gruenauer <marcel@cpan.org>) 
  • lang/perl/LWP-UserAgent-ProgressBar/trunk/lib/LWP/UserAgent/ProgressBar.pm

    r16588 r21800  
    99 
    1010 
    11 our $VERSION = '0.03'; 
     11our $VERSION = '0.04'; 
    1212 
     13sub post_with_progress { 
     14    my ($self, $url, $form, %args) = @_; 
     15    $self->_request_with_progress('post', $url, $form, %args); 
     16} 
    1317 
    1418sub get_with_progress { 
    1519    my ($self, $url, %args) = @_; 
     20    $self->_request_with_progress('get', $url, undef, %args); 
     21} 
     22 
     23sub _request_with_progress { 
     24    my ($self, $req, $url, $form, %args) = @_; 
    1625 
    1726    # don't buffer the prints to make the status update 
     
    3847 
    3948    delete $args{$_} for qw(:content_cb :read_size_hint); 
     49    $args{':content_cb'} =  sub { 
     50        my ($data, $cb_response, $protocol) = @_; 
    4051 
    41     my $response = $self->get( 
    42         $url, 
    43         %args, 
    44         ':content_cb' => sub { 
    45             my ($data, $cb_response, $protocol) = @_; 
     52        unless ($did_set_target) { 
     53            if (my $content_length = $cb_response->content_length) { 
     54                $progress->target($content_length); 
     55                $did_set_target = 1; 
     56            } else { 
     57                $progress->target($received_size + 2 * length $data); 
     58            } 
     59        } 
    4660 
    47             unless ($did_set_target) { 
    48                 if (my $content_length = $cb_response->content_length) { 
    49                     $progress->target($content_length); 
    50                     $did_set_target = 1; 
    51                 } else { 
    52                     $progress->target($received_size + 2 * length $data); 
    53                 } 
    54             } 
     61        $received_size += length $data; 
     62        $content .= $data; 
    5563 
    56             $received_size += length $data; 
    57             $content .= $data; 
     64        $next_update = $progress->update($received_size) if 
     65            $received_size >= $next_update; 
    5866 
    59             $next_update = $progress->update($received_size) if 
    60                 $received_size >= $next_update; 
     67    }; 
     68    $args{':read_size_hint'} = 8192; 
    6169 
    62         }, 
    63         ':read_size_hint' => 8192, 
    64     ); 
     70    my $response = $self->$req($url, $req eq 'get' ? (%$form, %args) : ($form, %args)); 
     71 
    6572    print "\n"; 
    6673    $response->content($content); 
     
    100107 
    101108Takes the same arguments as L<LWP::UserAgent>'s C<get()>, but overrides the 
     109C<:content_cb> and C<:read_size_hint> arguments. During download, a progress 
     110bar is displayed. 
     111 
     112=item post_with_progress 
     113 
     114Takes the same arguments as L<LWP::UserAgent>'s C<post()>, but overrides the 
    102115C<:content_cb> and C<:read_size_hint> arguments. During download, a progress 
    103116bar is displayed. 
     
    143156 
    144157Marcel GrE<uuml>nauer, C<< <marcel@cpan.org> >> 
     158Ktat C<< ktat at cpan.org >> 
    145159 
    146160=head1 COPYRIGHT AND LICENSE 
  • lang/perl/LWP-UserAgent-ProgressBar/trunk/t/01_misc.t

    r9783 r21800  
    66use File::Spec; 
    77use LWP::UserAgent::ProgressBar; 
    8 use Test::More tests => 2; 
     8use Test::More tests => 4; 
    99use Test::Differences; 
    1010 
     
    2525eq_or_diff $content, $self_content, 'content matches'; 
    2626 
     27$self_url = 'http://search.cpan.org/search'; 
     28$response = LWP::UserAgent::ProgressBar->new->post_with_progress( 
     29    $self_url, {query => 'LWP'}, bar_name => '# ' 
     30); 
     31 
     32ok($response->is_success, 'successful download'); 
     33ok($response->content, 'content is not empty'); 
     34