AnyEvent-UserAgent

 view release on metacpan or  search on metacpan

META.json  view on Meta::CPAN

            "AnyEvent::HTTP" : "0",
            "HTTP::Cookies" : "0",
            "HTTP::Message" : "0",
            "Moo" : "0",
            "namespace::clean" : "0"
         }
      },
      "test" : {
         "requires" : {
            "AnyEvent" : "0",
            "HTTP::Request::Common" : "0",
            "Test::Deep" : "0",
            "Test::More" : "0"
         }
      }
   },
   "release_status" : "stable",
   "resources" : {
      "bugtracker" : {
         "web" : "http://github.com/AdCampRu/anyevent-useragent/issues"
      },

META.yml  view on Meta::CPAN

---
abstract: 'AnyEvent::HTTP OO-wrapper'
author:
  - 'Denis Ibaev <dionys@cpan.org>'
build_requires:
  AnyEvent: '0'
  ExtUtils::MakeMaker: '0'
  HTTP::Request::Common: '0'
  Test::Deep: '0'
  Test::More: '0'
configure_requires:
  ExtUtils::MakeMaker: '6.64'
dynamic_config: 1
generated_by: 'ExtUtils::MakeMaker version 7.64, CPAN::Meta::Converter version 2.150010'
license: perl
meta-spec:
  url: http://module-build.sourceforge.net/META-spec-v1.4.html
  version: '1.4'

Makefile.PL  view on Meta::CPAN

	NAME               => 'AnyEvent::UserAgent',
	AUTHOR             => 'Denis Ibaev <dionys@cpan.org>',
	VERSION_FROM       => 'lib/AnyEvent/UserAgent.pm',
	ABSTRACT_FROM      => 'lib/AnyEvent/UserAgent.pm',
	LICENSE            => 'perl',
	CONFIGURE_REQUIRES => {
		'ExtUtils::MakeMaker'   => '6.64',
	},
	TEST_REQUIRES      => {
		'AnyEvent'              => 0,
		'HTTP::Request::Common' => 0,
		'Test::Deep'            => 0,
		'Test::More'            => 0,
	},
	PREREQ_PM          => {
		'namespace::clean'      => 0,
		'AnyEvent::HTTP'        => 0,
		'HTTP::Cookies'         => 0,
		'HTTP::Message'         => 0,
		'Moo'                   => 0,
	},

lib/AnyEvent/UserAgent.pm  view on Meta::CPAN

package AnyEvent::UserAgent;

# This module based on original AnyEvent::HTTP::Simple module by punytan
# (punytan@gmail.com): http://github.com/punytan/AnyEvent-HTTP-Simple

use Moo;

use AnyEvent::HTTP ();
use HTTP::Cookies ();
use HTTP::Request ();
use HTTP::Request::Common ();
use HTTP::Response ();

use namespace::clean;

our $VERSION = '0.09';


has agent              => (is => 'rw', default => sub { $AnyEvent::HTTP::USERAGENT . ' AnyEvent-UserAgent/' . $VERSION });

has cookie_jar         => (is => 'rw', default => sub { HTTP::Cookies->new });

lib/AnyEvent/UserAgent.pm  view on Meta::CPAN


sub request {
	my $cb = pop();
	my ($self, $req, %opts) = @_;

	$self->_request($req, \%opts, sub {
		$self->_response($req, @_, $cb);
	});
}

sub get    { _do_request(\&HTTP::Request::Common::GET    => @_) }
sub head   { _do_request(\&HTTP::Request::Common::HEAD   => @_) }
sub put    { _do_request(\&HTTP::Request::Common::PUT    => @_) }
sub delete { _do_request(\&HTTP::Request::Common::DELETE => @_) }
sub post   { _do_request(\&HTTP::Request::Common::POST   => @_) }
sub patch   { _do_request(\&HTTP::Request::Common::PATCH   => @_) }
sub options   { _do_request(\&HTTP::Request::Common::OPTIONS   => @_) }

sub _do_request {
	my $cb   = pop();
	my $meth = shift();
	my $self = shift();

	$self->request($meth->(@_), $cb);
}

sub _request {

lib/AnyEvent/UserAgent.pm  view on Meta::CPAN

    my $ua = AnyEvent::UserAgent->new(request_timeout => 60);

Constructor for the user agent. You can pass it either a hash or a hash
reference with attribute values.

=head2 request

    $ua->request(GET 'http://example.com/', sub { print($_[0]->code) });

This method will dispatch the given request object. Normally this will be an
instance of the L<HTTP::Request> class, but any object with a similar interface
will do. The last argument must be a callback that will be called with a
response object as first argument. Response will be an instance of the
L<HTTP::Response> class.

This method is a wrapper for the L<C<AnyEvent::HTTP::http_request()>|AnyEvent::HTTP>
method. So you also can pass parameters for it, e.g.:

    $ua->request(GET 'http://example.com/', want_body_handle => 0, sub { print($_[0]->code) });

Full parameter list see at the L<AnyEvent::HTTP> documentation.

=head2 get

    $ua->get('http://example.com/', sub { print($_[0]->code) });

This method is a wrapper for the L<C<request()>|/request> method and the
L<C<HTTP::Request::Common::GET()>|HTTP::Request::Common/GET $url> function.
The last argument must be a callback.

=head2 head

This method is a wrapper for the L<C<request()>|/request> method and the
L<C<HTTP::Request::Common::HEAD()>|HTTP::Request::Common/HEAD $url> function.
See L<C<get()>|/get>.

=head2 put

This method is a wrapper for the L<C<request()>|/request> method and the
L<C<HTTP::Request::Common::PUT()>|HTTP::Request::Common/PUT $url> function.
See L<C<get()>|/get>.

=head2 delete

This method is a wrapper for the L<C<request()>|/request> method and the
L<C<HTTP::Request::Common::DELETE()>|HTTP::Request::Common/DELETE $url>
function. See L<C<get()>|/get>.

=head2 post

    $ua->post('http://example.com/', [key => 'value'], sub { print($_[0]->code) });

This method is a wrapper for the L<C<request()>|/request> method and the
L<C<HTTP::Request::Common::POST()>|HTTP::Request::Common/POST $url> function.
The last argument must be a callback.

=head2 patch

This method is a wrapper for the L<C<request()>|/request> method and the
L<C<HTTP::Request::Common::PATCH()>|HTTP::Request::Common/PATCH $url> function.
The last argument must be a callback.

=head2 options

This method is a wrapper for the L<C<request()>|/request> method and the
L<C<HTTP::Request::Common::OPTIONS()>|HTTP::Request::Common/OPTIONS $url>
function. The last argument must be a callback.

=head1 LIMITATIONS

Because of the handling of redirections done by this module as well as
L<AnyEvent::HTTP>, if you are connecting to a web server that does not implement
persistent connections (which is common for test servers) then you should pass a
C<persistent => 0> option to the C<AnyEvent::UserAgent> constructor otherwise
some requests might fail.

=head1 SEE ALSO

L<AnyEvent::HTTP>, L<HTTP::Cookies>, L<HTTP::Request::Common>, L<HTTP::Request>,
L<HTTP::Response>.

=head1 SUPPORT

=over 4

=item * Repository

L<http://github.com/AdCampRu/anyevent-useragent>

t/03-options-2.t  view on Meta::CPAN

#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;

use AnyEvent ();
use AnyEvent::UserAgent ();
use HTTP::Request::Common ();


subtest 'Set request options' => sub {
	no warnings 'prototype';
	no warnings 'redefine';

	*AnyEvent::HTTP::http_request = sub {
		my $cb = pop();
		my (undef, undef, %opts) = @_;

t/03-options-2.t  view on Meta::CPAN

		ok !exists($opts{foo});

		$cb->('', {Status => 200});
	};

	{
		my $ua = AnyEvent::UserAgent->new;
		my $cv = AE::cv;

		$ua->request(
			HTTP::Request::Common::GET('http://example.com/'),
			foo        => 'bar',
			persistent => 1,
			sub {
				$cv->send();
			}
		);
		$cv->recv();
	}

	{

t/03-options-2.t  view on Meta::CPAN

		my $cv = AE::cv;

		$ua->get('http://example.com/', sub { $cv->send() });
		$cv->recv();
	}

	{
		my $ua = AnyEvent::UserAgent->new(persistent => 1);
		my $cv = AE::cv;

		$ua->request(HTTP::Request::Common::GET('http://example.com/'), persistent => undef, sub { $cv->send() });
		$cv->recv();
	}
};


done_testing;



( run in 0.712 second using v1.01-cache-2.11-cpan-de7293f3b23 )