File Coverage

blib/lib/Future/HTTP/AnyEvent.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


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