| 1 | package Net::Async::Consul; | ||||||||||
| 2 | 4 | $Net::Async::Consul::VERSION = '0.002'; | |||||||||
| 3 | # ABSTRACT: Make async calls to Consul via IO::Async | ||||||||||
| 4 | |||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 4 | ▶ | use Consul 0.016; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 4 | ▶ | use Net::Async::HTTP; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 4 | ▶ | use HTTP::Request; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 4 | ▶ | use HTTP::Headers; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 4 | ▶ | use Hash::MultiValue; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 4 | ▶ | use List::Util 1.29 qw(pairmap); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 4 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | sub new { | ||||||||||
| 17 | 2 | ▶ | my ($class, %args) = @_; | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||
| 18 | |||||||||||
| 19 | 2 | my $loop = delete $args{loop}; | |||||||||
| 20 | 2 | ▶ | croak "missing required param: loop" unless $loop; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 2 | my $http = Net::Async::HTTP->new( | |||||||||
| 22 | max_connections_per_host => 4, | ||||||||||
| 23 | max_in_flight => 4, | ||||||||||
| 24 | ); | ||||||||||
| 25 | 2 | $loop->add($http); | |||||||||
| 26 | |||||||||||
| 27 | Consul->new(%args, | ||||||||||
| 28 | request_cb => sub { | ||||||||||
| 29 | 2 | ▶ | my ($self, $req) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.5
| |||||||||||
| 30 | $http->do_request( | ||||||||||
| 31 | request => HTTP::Request->new( | ||||||||||
| 32 | $req->method, | ||||||||||
| 33 | $req->url, | ||||||||||
| 34 | 2 | HTTP::Headers->new(%{$req->headers->as_hashref}), | |||||||||
| 35 | $req->content, | ||||||||||
| 36 | ), | ||||||||||
| 37 | timeout => $self->timeout, | ||||||||||
| 38 | on_response => sub { | ||||||||||
| 39 | 0 | ▶ | my ($r) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 40 | $req->callback->(Consul::Response->new( | ||||||||||
| 41 | status => $r->code, | ||||||||||
| 42 | reason => $r->message,, | ||||||||||
| 43 | 0 | ▶ | headers => Hash::MultiValue->new(pairmap { (lc($a) => $b) } $r->headers->flatten), | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 44 | content => $r->content, | ||||||||||
| 45 | request => $req, | ||||||||||
| 46 | )); | ||||||||||
| 47 | }, | ||||||||||
| 48 | on_error => sub { | ||||||||||
| 49 | 2 | ▶ | $req->callback->(Consul::Response->new( | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 50 | status => 599, | ||||||||||
| 51 | reason => "internal error: @_", | ||||||||||
| 52 | request => $req, | ||||||||||
| 53 | )); | ||||||||||
| 54 | } | ||||||||||
| 55 | 2 | ); | |||||||||
| 56 | 2 | return; | |||||||||
| 57 | }, | ||||||||||
| 58 | 2 | ); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 0 | ▶ | sub acl { shift->new(@_)->acl } | ||||||||
|
Subroutine
acl: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 62 | 2 | ▶ | sub agent { shift->new(@_)->agent } | ||||||||
|
Subroutine
agent: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 63 | 0 | ▶ | sub catalog { shift->new(@_)->catalog } | ||||||||
|
Subroutine
catalog: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 64 | 0 | ▶ | sub event { shift->new(@_)->event } | ||||||||
|
Subroutine
event: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 65 | 0 | ▶ | sub health { shift->new(@_)->health } | ||||||||
|
Subroutine
health: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 66 | 0 | ▶ | sub kv { shift->new(@_)->kv } | ||||||||
|
Subroutine
kv: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 67 | 0 | ▶ | sub session { shift->new(@_)->session } | ||||||||
|
Subroutine
session: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 68 | 0 | ▶ | sub status { shift->new(@_)->status } | ||||||||
|
Subroutine
status: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 69 | |||||||||||
| 70 | 4 | 1; | |||||||||
| 71 | |||||||||||
| 72 | =pod | ||||||||||
| 73 | |||||||||||
| 74 | =encoding UTF-8 | ||||||||||
| 75 | |||||||||||
| 76 | =for markdown [](http://travis-ci.org/robn/Net-Async-Consul) | ||||||||||
| 77 | |||||||||||
| 78 | =head1 NAME | ||||||||||
| 79 | |||||||||||
| 80 | Net::Async::Consul - Make async calls to Consul via IO::Async | ||||||||||
| 81 | |||||||||||
| 82 | =head1 SYNOPSIS | ||||||||||
| 83 | |||||||||||
| 84 | use IO::Async::Loop; | ||||||||||
| 85 | use Net::Async::Consul; | ||||||||||
| 86 | |||||||||||
| 87 | my $loop = IO::Async::Loop->new; | ||||||||||
| 88 | |||||||||||
| 89 | my $kv = Net::Async::Consul->kv(loop => $loop); | ||||||||||
| 90 | |||||||||||
| 91 | # do some blocking op to discover the current index | ||||||||||
| 92 | $kv->get("mykey", cb => sub { | ||||||||||
| 93 | my ($v, $meta) = @_; | ||||||||||
| 94 | |||||||||||
| 95 | # now set up a long-poll to watch a key we're interested in | ||||||||||
| 96 | $kv->get("mykey", index => $meta->index, cb => sub { | ||||||||||
| 97 | my ($v, $meta) = @_; | ||||||||||
| 98 | say "mykey changed to ".$v->value; | ||||||||||
| 99 | $loop->stop; | ||||||||||
| 100 | }); | ||||||||||
| 101 | }); | ||||||||||
| 102 | |||||||||||
| 103 | # make the change | ||||||||||
| 104 | $kv->put("mykey" => "newval"); | ||||||||||
| 105 | |||||||||||
| 106 | $loop->run; | ||||||||||
| 107 | |||||||||||
| 108 | =head1 DESCRIPTION | ||||||||||
| 109 | |||||||||||
| 110 | Net::Async::Consul is a thin wrapper around L<Consul> to connect it to | ||||||||||
| 111 | L<Net::Async::HTTP> for asynchronous operation. | ||||||||||
| 112 | |||||||||||
| 113 | It takes the same arguments and methods as L<Consul> itself, so see the | ||||||||||
| 114 | documentation for that module for details. The important difference is that you | ||||||||||
| 115 | must pass the C<loop> option with the loop object to API methods, the C<cb> | ||||||||||
| 116 | option to the endpoint methods to enable their asynchronous mode. | ||||||||||
| 117 | |||||||||||
| 118 | There's also a C<on_error> argument. If you pass in a coderef for this | ||||||||||
| 119 | argument, it will be called with a single string arg whenever something goes | ||||||||||
| 120 | wrong internally (usually a HTTP failure). Use it to safely log or cleanup | ||||||||||
| 121 | after the error. | ||||||||||
| 122 | |||||||||||
| 123 | =head1 SUPPORT | ||||||||||
| 124 | |||||||||||
| 125 | =head2 Bugs / Feature Requests | ||||||||||
| 126 | |||||||||||
| 127 | Please report any bugs or feature requests through the issue tracker | ||||||||||
| 128 | at L<https://github.com/robn/Net-Async-Consul/issues>. | ||||||||||
| 129 | You will be notified automatically of any progress on your issue. | ||||||||||
| 130 | |||||||||||
| 131 | =head2 Source Code | ||||||||||
| 132 | |||||||||||
| 133 | This is open source software. The code repository is available for | ||||||||||
| 134 | public review and contribution under the terms of the license. | ||||||||||
| 135 | |||||||||||
| 136 | L<https://github.com/robn/Net-Async-Consul> | ||||||||||
| 137 | |||||||||||
| 138 | git clone https://github.com/robn/Net-Async-Consul.git | ||||||||||
| 139 | |||||||||||
| 140 | =head1 AUTHORS | ||||||||||
| 141 | |||||||||||
| 142 | =over 4 | ||||||||||
| 143 | |||||||||||
| 144 | =item * | ||||||||||
| 145 | |||||||||||
| 146 | Rob N ★ <robn@robn.io> | ||||||||||
| 147 | |||||||||||
| 148 | =back | ||||||||||
| 149 | |||||||||||
| 150 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 151 | |||||||||||
| 152 | This software is copyright (c) 2018 by Rob N ★. | ||||||||||
| 153 | |||||||||||
| 154 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 155 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 156 | |||||||||||
| 157 | =cut | ||||||||||