| 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 | 4 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 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.2.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 | # not an object metohd | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # build JSON object for encode/decode, disallow nonref | ||||||||||||||||||||||||||||||||||||||||||
| 22 | # to match previous non object usage, APIs should be returning | ||||||||||||||||||||||||||||||||||||||||||
| 23 | # JSON arrays or JSON objects | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 8 | ▶ | sub _build_json { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_json: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | # utf8: encode/decode operates on byte strings. HTTP::Request requires | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # bytes; without this, non-ASCII payloads carry the utf8 flag and trip | ||||||||||||||||||||||||||||||||||||||||||
| 27 | # "HTTP::Message content must be bytes". | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 8 | JSON->new->utf8->allow_nonref(0); | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | sub _debug | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 193 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_debug: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 193 | my ($self, @lines) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 193 | my $output = join('\n', @lines); | |||||||||||||||||||||||||||||||||||||||||
| 34 | 193 | ▶ | print STDERR $output . "\n" if ($self->{debug}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | sub _server | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 45 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_server: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 45 | my ($self, $input) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 45 | $input =~ s|^(https?://)?||; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 45 | $input =~ m|^([^\s:/]+)(:\d+)?.*|; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 45 | ▶ | $input = $1 . ($2 || ''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$2 || ''
MC/DC100%1/1
$2 || ''
$2
Truth table100%2/2
A
A= $2
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 45 | return $input; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | sub _http_req | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 26 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_http_req: called CC=15 SCAR=28.5
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 26 | my ($self, $method, $path, $data, $apphdr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 26 | $self->_debug('_http_req called with the following:',Dumper($method,$path,$data, $apphdr)); | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 26 | my $url = $self->url($path); | |||||||||||||||||||||||||||||||||||||||||
| 52 | 26 | $self->_debug("URL calculated to be: $url"); | |||||||||||||||||||||||||||||||||||||||||
| 53 | 26 | delete $self->{response}; | |||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 26 | my $headers = HTTP::Headers->new( | |||||||||||||||||||||||||||||||||||||||||
| 56 | 'Accept' => 'application/json', | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 'Content-Type' => 'application/json', | ||||||||||||||||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 26 | ▶ | if( $apphdr && ref $apphdr ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$apphdr and ref $apphdr
MC/DC0%0/2
$apphdr and ref $apphdr
$apphdr
ref $apphdr
Truth table33%1/3
A and B
A= $apphdrB= ref $apphdr
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | $headers->header( $_, $apphdr->{$_} ) foreach (keys %$apphdr); | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 26 | my $json; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 26 | ▶ | if (defined $data) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 8 | $json = $self->_encode($data); | |||||||||||||||||||||||||||||||||||||||||
| 65 | 8 | ▶ | return (wantarray ? (500, {}) : {}) unless defined $json; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 24 | my $req = HTTP::Request->new($method, $url, $headers, $json); | |||||||||||||||||||||||||||||||||||||||||
| 69 | 24 | $self->_debug("Requesting: ",Dumper($req)); | |||||||||||||||||||||||||||||||||||||||||
| 70 | 24 | my $res = $self->{user_agent}->request($req); | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 24 | $self->_debug("Response: ",Dumper($res)); | |||||||||||||||||||||||||||||||||||||||||
| 73 | 24 | $self->{response} = $res; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 24 | ▶ | if ($res->is_success) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 18 | $self->{has_error} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 18 | $self->{error_string} = ''; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 18 | $self->_debug("Successful request detected"); | |||||||||||||||||||||||||||||||||||||||||
| 78 | } elsif ($res->code == HTTP_NOT_MODIFIED) { | ||||||||||||||||||||||||||||||||||||||||||
| 79 | return wantarray ? | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | ($res->code, {}) : | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | {}; | ||||||||||||||||||||||||||||||||||||||||||
| 82 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 6 | $self->{has_error} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 6 | $self->{error_string} = $res->content; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 6 | $self->_debug("Error detected: ".$self->{error_string}); | |||||||||||||||||||||||||||||||||||||||||
| 86 | # If internal warning, return before decoding, as it will fail + overwrite the error_string | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 6 | ▶ | if ($res->header('client-warning') =~ m/internal response/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | return wantarray ? ($res->code, {}) : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 24 | ▶ | my $decoded = $res->content ? ($self->_decode($res->content) || {}) : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->_decode($res->content) or {}
MC/DC100%1/1
$self->_decode($res->content) or {}
$self->_decode($res->content)
Truth table100%2/2
A
A= $self->_decode($res->content)
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | #FIXME: should we auto-populate an error key in the {} if error detected but no content? | ||||||||||||||||||||||||||||||||||||||||||
| 94 | return wantarray ? | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 24 | ▶ | ($res->code, $decoded) : | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | $decoded; | ||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | sub _encode | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 13 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encode: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 13 | my ($self, $obj) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | # Validate input ourselves rather than relying on JSON backend's | ||||||||||||||||||||||||||||||||||||||||||
| 104 | # allow_nonref(0). Modern JSON::PP/JSON::XS default allow_nonref to | ||||||||||||||||||||||||||||||||||||||||||
| 105 | # true and may ignore allow_nonref(0); error message text also varies | ||||||||||||||||||||||||||||||||||||||||||
| 106 | # across backends. Guarantee consistent behavior + errstr here. | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 13 | ▶ | unless (ref($obj) eq 'HASH' || ref($obj) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $obj eq "HASH" or ref $obj eq "ARRAY"
MC/DC50%1/2
ref $obj eq "HASH" or ref $obj eq "ARRAY"
ref $obj eq "HASH"
ref $obj eq "ARRAY"
Truth table66%2/3
A or B
A= ref $obj eq "HASH"B= ref $obj eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 3 | $self->{has_error} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 3 | $self->{error_string} = 'hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)'; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 3 | $self->_debug("Error serializing json from \$obj:" . $self->{error_string}); | |||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | return undef; ## no critic (ProhibitExplicitReturnUndef) | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 10 | my $json = undef; | |||||||||||||||||||||||||||||||||||||||||
| 115 | eval { | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 10 | ▶ | $self->{_json} ||= _build_json(); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"_json"} ||= _build_json()
MC/DC0%0/2
$self->{"_json"} ||= _build_json()
$self->{"_json"}
_build_json()
Truth table66%2/3
A ||= B
A= $self->{"_json"}B= _build_json()
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 10 | $json = $self->{_json}->encode($obj); | |||||||||||||||||||||||||||||||||||||||||
| 118 | 8 | $self->_debug("JSON created: $json"); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 10 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 10 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 2 | $self->{has_error} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 2 | $self->{error_string} = $@; | |||||||||||||||||||||||||||||||||||||||||
| 123 | # Strip Perl's "at FILE line N" location, optional ", <FH> line/chunk N" | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # suffix Perl appends when <> was active, and any multi-line carp | ||||||||||||||||||||||||||||||||||||||||||
| 125 | # stack trace that follows. /s lets .* span newlines. | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 2 | $self->{error_string} =~ s/\s+at\s+\S+\s+line\s+\d+(?:,\s+\S+\s+(?:line|chunk)\s+\d+)?\..*\z//s; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 2 | $self->_debug("Error serializing json from \$obj:" . $self->{error_string}); | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 10 | return $json; | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | sub _decode | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 27 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decode: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 27 | my ($self, $json) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 27 | $self->_debug("Deserializing JSON"); | |||||||||||||||||||||||||||||||||||||||||
| 138 | 27 | my $obj = undef; | |||||||||||||||||||||||||||||||||||||||||
| 139 | eval { | ||||||||||||||||||||||||||||||||||||||||||
| 140 | $json = $self->{predecodehook}->($json) | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 27 | ▶ | if defined($self->{predecodehook}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 27 | ▶ | $self->{_json} ||= _build_json(); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"_json"} ||= _build_json()
MC/DC0%0/2
$self->{"_json"} ||= _build_json()
$self->{"_json"}
_build_json()
Truth table66%2/3
A ||= B
A= $self->{"_json"}B= _build_json()
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 27 | $obj = $self->{_json}->decode($json); | |||||||||||||||||||||||||||||||||||||||||
| 144 | 20 | $self->_debug("Deserializing successful:",Dumper($obj)); | |||||||||||||||||||||||||||||||||||||||||
| 145 | 27 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 27 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 7 | $self->{has_error} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 7 | $self->{error_string} = $@; | |||||||||||||||||||||||||||||||||||||||||
| 149 | # Strip Perl's "at FILE line N" location, optional ", <FH> line/chunk N" | ||||||||||||||||||||||||||||||||||||||||||
| 150 | # suffix Perl appends when <> was active, and any multi-line carp | ||||||||||||||||||||||||||||||||||||||||||
| 151 | # stack trace that follows. /s lets .* span newlines. | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 7 | $self->{error_string} =~ s/\s+at\s+\S+\s+line\s+\d+(?:,\s+\S+\s+(?:line|chunk)\s+\d+)?\..*\z//s; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 7 | $self->_debug("Error deserializing: ".$self->{error_string}); | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 27 | return $obj; | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | sub new | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 38 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 38 | my ($class, $base_url, %parameters) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 38 | ▶ | return undef unless $base_url; ## no critic (ProhibitExplicitReturnUndef) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 37 | my %ua_opts = %parameters; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 37 | map { delete $parameters{$_}; } qw(user pass realm debug predecodehook); | |||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 37 | my $ua = LWP::UserAgent->new(%parameters); | |||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | my $self = bless ({ | ||||||||||||||||||||||||||||||||||||||||||
| 170 | base_url => $base_url, | ||||||||||||||||||||||||||||||||||||||||||
| 171 | user_agent => $ua, | ||||||||||||||||||||||||||||||||||||||||||
| 172 | has_error => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 173 | error_string => '', | ||||||||||||||||||||||||||||||||||||||||||
| 174 | debug => $ua_opts{debug}, | ||||||||||||||||||||||||||||||||||||||||||
| 175 | predecodehook => $ua_opts{predecodehook}, | ||||||||||||||||||||||||||||||||||||||||||
| 176 | 37 | ▶ | }, ref ($class) || $class); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 37 | my $server = $self->_server($base_url); | |||||||||||||||||||||||||||||||||||||||||
| 179 | 37 | ▶ | my $default_port = $base_url =~ m|^https://| ? 443 : 80; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 37 | ▶ | $server .= ":$default_port" unless $server =~ /:\d+$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | $ua->credentials($server, $ua_opts{realm}, $ua_opts{user}, $ua_opts{pass}) | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 37 | ▶ | if ($ua_opts{realm} && $ua_opts{user} && $ua_opts{pass}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$ua_opts{"realm"} and $ua_opts{"user"} and $ua_opts{"pass"}
$ua_opts{"realm"} and $ua_opts{"user"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 37 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | sub get | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 12 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 12 | my ($self, $path, $data, $apphdr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 190 | 12 | ▶ | if ($data) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | 2 | my @qp = map { "$_=".uri_encode($data->{$_}, { encode_reserved => 1 }) } sort keys %$data; | |||||||||||||||||||||||||||||||||||||||||
| 192 | 2 | $path .= "?".join("&", @qp); | |||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 12 | $self->_http_req("GET", $path, undef, $apphdr); | |||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | sub patch | ||||||||||||||||||||||||||||||||||||||||||
| 198 | 4 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
patch: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 4 | my ($self, $path, $data, $apphdr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 4 | $self->_http_req("PATCH", $path, $data, $apphdr); | |||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | sub put | ||||||||||||||||||||||||||||||||||||||||||
| 204 | 4 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
put: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 4 | my ($self, $path, $data, $apphdr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 4 | $self->_http_req("PUT", $path, $data, $apphdr); | |||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | sub post | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 4 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
post: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 4 | my ($self, $path, $data, $apphdr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 4 | $self->_http_req("POST", $path, $data, $apphdr); | |||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | sub del | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 2 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
del: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 2 | my ($self, $path, $apphdr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 218 | 2 | $self->_http_req("DELETE", $path, undef, $apphdr); | |||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | sub url | ||||||||||||||||||||||||||||||||||||||||||
| 222 | 30 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 30 | my ($self, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 224 | 30 | my $url = $self->{base_url} . "/$path"; | |||||||||||||||||||||||||||||||||||||||||
| 225 | # REGEX-FU: look through the URL, replace any matches of /+ with '/', | ||||||||||||||||||||||||||||||||||||||||||
| 226 | # as long as the previous character was not a ':' | ||||||||||||||||||||||||||||||||||||||||||
| 227 | # (e.g. http://example.com//api//mypath/ becomes http://example.com/api/mypath/ | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 30 | $url =~ s|(?<!:)/+|/|g; | |||||||||||||||||||||||||||||||||||||||||
| 229 | 30 | return $url; | |||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | sub response | ||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
response: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | return $self->{response}; | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | sub header | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
header: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | ▶ | return unless( $self->{response} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | unless( $name ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | return $self->{response}->header_field_names; | |||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | return $self->{response}->header( $name ); | |||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | sub errstr | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 9 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
errstr: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 9 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 254 | 9 | ▶ | return ! $self->was_success ? $self->{error_string} : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | sub was_success | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 11 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
was_success: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | 11 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 260 | 11 | ▶ | return $self->{has_error} ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | __END__ | ||||||||||||||||||||||||||||||||||||||||||