File Coverage

blib/lib/AnyEvent/Consul.pm
Criterion Covered Total %
statement 18 41 43.9
branch 0 4 0.0
condition n/a
subroutine 6 17 35.2
pod 0 9 0.0
total 24 71 33.8


line stmt bran cond sub pod time code
1             package AnyEvent::Consul;
2             $AnyEvent::Consul::VERSION = '0.004';
3             # ABSTRACT: Make async calls to Consul via AnyEvent
4              
5 3     3   233515 use warnings;
  3         7  
  3         99  
6 3     3   17 use strict;
  3         7  
  3         62  
7              
8 3     3   2182 use Consul;
  3         839519  
  3         135  
9 3     3   3380 use AnyEvent::HTTP qw(http_request);
  3         88319  
  3         275  
10 3     3   35 use Hash::MultiValue;
  3         6  
  3         79  
11 3     3   16 use Carp qw(croak);
  3         4  
  3         1506  
12              
13             sub new {
14 0     0 0   shift;
15             Consul->new(@_,
16             request_cb => sub {
17 0     0     my ($self, $req) = @_;
18             http_request($req->method, $req->url,
19             body => $req->content,
20             headers => $req->headers->as_hashref,
21             timeout => $self->timeout,
22             sub {
23 0           my ($rdata, $rheaders) = @_;
24 0           my $rstatus = delete $rheaders->{Status};
25 0           my $rreason = delete $rheaders->{Reason};
26 0           delete $rheaders->{$_} for grep { m/^[A-Z]/ } keys %$rheaders;
  0            
27 0 0         $req->callback->(Consul::Response->new(
28             status => $rstatus,
29             reason => $rreason,
30             headers => Hash::MultiValue->from_mixed($rheaders),
31             content => defined $rdata ? $rdata : "",
32             request => $req,
33             ));
34             },
35 0           );
36 0           return;
37             },
38             error_cb => sub {
39 0     0     my ($msg, $resp) = @_;
40 0           my $on_error = $resp->request->args->{on_error};
41 0 0         return $on_error->($msg) if ref $on_error eq 'CODE';
42 0           croak $msg;
43             },
44 0           );
45             }
46              
47 0     0 0   sub acl { shift->new(@_)->acl }
48 0     0 0   sub agent { shift->new(@_)->agent }
49 0     0 0   sub catalog { shift->new(@_)->catalog }
50 0     0 0   sub event { shift->new(@_)->event }
51 0     0 0   sub health { shift->new(@_)->health }
52 0     0 0   sub kv { shift->new(@_)->kv }
53 0     0 0   sub session { shift->new(@_)->session }
54 0     0 0   sub status { shift->new(@_)->status }
55              
56             1;
57              
58             =pod
59              
60             =encoding UTF-8
61              
62             =head1 NAME
63              
64             AnyEvent::Consul - Make async calls to Consul via AnyEvent
65              
66             =head1 SYNOPSIS
67              
68             use AnyEvent;
69             use AnyEvent::Consul;
70            
71             my $cv = AE::cv;
72            
73             my $kv = AnyEvent::Consul->kv;
74              
75             # do some blocking op to discover the current index
76             $kv->get("mykey", cb => sub {
77             my ($v, $meta) = @_;
78            
79             # now set up a long-poll to watch a key we're interested in
80             $kv->get("mykey", index => $meta->index, cb => sub {
81             my ($v, $meta) = @_;
82             say "mykey changed to ".$v->value;
83             $cv->send;
84             });
85             });
86            
87             # make the change
88             $kv->put("mykey" => "newval");
89            
90             $cv->recv;
91              
92             =head1 DESCRIPTION
93              
94             AnyEvent::Consul is a thin wrapper around L to connect it to
95             L for asynchronous operation.
96              
97             It takes the same arguments and methods as L itself, so see the
98             documentation for that module for details. The important difference is that you
99             must pass the C option to the endpoint methods to enable their asynchronous
100             mode.
101              
102             There's also a C argument. If you pass in a coderef for this
103             argument, it will be called with a single string arg whenever something goes
104             wrong internally (usually a HTTP failure). Use it to safely log or cleanup
105             after the error.
106              
107             =head1 SUPPORT
108              
109             =head2 Bugs / Feature Requests
110              
111             Please report any bugs or feature requests through the issue tracker
112             at L.
113             You will be notified automatically of any progress on your issue.
114              
115             =head2 Source Code
116              
117             This is open source software. The code repository is available for
118             public review and contribution under the terms of the license.
119              
120             L
121              
122             git clone https://github.com/robn/AnyEvent-Consul.git
123              
124             =head1 AUTHORS
125              
126             =over 4
127              
128             =item *
129              
130             Robert Norris
131              
132             =back
133              
134             =head1 COPYRIGHT AND LICENSE
135              
136             This software is copyright (c) 2015 by Robert Norris.
137              
138             This is free software; you can redistribute it and/or modify it under
139             the same terms as the Perl 5 programming language system itself.
140              
141             =cut