blib/lib/JSON/API.pm

filter: Statementstmt 89.2% 116 / 130 Branchbran 65.3% 34 / 52 Conditioncond 75.0% 12 / 16 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 91.6% 22 / 24 Podpod 100% 10 / 10 totaltotal 82.5% 199 / 241 CC 34 SCAR 20.7
CC
34
Cov
82%
_http_req
19.3
header
12.0
new
8.0
CRAP 7.9
↑ Summary Build »
1 package JSON::API;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use HTTP::Status qw/:constants/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use URI::Encode qw/uri_encode/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 BEGIN {
10 4 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶ $VERSION = v1.1.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 @ISA = qw(Exporter);
14 #Give a hoot don't pollute, do not export more than needed by default
15 4 @EXPORT = qw();
16 4 @EXPORT_OK = qw();
17 4 %EXPORT_TAGS = ();
18 }
19  
20 sub _debug
21 {
22 164 ▶ my ($self, @lines) = @_;
Subroutine _debug: called CC=2 SCAR=6.9
23 164 my $output = join('\n', @lines);
24 164 ▶ print STDERR $output . "\n" if ($self->{debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 1 163 164
25 }
26  
27 sub _server
28 {
29 39 ▶ my ($self, $input) = @_;
Subroutine _server: called CC=2 SCAR=6.9
30 39 $input =~ s|^(https?://)?||;
31 39 $input =~ m|^([^\s:/]+)(:\d+)?.*|;
32 39 ▶ $input = $1 . ($2 || '');
Condition100%2/2
$2 || ''
l!l
831
MC/DC100%1/1
$2 || ''
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
33 39 return $input;
34 }
35  
36 sub _http_req
37 {
38 22 ▶ my ($self, $method, $path, $data, $apphdr) = @_;
Subroutine _http_req: called CC=15 SCAR=29.6
39 22 $self->_debug('_http_req called with the following:',Dumper($method,$path,$data, $apphdr));
40  
41 22 my $url = $self->url($path);
42 22 $self->_debug("URL calculated to be: $url");
43 22 delete $self->{response};
44  
45 22 my $headers = HTTP::Headers->new(
46 'Accept' => 'application/json',
47 'Content-Type' => 'application/json',
48 );
49 22 ▶ if( $apphdr && ref $apphdr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($apphdr and ref $apphdr) 0 22 22
Condition33%1/3
$apphdr and ref $apphdr
!ll&&!rl&&r
2200
MC/DC0%0/2
$apphdr and ref $apphdr
$apphdr ref $apphdr
Truth table33%1/3
A and B
A= $apphdrB= ref $apphdr
ABresult
0X0
100
111
50 0 $headers->header( $_, $apphdr->{$_} ) foreach (keys %$apphdr);
51 }
52 22 my $json;
53 22 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 6 16 22
54 6 $json = $self->_encode($data);
55 6 ▶ return (wantarray ? (500, {}) : {}) unless defined $json;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $json 0 6 6
wantarray ? : 0 0 0
56 }
57  
58 22 my $req = HTTP::Request->new($method, $url, $headers, $json);
59 22 $self->_debug("Requesting: ",Dumper($req));
60 22 my $res = $self->{user_agent}->request($req);
61  
62 22 $self->_debug("Response: ",Dumper($res));
63 22 $self->{response} = $res;
64 22 ▶ if ($res->is_success) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res->is_success) { } 16 6 22
elsif ($res->code == 304) { } 0 6 6
65 16 $self->{has_error} = 0;
66 16 $self->{error_string} = '';
67 16 $self->_debug("Successful request detected");
68 } elsif ($res->code == HTTP_NOT_MODIFIED) {
69 return wantarray ?
70 0 ▶ ($res->code, {}) :
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
71 {};
72 } else {
73 6 $self->{has_error} = 1;
74 6 $self->{error_string} = $res->content;
75 6 $self->_debug("Error detected: ".$self->{error_string});
76 # If internal warning, return before decoding, as it will fail + overwrite the error_string
77 6 ▶ if ($res->header('client-warning') =~ m/internal response/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->header("client-warning") =~ /internal response/i) 0 6 6
78 0 ▶ return wantarray ? ($res->code, {}) : {};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
79 }
80 }
81 22 ▶ my $decoded = $res->content ? ($self->_decode($res->content) || {}) : {};
Branch: 1 branch
BranchTrueFalseTotal
$res->content ? : 20 2 22
Condition100%2/2
$self->_decode($res->content) or {}
l!l
182
MC/DC100%1/1
$self->_decode($res->content) or {}
$self->_decode($res->content)
Truth table100%2/2
A
A= $self->_decode($res->content)
Aresult
11
00
82  
83 #FIXME: should we auto-populate an error key in the {} if error detected but no content?
84 return wantarray ?
85 22 ▶ ($res->code, $decoded) :
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 11 11 22
86 $decoded;
87 }
88  
89 sub _encode
90 {
91 8 ▶ my ($self, $obj) = @_;
Subroutine _encode: called CC=3 SCAR=12.7
92  
93 8 my $json = undef;
94 eval {
95 8 $json = to_json($obj);
96 8 $self->_debug("JSON created: $json");
97 8 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $json = &to_json($obj); $self->_debug("JSON created: $json") } } or do { if ($@) { $self->{"has_error"} = 1; $self->{"error_string"} = $@; $self->{"error_string"} =~ s/\s+at\s+\S+\s+line\s+\d+\.?\s*//; $self->_debug('Error serializing json from $obj:' . $self->{"error_string"}); } } 8 0 8
98 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
99 0 $self->{has_error} = 1;
100 0 $self->{error_string} = $@;
101 0 $self->{error_string} =~ s/\s+at\s+\S+\s+line\s+\d+\.?\s*//;
102 0 $self->_debug("Error serializing json from \$obj:" . $self->{error_string});
103 }
104 };
105 8 return $json;
106 }
107  
108 sub _decode
109 {
110 22 ▶ my ($self, $json) = @_;
Subroutine _decode: called CC=4 SCAR=13.9
111  
112 22 $self->_debug("Deserializing JSON");
113 22 my $obj = undef;
114 eval {
115 $json = $self->{predecodehook}->($json)
116 22 ▶ if defined($self->{predecodehook});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"predecodehook"} 0 22 22
117 22 $obj = from_json($json);
118 19 $self->_debug("Deserializing successful:",Dumper($obj));
119 22 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $json = $self->{"predecodehook"}->($json) if defined $self->{"predecodehook"}; $obj = &from_json($json); $self->_debug("Deserializing successful:", Dumper($obj)) } } or do { if ($@) { $self->{"has_error"} = 1; $self->{"error_string"} = $@; $self->{"error_string"} =~ s/\s+at\s+\S+\s+line\s+\d+\.?\s*//; $self->_debug('Error deserializing: ' . $self->{"error_string"}); } } 22 0 22
120 22 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 3 19 22
121 3 $self->{has_error} = 1;
122 3 $self->{error_string} = $@;
123 3 $self->{error_string} =~ s/\s+at\s+\S+\s+line\s+\d+\.?\s*//;
124 3 $self->_debug("Error deserializing: ".$self->{error_string});
125 }
126 };
127 22 return $obj;
128 }
129  
130 sub new
131 {
132 32 ▶ my ($class, $base_url, %parameters) = @_;
Subroutine new: called CC=8 SCAR=20.8
Pod documented
133 32 ▶ return undef unless $base_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $base_url 1 31 32
134  
135 31 my %ua_opts = %parameters;
136 31 map { delete $parameters{$_}; } qw(user pass realm debug predecodehook);
137  
138 31 my $ua = LWP::UserAgent->new(%parameters);
139  
140 my $self = bless ({
141 base_url => $base_url,
142 user_agent => $ua,
143 has_error => 0,
144 error_string => '',
145 debug => $ua_opts{debug},
146 predecodehook => $ua_opts{predecodehook},
147 31 ▶ }, ref ($class) || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0310
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
148  
149 31 my $server = $self->_server($base_url);
150 31 ▶ my $default_port = $base_url =~ m|^https://| ? 443 : 80;
Branch: 1 branch
BranchTrueFalseTotal
$base_url =~ m[^https://] ? : 1 30 31
151 31 ▶ $server .= ":$default_port" unless $server =~ /:\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $server =~ /:\d+$/ 27 4 31
152 $ua->credentials($server, $ua_opts{realm}, $ua_opts{user}, $ua_opts{pass})
153 31 ▶ if ($ua_opts{realm} && $ua_opts{user} && $ua_opts{pass});
Branch: 1 branch
BranchTrueFalseTotal
if $ua_opts{"realm"} and $ua_opts{"user"} and $ua_opts{"pass"} 3 28 31
Condition100%6/6
$ua_opts{"realm"} and $ua_opts{"user"} and $ua_opts{"pass"}
!ll&&!rl&&r
2713
$ua_opts{"realm"} and $ua_opts{"user"}
!ll&&!rl&&r
2524
MC/DC100%3/3
$ua_opts{"realm"} and $ua_opts{"user"} and $ua_opts{"pass"}
$ua_opts{"realm"} $ua_opts{"user"} $ua_opts{"pass"}
Truth table100%4/4
A and B and C
A= $ua_opts{"realm"}B= $ua_opts{"user"}C= $ua_opts{"pass"}
ABCresult
0XX0
10X0
1100
1111
154  
155 31 return $self;
156 }
157  
158 sub get
159 {
160 12 ▶ my ($self, $path, $data, $apphdr) = @_;
Subroutine get: called CC=2 SCAR=6.9
Pod documented
161 12 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 2 10 12
162 2 my @qp = map { "$_=".uri_encode($data->{$_}, { encode_reserved => 1 }) } sort keys %$data;
163 2 $path .= "?".join("&", @qp);
164 }
165 12 $self->_http_req("GET", $path, undef, $apphdr);
166 }
167  
168 sub put
169 {
170 4 ▶ my ($self, $path, $data, $apphdr) = @_;
Subroutine put: called CC=1 SCAR=0.0
Pod documented
171 4 $self->_http_req("PUT", $path, $data, $apphdr);
172 }
173  
174 sub post
175 {
176 4 ▶ my ($self, $path, $data, $apphdr) = @_;
Subroutine post: called CC=1 SCAR=0.0
Pod documented
177 4 $self->_http_req("POST", $path, $data, $apphdr);
178 }
179  
180 sub del
181 {
182 2 ▶ my ($self, $path, $apphdr) = @_;
Subroutine del: called CC=1 SCAR=0.0
Pod documented
183 2 $self->_http_req("DELETE", $path, undef, $apphdr);
184 }
185  
186 sub url
187 {
188 26 ▶ my ($self, $path) = @_;
Subroutine url: called CC=1 SCAR=0.0
Pod documented
189 26 my $url = $self->{base_url} . "/$path";
190 # REGEX-FU: look through the URL, replace any matches of /+ with '/',
191 # as long as the previous character was not a ':'
192 # (e.g. http://example.com//api//mypath/ becomes http://example.com/api/mypath/
193 26 $url =~ s|(?<!:)/+|/|g;
194 26 return $url;
195 }
196  
197 sub response
198 {
199 0 ▶ my ($self) = @_;
Subroutine response: not called CC=1 SCAR=6.9
Pod documented
200  
201 0 return $self->{response};
202 }
203  
204 sub header
205 {
206 0 ▶ my ($self, $name) = @_;
Subroutine header: not called CC=3 SCAR=24.8
Pod documented
207  
208 0 ▶ return unless( $self->{response} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"response"} 0 0 0
209  
210 0 ▶ unless( $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 0 0
211 0 return $self->{response}->header_field_names;
212 }
213 0 return $self->{response}->header( $name );
214 }
215  
216 sub errstr
217 {
218 5 ▶ my ($self) = @_;
Subroutine errstr: called CC=2 SCAR=6.9
Pod documented
219 5 ▶ return ! $self->was_success ? $self->{error_string} : '';
Branch: 1 branch
BranchTrueFalseTotal
!$self->was_success ? : 3 2 5
220 }
221  
222 sub was_success
223 {
224 7 ▶ my ($self) = @_;
Subroutine was_success: called CC=2 SCAR=6.9
Pod documented
225 7 ▶ return $self->{has_error} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'has_error'} ? : 4 3 7
226 }
227  
228 4 1;
229  
230 __END__
↑ Summary Build »