line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Future::HTTP::AnyEvent; |
2
|
1
|
|
|
1
|
|
588
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
3
|
1
|
|
|
1
|
|
810
|
use Future; |
|
1
|
|
|
|
|
10571
|
|
|
1
|
|
|
|
|
32
|
|
4
|
1
|
|
|
1
|
|
258
|
use AnyEvent::HTTP (); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use AnyEvent::Future 'as_future_cb'; |
6
|
|
|
|
|
|
|
BEGIN { |
7
|
|
|
|
|
|
|
# Versions before that didn't have as_future_cb |
8
|
|
|
|
|
|
|
AnyEvent::Future->VERSION(0.02) |
9
|
|
|
|
|
|
|
} |
10
|
|
|
|
|
|
|
use Moo 2; # or Moo::Lax if you can't have Moo v2 |
11
|
|
|
|
|
|
|
use Filter::signatures; |
12
|
|
|
|
|
|
|
no warnings 'experimental::signatures'; |
13
|
|
|
|
|
|
|
use feature 'signatures'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = '0.14'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
with 'Future::HTTP::Handler'; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 NAME |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
Future::HTTP::AnyEvent - asynchronous HTTP client with a Future interface |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=cut |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub BUILDARGS( $class, %options ) { |
26
|
|
|
|
|
|
|
return {} |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub is_async { !0 } |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub future_from_result { |
32
|
|
|
|
|
|
|
my( $self, $body, $headers ) = @_; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
$body ||= $headers->{Reason}; # just in case we didn't get a body at all |
35
|
|
|
|
|
|
|
my $res = Future->new(); |
36
|
|
|
|
|
|
|
$self->http_response_received( $res, $body, $headers ); |
37
|
|
|
|
|
|
|
$res |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub http_request($self,$method,$url,%options) { |
41
|
|
|
|
|
|
|
my $res = AnyEvent::Future->new()->then(sub ($body, $headers) { |
42
|
|
|
|
|
|
|
return $self->future_from_result($body, $headers); |
43
|
|
|
|
|
|
|
}); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my $r; |
46
|
|
|
|
|
|
|
$r = AnyEvent::HTTP::http_request($method => $url, %options, sub ($body, $headers) { |
47
|
|
|
|
|
|
|
undef $r; |
48
|
|
|
|
|
|
|
$res->done( $body,$headers ) |
49
|
|
|
|
|
|
|
}); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
$res |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub http_get($self,$url,%options) { |
55
|
|
|
|
|
|
|
$self->http_request('GET',$url, %options) |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub http_head($self,$url,%options) { |
59
|
|
|
|
|
|
|
$self->http_request('HEAD',$url, %options) |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub http_post($self,$url,$body, %options) { |
63
|
|
|
|
|
|
|
$self->http_request('POST',$url, body => $body, %options) |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 DESCRIPTION |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
This is the backend chosen if L or L are detected |
69
|
|
|
|
|
|
|
in C<%INC>. It will execute the requests asynchronously |
70
|
|
|
|
|
|
|
using L. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 METHODS |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head2 C<< Future::HTTP::AnyEvent->new() >> |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my $ua = Future::HTTP::AnyEvent->new(); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Creates a new instance of the HTTP client. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head2 C<< $ua->is_async() >> |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Returns true, because this backend is asynchronous. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head2 C<< $ua->http_get($url, %options) >> |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
$ua->http_get('http://example.com/', |
87
|
|
|
|
|
|
|
headers => { |
88
|
|
|
|
|
|
|
'Accept' => 'text/json', |
89
|
|
|
|
|
|
|
}, |
90
|
|
|
|
|
|
|
)->then(sub { |
91
|
|
|
|
|
|
|
my( $body, $headers ) = @_; |
92
|
|
|
|
|
|
|
... |
93
|
|
|
|
|
|
|
}); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Retrieves the URL and returns the body and headers, like |
96
|
|
|
|
|
|
|
the function in L. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 C<< $ua->http_head($url, %options) >> |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
$ua->http_head('http://example.com/', |
101
|
|
|
|
|
|
|
headers => { |
102
|
|
|
|
|
|
|
'Accept' => 'text/json', |
103
|
|
|
|
|
|
|
}, |
104
|
|
|
|
|
|
|
)->then(sub { |
105
|
|
|
|
|
|
|
my( $body, $headers ) = @_; |
106
|
|
|
|
|
|
|
... |
107
|
|
|
|
|
|
|
}); |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Retrieves the header of the URL and returns the headers, |
110
|
|
|
|
|
|
|
like the function in L. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head2 C<< $ua->http_post($url, $body, %options) >> |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
$ua->http_post('http://example.com/api', |
115
|
|
|
|
|
|
|
'{token:"my_json_token"}', |
116
|
|
|
|
|
|
|
headers => { |
117
|
|
|
|
|
|
|
'Accept' => 'text/json', |
118
|
|
|
|
|
|
|
}, |
119
|
|
|
|
|
|
|
)->then(sub { |
120
|
|
|
|
|
|
|
my( $body, $headers ) = @_; |
121
|
|
|
|
|
|
|
... |
122
|
|
|
|
|
|
|
}); |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Posts the content to the URL and returns the body and headers, |
125
|
|
|
|
|
|
|
like the function in L. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 C<< $ua->http_request($method, $url, %options) >> |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
$ua->http_request('PUT' => 'http://example.com/api', |
130
|
|
|
|
|
|
|
headers => { |
131
|
|
|
|
|
|
|
'Accept' => 'text/json', |
132
|
|
|
|
|
|
|
}, |
133
|
|
|
|
|
|
|
body => '{token:"my_json_token"}', |
134
|
|
|
|
|
|
|
)->then(sub { |
135
|
|
|
|
|
|
|
my( $body, $headers ) = @_; |
136
|
|
|
|
|
|
|
... |
137
|
|
|
|
|
|
|
}); |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Posts the content to the URL and returns the body and headers, |
140
|
|
|
|
|
|
|
like the function in L. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 SEE ALSO |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
L |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
L for the details of the API |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 REPOSITORY |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
The public repository of this module is |
151
|
|
|
|
|
|
|
L. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 SUPPORT |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
The public support forum of this module is |
156
|
|
|
|
|
|
|
L. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 BUG TRACKER |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Please report bugs in this module via the RT CPAN bug queue at |
161
|
|
|
|
|
|
|
L |
162
|
|
|
|
|
|
|
or via mail to L. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 AUTHOR |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Max Maischein C |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 COPYRIGHT (c) |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Copyright 2016-2020 by Max Maischein C. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 LICENSE |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
This module is released under the same terms as Perl itself. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=cut |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
1; |