File Coverage

blib/lib/WebService/PagerDuty/Request.pm
Criterion Covered Total %
statement 51 54 94.4
branch 7 14 50.0
condition 6 18 33.3
subroutine 12 14 85.7
pod 1 3 33.3
total 77 103 74.7


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl -w
2              
3             ## workaround for PkgVersion
4             ## no critic
5             package WebService::PagerDuty::Request;
6             {
7             $WebService::PagerDuty::Request::VERSION = '1.20131219.1627';
8             }
9             ## use critic
10 2     2   11 use strict;
  2         4  
  2         65  
11 2     2   10 use warnings;
  2         4  
  2         52  
12              
13 2     2   12 use base qw/ WebService::PagerDuty::Base /;
  2         3  
  2         158  
14 2     2   3834 use HTTP::Request;
  2         68763  
  2         78  
15 2     2   3110 use LWP::UserAgent;
  2         118518  
  2         105  
16 2     2   8345 use JSON;
  2         32757  
  2         15  
17 2     2   402 use URI;
  2         4  
  2         57  
18 2     2   1951 use URI::QueryParam;
  2         1384  
  2         142  
19 2     2   1310 use WebService::PagerDuty::Response;
  2         9  
  2         24  
20              
21             __PACKAGE__->mk_ro_accessors(qw/ agent /);
22              
23             sub new {
24 2     2 1 19241 my $self = shift;
25             $self->SUPER::new(
26             _defaults => {
27             agent => sub {
28 0     0   0 LWP::UserAgent->new;
29             },
30             },
31             @_
32 2         34 );
33             }
34              
35             sub get_data {
36 2     2 0 32 my $self = shift;
37 2         13 return $self->_perform_request( method => 'GET', @_ );
38             }
39              
40             sub post_data {
41 0     0 0 0 my $self = shift;
42 0         0 return $self->_perform_request( method => 'POST', @_ );
43             }
44              
45             sub _perform_request {
46 2     2   12 my ( $self, %args ) = @_;
47              
48 2         5 my $method = delete $args{method};
49 2         7 my $url = delete $args{url};
50 2         6 my $user = delete $args{user};
51 2         5 my $password = delete $args{password};
52 2         5 my $api_key = delete $args{api_key};
53 2         5 my $params = delete $args{params};
54 2         5 my $body = {%args};
55              
56 2 50       16 die( 'Unknown method: ' . $method ) unless $method =~ m/^(get|post)$/io;
57 2 100 66     29 die( 'api_key and user/password are mutually exclusive') if $api_key && ( $user || $password );
      33        
58              
59 1 0 33     6 $url->query_form_hash($params) if $params && ref($params) && ref($params) eq 'HASH' && %$params;
      33        
      0        
60              
61 1         9 my $headers = HTTP::Headers->new;
62 1 50       18 $headers->header( 'Content-Type' => 'application/json' ) if %$body;
63 1 50 33     5 $headers->authorization_basic( $user, $password ) if $user && $password;
64 1 50       8 $headers->header( 'Authorization' => "Token token=$api_key" ) if $api_key;
65              
66 1         51 my $content = '';
67 1 50       3 $content = to_json($body) if %$body;
68              
69 1         8 my $request = HTTP::Request->new( $method, $url, $headers, $content );
70              
71 1         270 my $response = $self->agent->request($request);
72              
73 1         3412 return WebService::PagerDuty::Response->new($response);
74             }
75              
76             1;
77              
78             __END__