AnyEvent-HTTP-ScopedClient-0.0.5000755001750001750 012161571204 16332 5ustar00hshonghshong000000000000LICENSE100644001750001750 27612161571204 17405 0ustar00hshonghshong000000000000AnyEvent-HTTP-ScopedClient-0.0.5This software is copyright (c) 2012 by Hyungsuk Hong. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Changes100644001750001750 75512161571204 17675 0ustar00hshonghshong000000000000AnyEvent-HTTP-ScopedClient-0.0.5go to https://github.com/aanoaa/AnyEvent-HTTP-ScopedClient/issues for the roadmap and known issues. v0.0.5 Sun Jun 23 22:03:40 2013 KST - prevent 'newline' at 'Authorization' header v0.0.4 Thu May 30 11:13:44 2013 KST - uri_escape_utf8(request_body) 0.0.3 Fri Nov 30 10:04:43 2012 KST - depend `Host` req header to `AnyEvent::HTTP` 0.0.2 Thu Oct 11 10:59:18 2012 KST - support editing multiple header at once - enhancement test - tidied all source code INSTALL100644001750001750 204012161571204 17440 0ustar00hshonghshong000000000000AnyEvent-HTTP-ScopedClient-0.0.5 This is the Perl distribution AnyEvent-HTTP-ScopedClient. Installing AnyEvent-HTTP-ScopedClient is straightforward. ## Installation with cpanm If you have cpanm, you only need one line: % cpanm AnyEvent::HTTP::ScopedClient If you are installing into a system-wide directory, you may need to pass the "-S" flag to cpanm, which uses sudo to install the module: % cpanm -S AnyEvent::HTTP::ScopedClient ## Installing with the CPAN shell Alternatively, if your CPAN shell is set up, you should just be able to do: % cpan AnyEvent::HTTP::ScopedClient ## Manual installation As a last resort, you can manually install it. Download the tarball, untar it, then build it: % perl Makefile.PL % make && make test Then install it: % make install If you are installing into a system-wide directory, you may need to run: % sudo make install ## Documentation AnyEvent-HTTP-ScopedClient documentation is available as POD. You can run perldoc from a shell to read the documentation: % perldoc AnyEvent::HTTP::ScopedClient dist.ini100644001750001750 54612161571204 20044 0ustar00hshonghshong000000000000AnyEvent-HTTP-ScopedClient-0.0.5name = AnyEvent-HTTP-ScopedClient abstract = AnyEvent::HTTP wrapper author = Hyungsuk Hong license = Perl_5 copyright_holder = Hyungsuk Hong copyright_year = 2012 version = 0.0.5 [@Filter] -bundle = @Basic -remove = License -remove = Readme option = for_basic [PodWeaver] [AutoPrereqs] [PkgVersion] [InstallGuide] ;[ReadmeMarkdownFromPod] META.yml100644001750001750 120612161571204 17663 0ustar00hshonghshong000000000000AnyEvent-HTTP-ScopedClient-0.0.5--- abstract: 'AnyEvent::HTTP wrapper' author: - 'Hyungsuk Hong ' build_requires: AnyEvent: 0 Test::More: 0 strict: 0 utf8: 0 warnings: 0 configure_requires: ExtUtils::MakeMaker: 6.30 dynamic_config: 0 generated_by: 'Dist::Zilla version 4.300034, CPAN::Meta::Converter version 2.131560' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: AnyEvent-HTTP-ScopedClient requires: AnyEvent::HTTP: 0 Encode: 0 HTTP::Request: 0 MIME::Base64: 0 Moose: 0 Try::Tiny: 0 URI: 0 URI::Escape: 0 URI::QueryParam: 0 namespace::autoclean: 0 version: 0.0.5 MANIFEST100644001750001750 22412161571204 17522 0ustar00hshonghshong000000000000AnyEvent-HTTP-ScopedClient-0.0.5Changes INSTALL LICENSE MANIFEST META.yml Makefile.PL README.md dist.ini lib/AnyEvent/HTTP/ScopedClient.pm t/00-load.t t/scoped-client.t weaver.ini README.md100644001750001750 356412161571204 17702 0ustar00hshonghshong000000000000AnyEvent-HTTP-ScopedClient-0.0.5## Scoped HTTP Client for Perl ## Yet another [AnyEvent::HTTP](http://search.cpan.org/~mlehmann/AnyEvent-HTTP/HTTP.pm) based client. stolen from [Scoped HTTP Client for Node.js](https://github.com/technoweenie/node-scoped-http-client) ```perl use AnyEvent; use AnyEvent::HTTP::ScopedClient; my $client = AnyEvent::HTTP::ScopedClient->new('http://example.com'); $client->request('GET', sub { my ($body, $hdr) = @_; # $body is undef if error occured return if ( !$body || !$hdr->{Status} =~ /^2/ ); # do something; }); # shorcut for GET $client->get(sub { my ($body, $hdr) = @_; # $body is undef if error occured return if ( !$body || !$hdr->{Status} =~ /^2/ ); # do something; }); # Content-Type: application/x-www-form-urlencoded $client->post( { foo => 1, bar => 2 }, # note this. sub { my ($body, $hdr) = @_; # $body is undef if error occured return if ( !$body || !$hdr->{Status} =~ /^2/ ); # do something; } ); # application/x-www-form-urlencoded post request $client->post( "foo=1&bar=2" # and note this. sub { my ($body, $hdr) = @_; # $body is undef if error occured return if ( !$body || !$hdr->{Status} =~ /^2/ ); # do something; } ); # Content-Type: application/json use JSON::XS; $client->header('Content-Type', 'application/json') ->post( encode_json({ foo => 1 }), sub { my ($body, $hdr) = @_; # $body is undef if error occured return if ( !$body || !$hdr->{Status} =~ /^2/ ); # do something; } ); $client->header('Accept', 'application/json') ->query({ key => 'value' }) ->query('key', 'value') ->get(sub { my ($body, $hdr) = @_; # $body is undef if error occured return if ( !$body || !$hdr->{Status} =~ /^2/ ); # do something; }); AnyEvent->condvar->recv; ``` weaver.ini100644001750001750 5112161571204 20341 0ustar00hshonghshong000000000000AnyEvent-HTTP-ScopedClient-0.0.5[@Default] [-Encoding] encoding = utf-8 t000755001750001750 012161571204 16516 5ustar00hshonghshong000000000000AnyEvent-HTTP-ScopedClient-0.0.500-load.t100644001750001750 14712161571204 20161 0ustar00hshonghshong000000000000AnyEvent-HTTP-ScopedClient-0.0.5/tuse strict; use warnings; use Test::More tests => 1; BEGIN { use_ok('AnyEvent::HTTP::ScopedClient') } Makefile.PL100644001750001750 323612161571204 20371 0ustar00hshonghshong000000000000AnyEvent-HTTP-ScopedClient-0.0.5 use strict; use warnings; use ExtUtils::MakeMaker 6.30; my %WriteMakefileArgs = ( "ABSTRACT" => "AnyEvent::HTTP wrapper", "AUTHOR" => "Hyungsuk Hong ", "BUILD_REQUIRES" => {}, "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => "6.30" }, "DISTNAME" => "AnyEvent-HTTP-ScopedClient", "EXE_FILES" => [], "LICENSE" => "perl", "NAME" => "AnyEvent::HTTP::ScopedClient", "PREREQ_PM" => { "AnyEvent::HTTP" => 0, "Encode" => 0, "HTTP::Request" => 0, "MIME::Base64" => 0, "Moose" => 0, "Try::Tiny" => 0, "URI" => 0, "URI::Escape" => 0, "URI::QueryParam" => 0, "namespace::autoclean" => 0 }, "TEST_REQUIRES" => { "AnyEvent" => 0, "Test::More" => 0, "strict" => 0, "utf8" => 0, "warnings" => 0 }, "VERSION" => "0.0.5", "test" => { "TESTS" => "t/*.t" } ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { my $tr = delete $WriteMakefileArgs{TEST_REQUIRES}; my $br = $WriteMakefileArgs{BUILD_REQUIRES}; for my $mod ( keys %$tr ) { if ( exists $br->{$mod} ) { $br->{$mod} = $tr->{$mod} if $tr->{$mod} > $br->{$mod}; } else { $br->{$mod} = $tr->{$mod}; } } } unless ( eval { ExtUtils::MakeMaker->VERSION(6.56) } ) { my $br = delete $WriteMakefileArgs{BUILD_REQUIRES}; my $pp = $WriteMakefileArgs{PREREQ_PM}; for my $mod ( keys %$br ) { if ( exists $pp->{$mod} ) { $pp->{$mod} = $br->{$mod} if $br->{$mod} > $pp->{$mod}; } else { $pp->{$mod} = $br->{$mod}; } } } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); scoped-client.t100644001750001750 135312161571204 21576 0ustar00hshonghshong000000000000AnyEvent-HTTP-ScopedClient-0.0.5/tuse utf8; use strict; use warnings; use Test::More tests => 2; use AnyEvent; use AnyEvent::HTTP::ScopedClient; my $cv = AE::cv; my $http = AnyEvent::HTTP::ScopedClient->new('http://www.naver.com/'); $cv->begin; $http->header( { 'Accept' => '*/*' } )->get( sub { my ( $body, $hdr ) = @_; diag("$hdr->{Status}: $hdr->{Reason}") if $hdr->{Status} !~ /^2/; is( $hdr->{Status}, 200, 'GET request' ); $cv->end; } ); $cv->begin; $http->post( { foo => 'bar', bar => 'baz', baz => '유니코드' }, sub { my ( $body, $hdr ) = @_; diag("$hdr->{Status}: $hdr->{Reason}") if $hdr->{Status} !~ /^2/; is( $hdr->{Status}, 200, 'POST request' ); $cv->end; } ); $cv->recv; HTTP000755001750001750 012161571204 21331 5ustar00hshonghshong000000000000AnyEvent-HTTP-ScopedClient-0.0.5/lib/AnyEventScopedClient.pm100644001750001750 1337412161571204 24433 0ustar00hshonghshong000000000000AnyEvent-HTTP-ScopedClient-0.0.5/lib/AnyEvent/HTTPpackage AnyEvent::HTTP::ScopedClient; { $AnyEvent::HTTP::ScopedClient::VERSION = '0.0.5'; } # ABSTRACT: L based L use Moose; use namespace::autoclean; use URI; use Try::Tiny; use MIME::Base64; use HTTP::Request; use Encode qw/encode_utf8/; use AnyEvent::HTTP; use URI::QueryParam; use URI::Escape; has 'options' => ( is => 'ro', isa => 'HashRef', ); sub request { my ( $self, $method, $reqBody, $callback ) = @_; if ( 'CODE' eq ref($reqBody) ) { $callback = $reqBody; undef $reqBody; } my %options = %{ $self->options }; try { my %headers = %{ $options{headers} }; if ( 'HASH' eq ref($reqBody) ) { my @pair; # push @pair, "$_=$reqBody->{$_}" for ( keys %$reqBody ); push @pair, "$_=" . uri_escape_utf8( $reqBody->{$_} ) for ( keys %$reqBody ); $reqBody = join( '&', @pair ); } my $sendingData = ( $method =~ m/^P/ && $reqBody && length $reqBody > 0 ) ? 1 : 0; $headers{'Content-Length'} = length $reqBody if $sendingData; $headers{'Content-Type'} = 'application/x-www-form-urlencoded' if ( $sendingData && !$headers{'Content-Type'} ); if ( $options{auth} ) { $headers{Authorization} = 'Basic ' . encode_base64( $options{auth}, '' ); } if ( $ENV{DEBUG} ) { print "$method " . $self->options->{url} . "\n"; while ( my ( $k, $v ) = each %headers ) { print "$k: $v\n"; } print "\n"; print "$reqBody\n" if $sendingData; } http_request( $method, $options{url}, headers => \%headers, body => $sendingData ? encode_utf8($reqBody) : undef, $callback ); } catch { $callback->($_) if $callback; }; return $self; } sub fullPath { my ( $self, $p ) = @_; } sub scope { my ( $self, $url, $options, $callback ) = @_; } sub join { my ( $self, $suffix ) = @_; } sub path { my ( $self, $p ) = @_; } sub query { my ( $self, $key, $value ) = @_; if ( 'HASH' eq ref $key ) { while ( my ( $k, $v ) = each %$key ) { $self->options->{url}->query_param( $k => $v ); } } else { $self->options->{url}->query_param( $key => $value ); } return $self; } sub host { my ( $self, $h ) = @_; } sub protocol { my ( $self, $p ) = @_; } sub auth { my ( $self, $user, $pass ) = @_; if ( !$user ) { $self->options->{auth} = undef; } elsif ( !$pass && $user =~ m/:/ ) { $self->options->{auth} = $user; } else { $self->options->{auth} = "$user:$pass"; } return $self; } sub header { my ( $self, $name, $value ) = @_; if ( 'HASH' eq ref $name ) { while ( my ( $k, $v ) = each %$name ) { $self->options->{headers}{$k} = $v; } } else { $self->options->{headers}{$name} = $value; } return $self; } sub headers { my ( $self, $h ) = @_; } sub buildOptions { my ( $self, $url, $params ) = @_; $params->{options}{url} = URI->new($url); $params->{options}{headers} ||= {}; } sub BUILDARGS { my ( $self, $url, %params ) = @_; $self->buildOptions( $url, \%params ); return \%params; } sub get { shift->request( 'GET', @_ ) } sub post { shift->request( 'POST', @_ ) } sub patch { shift->request( 'PATCH', @_ ) } sub put { shift->request( 'PUT', @_ ) } sub delete { shift->request( 'DELETE', @_ ) } sub head { shift->request( 'HEAD', @_ ) } __PACKAGE__->meta->make_immutable; 1; __END__ =pod =encoding utf-8 =head1 NAME AnyEvent::HTTP::ScopedClient - L based L =head1 VERSION version 0.0.5 =head1 SYNOPSIS my $client = AnyEvent::HTTP::ScopedClient->new('http://example.com'); $client->request('GET', sub { my ($body, $hdr) = @_; # $body is undef if error occured return if ( !$body || $hdr->{Status} !~ /^2/ ); # do something; }); # shorcut for GET $client->get(sub { my ($body, $hdr) = @_; # ... }); # Content-Type: application/x-www-form-urlencoded $client->post( { foo => 1, bar => 2 }, # note this. sub { my ($body, $hdr) = @_; # ... } ); # application/x-www-form-urlencoded post request $client->post( "foo=1&bar=2" # and note this. sub { my ($body, $hdr) = @_; # ... } ); # Content-Type: application/json use JSON::XS; $client->header('Content-Type', 'application/json') ->post( encode_json({ foo => 1 }), sub { my ($body, $hdr) = @_; # ... } ); $client->header('Accept', 'application/json') ->query({ key => 'value' }) ->query('key', 'value') ->get( sub { my ($body, $hdr) = @_; # ... } ); # headers at once $client->header({ Accept => '*/*', Authorization => 'Basic abcd' })->get( sub { my ($body, $hdr) = @_; # ... } ); =head1 DESCRIPTION L wrapper =head1 SEE ALSO L =head1 AUTHOR Hyungsuk Hong =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2012 by Hyungsuk Hong. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut