API/Client.pm

filter: Statementstmt 82.4% 80 / 97 Branchbran 40.6% 13 / 32 Conditioncond 37.1% 13 / 35 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 100% 18 / 18 Podpod 100% 2 / 2 totaltotal 63.1% 127 / 201 CC 32 SCAR 23.6
CC
32
Cov
65%
_parse_path
20.1
_parse_response
18.5
_parse_headers
14.4
CRAP 10.6
« API/Resource.pm ↑ Summary API/Resource/Products.pm »
1 # Copyright 2013 Digital River, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14  
15 package Net::MyCommerce::API::Client;
16  
17 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 ▶use Encode qw( decode_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use JSON::Parse qw( json_to_perl );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use REST::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use URI::Escape qw( uri_escape );
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 =head1 NAME
29
30 Net::MyCommerce::API::Client
31
32 =head1 VERSION
33
34 version 1.0.1
35
36 =cut
37
38 4 our $VERSION = '1.0.1';
39
40 =head1 METHODS
41  
42 =head2 new
43  
44 REST::Client wrapper used by Net::MyCommerce::API::Token and Net::MyCommerce::API::Resource
45  
46 =cut
47  
48 3 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
49 3 my ($pkg, %args) = @_;
50 3 $args{client} = REST::Client->new(%args);
51 3 ▶ $args{timeout} ||= 60;
Condition100%2/2
$args{"timeout"} ||= 60
l!l
12
MC/DC100%1/1
$args{"timeout"} ||= 60
$args{"timeout"}
Truth table100%2/2
A
A= $args{"timeout"}
Aresult
11
00
52 3 return bless { %args }, $pkg;
53 }
54  
55 =head2 request (%opts)
56  
57 %opns:
58  
59 path: (required) full path or array ref with path elements; exclude prefix
60 params: (optional) query params
61 headers: (optional) headers
62  
63  
64 my $client = Net::MyCommerce::API::Client->new(%args);
65 my ($error, $result) = $client->request(%opts);
66 my $content = $result->{content};
67 my $status_code = $result->{status_code};
68 my $headers = $result->{headers};
69  
70 =cut
71  
72 2 ▶sub request {
Subroutine request: called CC=1 SCAR=0.0
Pod documented
73 2 my ($self, %opts) = @_;
74 2 $self->{client}->request($self->_parse_options(%opts));
75 2 return $self->_parse_response($self->{client}->{_res});
76 }
77  
78 # Private Methods
79  
80 2 ▶sub _parse_path {
Subroutine _parse_path: called CC=9 SCAR=30.0
81 2 my ($self, %opts) = @_;
82 2 ▶ my $prefix = $opts{prefix} || $self->{prefix} || '';
Condition40%2/5
$opts{'prefix'} || $self->{'prefix'} || ''
l!l
02
$opts{"prefix"} or $self->{"prefix"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$opts{'prefix'} || $self->{'prefix'} || ''
$opts{"prefix"} $self->{"prefix"}
Truth table33%1/3
A or B
A= $opts{"prefix"}B= $self->{"prefix"}
ABresult
1X1
011
000
83 2 ▶ my $path = $opts{path} || $self->{path};
Condition33%1/3
$opts{'path'} || $self->{'path'}
l!l&&r!l&&!r
020
MC/DC0%0/2
$opts{'path'} || $self->{'path'}
$opts{'path'} $self->{'path'}
Truth table33%1/3
A || B
A= $opts{'path'}B= $self->{'path'}
ABresult
1X1
011
000
84 2 ▶ my $params = $opts{params} || $self->{params} || {};
Condition40%2/5
$opts{'params'} || $self->{'params'} || {}
l!l
20
$opts{"params"} or $self->{"params"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$opts{'params'} || $self->{'params'} || {}
$opts{"params"} $self->{"params"}
Truth table33%1/3
A or B
A= $opts{"params"}B= $self->{"params"}
ABresult
1X1
011
000
85 2 ▶ if (ref($path) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path eq "ARRAY") 0 2 2
86 0 my $newpath = join("/", @$path);
87 0 $path = $newpath;
88 }
89 2 ▶ if (keys %$params > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$params > 0) 0 2 2
90 0 my @list = ();
91 0 foreach my $n (sort keys %$params) {
92 0 push @list, join("=", $n, uri_escape($params->{$n}));
93 }
94 0 $path .= '?' . join("&", @list);
95 }
96 2 $opts{path} = $prefix . $path;
97 2 return %opts;
98 }
99  
100 2 ▶sub _parse_data {
Subroutine _parse_data: called CC=3 SCAR=12.0
101 2 my ($self, %opts) = @_;
102 2 ▶ if ($opts{method} =~ /^(POST|PUT)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'method'} =~ /^(POST|PUT)$/) { } 2 0 2
103 2 ▶ $opts{data} = encode_json($opts{data}) if $self->{sendJSON};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sendJSON"} 0 2 2
104 } else {
105 0 $opts{data} = '';
106 }
107 2 return %opts;
108 }
109  
110 2 ▶sub _parse_headers {
Subroutine _parse_headers: called CC=8 SCAR=26.7
111 2 my ($self, %opts) = @_;
112 2 ▶ my $headers = $opts{headers} || $self->{headers} || {};
Condition40%2/5
$opts{'headers'} || $self->{'headers'} || {}
l!l
02
$opts{"headers"} or $self->{"headers"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$opts{'headers'} || $self->{'headers'} || {}
$opts{"headers"} $self->{"headers"}
Truth table33%1/3
A or B
A= $opts{"headers"}B= $self->{"headers"}
ABresult
1X1
011
000
113 2 ▶ if ($self->{credentials}{id} &&
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'credentials'}{'id'} and $self->{'credentials'}{'secret'}) { } 0 2 2
elsif ($opts{'token_id'}) { } 0 2 2
Condition33%1/3
$self->{"credentials"}{"id"} and $self->{"credentials"}{"secret"}
!ll&&!rl&&r
200
MC/DC0%0/2
$self->{"credentials"}{"id"} and $self->{"credentials"}{"secret"}
$self->{"credentials"}{"id"} $self->{"credentials"}{"secret"}
Truth table33%1/3
A and B
A= $self->{"credentials"}{"id"}B= $self->{"credentials"}{"secret"}
ABresult
0X0
100
111
114 $self->{credentials}{secret}) {
115 0 my $auth = join(":", $self->{credentials}{id}, $self->{credentials}{secret});
116 0 chomp( $headers->{Authorization} = 'Basic ' . encode_base64($auth) );
117 } elsif ($opts{token_id}) {
118 0 $headers->{Authorization} = 'Bearer ' . $opts{token_id};
119 }
120 2 ▶ if ($opts{method} =~ /^(POST|PUT)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"method"} =~ /^(POST|PUT)$/) 2 0 2
121 2 ▶ if ($self->{sendJSON}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'sendJSON'}) { } 0 2 2
122 0 $headers->{'Content-type'} = 'application/json;charset=UTF-8';
123 } else {
124 2 $headers->{'Content-type'} = 'application/x-www-form-urlencoded';
125 }
126 }
127 2 $opts{headers} = $headers;
128 2 return %opts;
129 }
130  
131 2 ▶sub _parse_options {
Subroutine _parse_options: called CC=3 SCAR=11.6
132 2 my ($self, %opts) = @_;
133 2 ▶ $opts{method} ||= $self->{method} || 'GET';
Condition40%2/5
$opts{"method"} ||= $self->{"method"} || "GET"
l!l&&r!l&&!r
020
$self->{'method'} || 'GET'
l!l
20
MC/DC0%0/2
$opts{"method"} ||= $self->{"method"} || "GET"
$opts{"method"} $self->{'method'}
Truth table33%1/3
A ||= B
A= $opts{"method"}B= $self->{'method'}
ABresult
1X1
011
000
134 2 %opts = $self->_parse_path(%opts);
135 2 %opts = $self->_parse_data(%opts);
136 2 %opts = $self->_parse_headers(%opts);
137 2 return ($opts{method}, $opts{path}, $opts{data}, $opts{headers});
138 }
139  
140 2 ▶sub _parse_response {
Subroutine _parse_response: called CC=9 SCAR=29.2
141 2 my ($self, $response) = @_;
142 2 ▶ if ($response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) 2 0 2
143 2 my $error = '';
144 2 my $result = {};
145 2 my $content = $response->content;
146 2 ▶ try {
Subroutine __ANON__: called CC=4 SCAR=17.9
147 2 ▶ if ($content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) { } 2 0 2
148 2 $content = decode_utf8($content);
149 2 ▶ $content = json_to_perl($content) if $self->{getJSON};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"getJSON"} 2 0 2
150 } else {
151 0 ▶ $content = {} if $self->{getJSON};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"getJSON"} 0 0 0
152 }
153 2 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
154 2 $content =~ s/\s+/ /g;;
155 2 $error = $_ . " [" . $content . "]";
156 2 };
157 2 ▶ if (ref($content) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq "HASH") 0 2 2
158 0 ▶ my $errmsg = $content->{error} || '';
Condition0%0/2
$content->{'error'} || ''
l!l
00
MC/DC0%0/1
$content->{'error'} || ''
$content->{'error'}
Truth table0%0/2
A
A= $content->{'error'}
Aresult
11
00
159 0 ▶ $errmsg .= ": " . $content->{error_code} if $content->{error_code};
Branch: 1 branch
BranchTrueFalseTotal
if $content->{"error_code"} 0 0 0
160 0 ▶ $errmsg .= ": " . $content->{error_description} if $content->{error_description};
Branch: 1 branch
BranchTrueFalseTotal
if $content->{"error_description"} 0 0 0
161 0 ▶ $error ||= $errmsg;
Condition0%0/3
$error ||= $errmsg
l!l&&r!l&&!r
000
MC/DC0%0/2
$error ||= $errmsg
$error $errmsg
Truth table0%0/3
A ||= B
A= $errorB= $errmsg
ABresult
1X1
011
000
162 }
163 2 ▶ if ($response->code =~ /^[345]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code =~ /^[345]/) 2 0 2
164 2 ▶ $error ||= "status code " . $response->code;
Condition50%1/2
$error ||= 'status code ' . $response->code
l!l
20
MC/DC0%0/1
$error ||= 'status code ' . $response->code
$error
Truth table50%1/2
A
A= $error
Aresult
11
00
165 }
166 2 $result->{content} = $content;
167 2 $result->{status_code} = $response->code;
168 2 $result->{headers} = $response->headers;
169 2 return ($error, $result);
170 }
171 0 return ("no response", {});
172 }
173  
174  
175 4 1;
« API/Resource.pm ↑ Summary API/Resource/Products.pm »