AnyEvent-HTTP-Message

 view release on metacpan or  search on metacpan

META.json  view on Meta::CPAN

            "List::Util" : "0",
            "Test::More" : "0.88"
         }
      }
   },
   "provides" : {
      "AnyEvent::HTTP::Message" : {
         "file" : "lib/AnyEvent/HTTP/Message.pm",
         "version" : "0.302"
      },
      "AnyEvent::HTTP::Request" : {
         "file" : "lib/AnyEvent/HTTP/Request.pm",
         "version" : "0.302"
      },
      "AnyEvent::HTTP::Response" : {
         "file" : "lib/AnyEvent/HTTP/Response.pm",
         "version" : "0.302"
      }
   },
   "release_status" : "stable",
   "resources" : {

META.yml  view on Meta::CPAN

    - xt
  namespace:
    - Local
    - t::lib
  package:
    - DB
provides:
  AnyEvent::HTTP::Message:
    file: lib/AnyEvent/HTTP/Message.pm
    version: 0.302
  AnyEvent::HTTP::Request:
    file: lib/AnyEvent/HTTP/Request.pm
    version: 0.302
  AnyEvent::HTTP::Response:
    file: lib/AnyEvent/HTTP/Response.pm
    version: 0.302
requires:
  AnyEvent::HTTP: 2
  Carp: 0
  Scalar::Util: 0
  parent: 0

lib/AnyEvent/HTTP/Message.pm  view on Meta::CPAN

  # don't use this directly

=head1 DESCRIPTION

This is a base class for:

=over 4

=item *

L<AnyEvent::HTTP::Request>

=item *

L<AnyEvent::HTTP::Response>

=back

=head1 CLASS METHODS

=head2 new

lib/AnyEvent/HTTP/Request.pm  view on Meta::CPAN

# This file is part of AnyEvent-HTTP-Message
#
# This software is copyright (c) 2012 by Randy Stauner.
#
# This is free software; you can redistribute it and/or modify it under
# the same terms as the Perl 5 programming language system itself.
#
use strict;
use warnings;

package AnyEvent::HTTP::Request;
{
  $AnyEvent::HTTP::Request::VERSION = '0.302';
}
BEGIN {
  $AnyEvent::HTTP::Request::AUTHORITY = 'cpan:RWSTAUNER';
}
# ABSTRACT: HTTP Request object for AnyEvent::HTTP

use parent 'AnyEvent::HTTP::Message';


sub new {
  my $class = shift;
  my $self = $class->SUPER::new(@_);

lib/AnyEvent/HTTP/Request.pm  view on Meta::CPAN

sub send {
  my ($self) = @_;
  require AnyEvent::HTTP;
  # circumvent the sub's prototype
  &AnyEvent::HTTP::http_request( $self->args );
}


sub to_http_message {
  my ($self) = @_;
  require HTTP::Request;

  my $res = HTTP::Request->new(
    $self->method,
    $self->uri,
    [ %{ $self->headers } ],
    $self->body
  );
  return $res;
}

1;

__END__

=pod

=encoding utf-8

=for :stopwords Randy Stauner ACKNOWLEDGEMENTS TODO featureful http uri cb params

=head1 NAME

AnyEvent::HTTP::Request - HTTP Request object for AnyEvent::HTTP

=head1 VERSION

version 0.302

=head1 SYNOPSIS

  # parses the same argument list as AnyEvent::HTTP::http_request
  my $req = AnyEvent::HTTP::Request->new(
    POST => $uri,
    body => $body,
    headers => \%headers,
    %params,
    sub { ... }
  );

  # provides introspection
  print $req->header('user-agent');
  print $req->uri;

  # can be upgraded to an HTTP::Request object
  my $http_req = $req->to_http_message;

  # or submitted via AnyEvent::HTTP::http_request
  $req->send();

=head1 DESCRIPTION

This class creates a lightweight object
to represent an HTTP request as used by L<AnyEvent::HTTP>.

lib/AnyEvent/HTTP/Request.pm  view on Meta::CPAN

Instead of code that looks something like this:

  is $args[0],       'POST',              'request method';
  is $args[1],       'http://some/where', 'request uri';
  is ref($args[-1]), 'CODE',              'http_request callback';
  is_deeply { @args[ 2 .. $#args - 1 ] }->{headers},
    \%expected_headers, 'request headers';

You can write clearer, simpler code like this:

  my $req = AnyEvent::HTTP::Request->new(@args);

  is $req->method,  'POST',              'request method';
  is $req->uri,     'http://some/where', 'request uri';
  is ref($req->cb), 'CODE',              'http_request callback';
  is_deeply $req->headers, \%expected_headers, 'request headers';

It's a little less weird, and easier to maintain (and do again).

This class also allows you to build an object by passing a hashref
of named parameters in case you'd prefer that.
You can then call L</send> to actually make the request
(via L<AnyEvent::HTTP/http_request>),
or L</args> to get the list of arguments the object would pass.

It can also be converted L<from|/from_http_message> or L<to|/to_http_message>
the more featureful
L<HTTP::Request>.

=head1 CLASS METHODS

=head2 new

Accepts the same argument list as
L<AnyEvent::HTTP/http_request>
(see L</parse_args>):

  AnyEvent::HTTP::Request->new(
    $method => $uri,
    body    => $body,
    headers => \%headers,
    %params,
    sub { ... }
  );

Alternatively accepts an instance of
L<HTTP::Request>
with an optional hashref of extra attributes
(see L</from_http_message>):

  AnyEvent::HTTP::Request->new(
    HTTP::Request->new( $method, $uri, $headers, $body ),
    {
      cb => sub { ... },
      params => \%params,
    }
  );

Also accepts a single hashref of named attributes
(see L</ATTRIBUTES>):

  AnyEvent::HTTP::Request->new({
    method  => 'POST',
    uri     => 'http://example.com',
    cb      => sub { ... },
    params  => \%params,
    headers => \%headers,
    body    => $body,
  });

=head2 parse_args

lib/AnyEvent/HTTP/Request.pm  view on Meta::CPAN

The list should look like
C<< ($method, $uri, %optional, \&callback) >>
where the C<%optional> hash may include C<body>, C<headers>,
and any of the other options accepted by
L<AnyEvent::HTTP/http_request>
(which will become L</params>).

=head2 from_http_message

Called by the constructor
when L</new> is passed an instance of L<HTTP::Request>.

Since only C<method>, C<uri>, C<headers>, and C<body>
can be determined from L<HTTP::Request>,
a hashref can be passed as a second parameter
containing C<cb> and C<params>.

=head1 ATTRIBUTES

=head2 method

Request method (GET, POST, etc)
(first argument to L<AnyEvent::HTTP/http_request>)

lib/AnyEvent/HTTP/Request.pm  view on Meta::CPAN

or any list of arguments that can be passed to
L<AnyEvent::HTTP::Response/new>.

=head2 send

Actually submit the request by passing L</args>
to L<AnyEvent::HTTP/http_request>

=head2 to_http_message

Returns an instance of L<HTTP::Request>
to provide additional functionality.

B<Note> that L</cb> and L</params>
will not be represented in the L<HTTP::Request> object
(since they are for the user-agent and not the request).

=for test_synopsis my ($uri, $body, %headers, %params);

=head1 SEE ALSO

=over 4

=item *

L<AnyEvent::HTTP>

=item *

L<AnyEvent::HTTP::Message> (base class)

=item *

L<HTTP::Request> - More featureful object

=back

=head1 AUTHOR

Randy Stauner <rwstauner@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Randy Stauner.

lib/AnyEvent/HTTP/Response.pm  view on Meta::CPAN

      if( !$http_res->is_success ){
        print $http_res->status_line;
      }
    }
  );

=head1 DESCRIPTION

This object represents an HTTP response from L<AnyEvent::HTTP>.

This is a companion class to L<AnyEvent::HTTP::Request>.

It parses the arguments passed to the final callback in
L<AnyEvent::HTTP/http_request>
(or produces the arguments that should be passed to that,
depending on how you'd like to use it).
and wraps them in an object.

It can also be converted L<from|/from_http_message> or L<to|/to_http_message>
the more featureful
L<HTTP::Response>.

t/request.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More 0.88;
use lib 't/lib';
use AEHTTP_Tests;

my $mod = 'AnyEvent::HTTP::Request';
eval "require $mod" or die $@;

use AnyEvent::HTTP;
no warnings 'redefine';
local *AnyEvent::HTTP::http_request = sub ($$@) {
  return $mod->new(@_);
};
use warnings;

# parse_args error

t/request.t  view on Meta::CPAN

    is $msg->method, 'YAWN', 'method';
    is $msg->uri, 'horse://sense', 'uri';
    is $msg->header('Wa'), 'hoo', 'single header';
    is $msg->header('X-Wa'), 'x-hoo', 'single header';
    is $msg->content, 'by cowboy', 'body/content';
  };
}

test_http_message sub {
  # The POST function from Common automatically adds Content-Length header.
  require HTTP::Request::Common;
  my $msg = HTTP::Request::Common::POST(
    'blue://buildings',
    (
      x_rain     => 'king',
      user_agent => 'perfect',
      User_Agent => 'round here',
      content_type => 'text/plain',
    ),
    content => 'anna begins',
  );
  isa_ok($msg, 'HTTP::Request');

  my $cb = sub { 'counting ' . shift };
  my $suffix = 'from HTTP::Request';
  my $req = new_ok($mod, [$msg, {cb => $cb}]);
  is $req->method, 'POST', "method $suffix";
  is $req->uri, 'blue://buildings', "uri $suffix";
  is $req->body, 'anna begins', "body $suffix";

  is $msg->header('content-length'), 11,
    'message object has content length header';

  is_deeply
    $req->headers,



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