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