File Coverage

blib/lib/AnyEvent/Consul.pm
Criterion Covered Total %
statement 30 37 81.0
branch 1 2 50.0
condition n/a
subroutine 9 16 56.2
pod 0 9 0.0
total 40 64 62.5


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