File: AnyEvent.pm

package info (click to toggle)
libxml-rpc-fast-perl 0.8-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 336 kB
  • sloc: perl: 3,246; makefile: 2
file content (102 lines) | stat: -rw-r--r-- 1,717 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package XML::RPC::UA::AnyEvent;

use strict;
use warnings;
use HTTP::Response;
use HTTP::Headers;
use AnyEvent::HTTP 'http_request';
use Carp;

use XML::RPC::Fast ();
our $VERSION = $XML::RPC::Fast::VERSION;

=head1 NAME

XML::RPC::UA::AnyEvent - XML::RPC useragent, using AnyEvent::HTTP

=head1 SYNOPSIS

    use XML::RPC::Fast;
    use XML::RPC::UA::AnyEvent;
    
    my $rpc = XML::RPC::Fast->new(
        $uri,
        ua => XML::RPC::UA::AnyEvent->new(
            ua      => 'YourApp/0.1',
            timeout => 3,
        ),
    );

=head1 DESCRIPTION

Asyncronous useragent for L<XML::RPC::Fast>. Could be used in any AnyEvent application.

=head1 IMPLEMENTED METHODS

=head2 new

=head2 async = 1

=head2 call

=head1 SEE ALSO

=over 4

=item * L<XML::RPC::UA>

Base class (also contains documentation)

=item * L<XML::RPC::UA::AnyEventSync>

Syncronous UA using AnyEvent

=item * L<AnyEvent>

DBI of event-loop programming

=item * L<AnyEvent::HTTP>

HTTP-client using AnyEvent

=back

=cut


sub async { 1 }

sub new {
	my $pkg = shift;
	my %args = @_;
	return bless \(do {my $o = $args{ua} || 'XML-RPC-Fast/'.$XML::RPC::Fast::VERSION }),$pkg;
}

sub call {
	my $self = shift;
	my ($method, $url) = splice @_,0,2;
	my %args = @_;
	$args{cb} or croak "cb required for useragent @{[%args]}";
	#warn "call";
	http_request
		$method => $url,
		headers => {
			'Content-Type'   => 'text/xml',
			'User-Agent'     => $$self,
			do { use bytes; ( 'Content-Length' => length($args{body}) ) },
			%{$args{headers} || {}},
		},
		body => $args{body},
		cb => sub {
			$args{cb}( HTTP::Response->new(
				$_[1]{Status},
				$_[1]{Reason},
				HTTP::Headers->new(%{$_[1]}),
				$_[0],
			) );
		},
	;
	return;
}

1;