File Coverage

blib/lib/WWW/RabbitMQ/Broker.pm
Criterion Covered Total %
statement 33 91 36.2
branch 2 22 9.0
condition 6 24 25.0
subroutine 9 14 64.2
pod 0 4 0.0
total 50 155 32.2


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