File Coverage

blib/lib/WWW/RabbitMQ/Broker.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package WWW::RabbitMQ::Broker;
2              
3 1     1   19683 use strict;
  1         2  
  1         35  
4 1     1   5 use warnings;
  1         2  
  1         38  
5              
6             our $VERSION = '0.01';
7              
8 1     1   1108 use LWP::UserAgent;
  1         48033  
  1         35  
9 1     1   9 use HTTP::Request;
  1         1  
  1         54  
10 1     1   425 use JSON;
  0            
  0            
11             use Want;
12             use URI;
13              
14             use WWW::RabbitMQ::Broker::Shovel;
15              
16             sub new
17             {
18             my $class = shift;
19             my $self = ref($_[0]) ? $_[0] : {@_};
20              
21             unless ($self->{username} and $self->{password} and $self->{host}) {
22             my @missing_arguments;
23             for my $key (qw/username password host/) {
24             push(@missing_arguments, $key) unless $self->{$key};
25             }
26             die "Missing arguments: " . join(', ', @missing_arguments) . "\n";
27             }
28              
29             $self->{base} ||= 'api'; # url will look like http://localhost:15672/api/
30             $self->{mode} ||= 'GET';
31             $self->{port} ||= '15672'; # default port for rabbitmq api
32             $self->{scheme} ||= 'http'; # by default https is not enabled
33              
34             $self->{uri} = URI->new("$self->{scheme}://$self->{host}:$self->{port}/");
35             return bless($self, $class);
36             }
37              
38             sub AUTOLOAD
39             {
40             my $self = shift;
41             our $AUTOLOAD;
42              
43             my ($key) = $AUTOLOAD =~ /.*::([\w_]+)/o;
44             return if ($key eq 'DESTROY');
45             push @{$self->{chain}}, $key;
46              
47             if (want('OBJECT') || want('VOID')) {
48             return $self;
49             }
50              
51             my $args = ref($_[0]) ? $_[0] : {@_};
52              
53             unshift(@{$self->{chain}}, $self->{base});
54             my $url = join('/', @{$self->{chain}});
55             $self->{chain} = [];
56             $self->{uri}->path($url);
57              
58             return $self->_apiCall($args);
59             }
60              
61             sub apiCall
62             {
63             my $self = shift;
64             my $method = "$self->{base}/" . shift;
65             my $args = shift || {};
66             $self->{uri}->path($method);
67             return $self->_apiCall($args);
68             }
69              
70             sub httpMethod
71             {
72             my ($self, $method) = @_;
73             $self->{mode} = $method;
74             return $self;
75             }
76              
77             sub getShovel
78             {
79             my $self = shift;
80             my $args = shift;
81             return WWW::RabbitMQ::Broker::Shovel->new($self, $args);
82             }
83              
84             sub _apiCall
85             {
86             my $self = shift;
87             my $args = shift;
88              
89             my $ua = LWP::UserAgent->new;
90             $ua->timeout(($self->{timeout} || 30));
91              
92             my $url = $self->{uri}->as_string;
93              
94             my $req = HTTP::Request->new($self->{mode} => $url);
95             $req->header('Content-Type' => 'application/json; charset=UTF-8');
96             $req->authorization_basic($self->{username}, $self->{password});
97              
98             my $parser = JSON->new->utf8(1);
99             my $json = $parser->encode($args);
100             $req->content($json);
101              
102             my $response = $ua->request($req);
103             $self->{mode} = 'GET';
104              
105             my $code = $response->code;
106             my $content = $response->content;
107              
108             if ($code == 200) {
109             my $results = $parser->decode($content);
110             return $results;
111             }
112              
113             if ($code == 204) {
114             return {success => 1};
115             }
116              
117             if ($code == 401) {
118             die "ERROR[401]: [username = $self->{username}, message => $content]\n";
119             }
120              
121             if ($code == 404) {
122             die "ERROR[404]: Not Found\n";
123             }
124              
125             if ($code == 500 && ($content =~ /timeout/)){
126             die "Error[500 - Timeout]: $content\n";
127             }
128             else {
129             die "Error[$code]: [url = $url, message = $content]\n";
130             }
131             }
132              
133             1;
134              
135             __END__