| 1 | package Eixo::Rest::Client; | ||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | |||||||||||||||
| 4 | 3 | ▶ | use Eixo::Base::Clase; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 3 | ▶ | use URI; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 3 | ▶ | use LWP::UserAgent; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 3 | ▶ | use LWP::Protocol::https; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 3 | ▶ | use JSON -convert_blessed_universally; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 3 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 3 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 3 | ▶ | use Config; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 3 | ▶ | use Eixo::Rest::RequestSync; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | |||||||||||||||
| 17 | 3 | my $REQ_PARSER = qr/\:\:([a-z]+)([A-Z]\w+?)$/; | |||||||||||||
| 18 | |||||||||||||||
| 19 | 3 | my $USER_AGENT_VERSION = 'EixoAgent/0.1'; | |||||||||||||
| 20 | |||||||||||||||
| 21 | 3 | has( | |||||||||||||
| 22 | |||||||||||||||
| 23 | ua=>undef, | ||||||||||||||
| 24 | endpoint=>undef, | ||||||||||||||
| 25 | format=>'json', | ||||||||||||||
| 26 | error_callback => undef, | ||||||||||||||
| 27 | current_method => undef, | ||||||||||||||
| 28 | ); | ||||||||||||||
| 29 | |||||||||||||||
| 30 | sub initialize{ | ||||||||||||||
| 31 | 3 | ▶ | my ($self, $endpoint, %opts) = @_; | ||||||||||||
|
Subroutine
initialize: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||
| 32 | |||||||||||||||
| 33 | 3 | $self->endpoint($endpoint); | |||||||||||||
| 34 | |||||||||||||||
| 35 | 3 | ▶ | die("API ENDPOINT NEEDED") unless($self->endpoint); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | |||||||||||||||
| 37 | 3 | $self->ua($USER_AGENT_VERSION, %opts); | |||||||||||||
| 38 | |||||||||||||||
| 39 | 3 | $self; | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | sub ua { | ||||||||||||||
| 43 | 8 | ▶ | my ($self, $ua_str, %opts) = @_; | ||||||||||||
|
Subroutine
ua: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 44 | |||||||||||||||
| 45 | 8 | ▶ | if($ua_str){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | 3 | my $ua = LWP::UserAgent->new(%opts); | |||||||||||||
| 47 | 3 | $ua->agent($ua_str); | |||||||||||||
| 48 | 3 | $self->{ua} = $ua; | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 8 | return $self->{ua}; | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | |||||||||||||||
| 55 | |||||||||||||||
| 56 | sub AUTOLOAD{ | ||||||||||||||
| 57 | 6 | ▶ | my ($self, %args) = @_; | ||||||||||||
|
Subroutine
AUTOLOAD: called CC=7 SCAR=19.5
| |||||||||||||||
| 58 | |||||||||||||||
| 59 | 6 | my ($method, $entity) = our $AUTOLOAD =~ $REQ_PARSER; | |||||||||||||
| 60 | |||||||||||||||
| 61 | # store current method for tracing | ||||||||||||||
| 62 | 6 | $self->current_method($method.$entity); | |||||||||||||
| 63 | |||||||||||||||
| 64 | 6 | $entity = lc($entity); | |||||||||||||
| 65 | |||||||||||||||
| 66 | 6 | ▶ | unless(grep { $method eq $_ } qw(put get post delete patch)){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | 1 | confess(ref($self) . ': UNKNOW METHOD: ' . $method); | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | |||||||||||||||
| 70 | 5 | my ($id, $action); | |||||||||||||
| 71 | |||||||||||||||
| 72 | 5 | ▶ | if(exists($args{id})){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | 1 | $id = $args{id}; | |||||||||||||
| 74 | 1 | delete($args{id}); | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | |||||||||||||||
| 77 | 5 | ▶ | if(exists($args{action})){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | 2 | $action = $args{action}; | |||||||||||||
| 79 | 2 | delete($args{action}); | |||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 5 | ▶ | if($args{__job_id}){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | 0 | $args{'__client_send_method'} = '__sendAsync'; | |||||||||||||
| 84 | } | ||||||||||||||
| 85 | else{ | ||||||||||||||
| 86 | 5 | $args{'__client_send_method'} = '__send'; | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 5 | ▶ | if(!$args{__format}){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 90 | 3 | $args{__format} = $self->format; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | # set error_callback unless already established | ||||||||||||||
| 94 | 5 | ▶ | unless(defined($args{PROCESS_DATA}->{onError})){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 95 | |||||||||||||||
| 96 | $args{PROCESS_DATA}->{onError} = sub { | ||||||||||||||
| 97 | |||||||||||||||
| 98 | 1 | ▶ | $self->remote_error(@_); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 99 | |||||||||||||||
| 100 | 5 | }; | |||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | 5 | my $uri = $self->build_uri($entity, $id, $action, $args{__implicit_format}); | |||||||||||||
| 104 | |||||||||||||||
| 105 | 5 | $self->$method($uri, %args); | |||||||||||||
| 106 | |||||||||||||||
| 107 | } | ||||||||||||||
| 108 | |||||||||||||||
| 109 | ▶ | sub DESTROY {} | |||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||
| 110 | |||||||||||||||
| 111 | |||||||||||||||
| 112 | sub get: Log { | ||||||||||||||
| 113 | |||||||||||||||
| 114 | 4 | ▶ | my ($self, $uri, %args) = @_; | ||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 115 | |||||||||||||||
| 116 | 4 | $uri->query_form($args{GET_DATA}); | |||||||||||||
| 117 | |||||||||||||||
| 118 | |||||||||||||||
| 119 | 4 | my $req = HTTP::Request->new(GET => $uri); | |||||||||||||
| 120 | |||||||||||||||
| 121 | 4 | my $send_method = $args{__client_send_method}; | |||||||||||||
| 122 | |||||||||||||||
| 123 | 4 | $self->$send_method($req, %args); | |||||||||||||
| 124 | 3 | ▶ | } | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 125 | |||||||||||||||
| 126 | sub post: Log { | ||||||||||||||
| 127 | 1 | ▶ | my ($self,$uri,%args) = @_; | ||||||||||||
|
Subroutine
post: called CC=6 SCAR=21.5
Pod
undocumented
| |||||||||||||||
| 128 | |||||||||||||||
| 129 | # Is possible to add query string args to post requests | ||||||||||||||
| 130 | 1 | $uri->query_form($args{GET_DATA}); | |||||||||||||
| 131 | |||||||||||||||
| 132 | 1 | my $req = HTTP::Request->new(POST => $uri); | |||||||||||||
| 133 | |||||||||||||||
| 134 | #$req->header('content-type' => 'application/json'); | ||||||||||||||
| 135 | |||||||||||||||
| 136 | 1 | ▶ | my $headers = $args{HEADER_DATA} || {'Content-Type' => 'application/json'}; | ||||||||||||
|
Condition50%1/2
$args{'HEADER_DATA'} || {'Content-Type', 'application/json'}
MC/DC0%0/1
$args{'HEADER_DATA'} || {'Content-Type', 'application/json'}
$args{'HEADER_DATA'}
Truth table50%1/2
A
A= $args{'HEADER_DATA'}
| |||||||||||||||
| 137 | |||||||||||||||
| 138 | 1 | $req->header(%$headers); | |||||||||||||
| 139 | |||||||||||||||
| 140 | 1 | my $content; | |||||||||||||
| 141 | |||||||||||||||
| 142 | 1 | ▶ | if($req->header("Content-Type") eq "application/json"){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 143 | |||||||||||||||
| 144 | $content = JSON->new->allow_blessed(1) | ||||||||||||||
| 145 | ->convert_blessed(1)->utf8 | ||||||||||||||
| 146 | 1 | ▶ | ->encode($args{POST_DATA} || {}); | ||||||||||||
|
Condition50%1/2
$args{'POST_DATA'} || {}
MC/DC0%0/1
$args{'POST_DATA'} || {}
$args{'POST_DATA'}
Truth table50%1/2
A
A= $args{'POST_DATA'}
| |||||||||||||||
| 147 | } | ||||||||||||||
| 148 | else{ | ||||||||||||||
| 149 | |||||||||||||||
| 150 | # application/x-www-form-urlencoded | ||||||||||||||
| 151 | # raw stream | ||||||||||||||
| 152 | 0 | ▶ | while(my ($key, $value) = each (%{$args{POST_DATA} || {}})){ | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 153 | 0 | $content .= "$key=$value&"; | |||||||||||||
| 154 | } | ||||||||||||||
| 155 | |||||||||||||||
| 156 | } | ||||||||||||||
| 157 | |||||||||||||||
| 158 | 1 | $req->content($content); | |||||||||||||
| 159 | |||||||||||||||
| 160 | 1 | my $send_method = $args{__client_send_method}; | |||||||||||||
| 161 | |||||||||||||||
| 162 | 1 | $self->$send_method($req, %args); | |||||||||||||
| 163 | 3 | ▶ | } | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 164 | |||||||||||||||
| 165 | sub delete: Log { | ||||||||||||||
| 166 | |||||||||||||||
| 167 | 0 | ▶ | my ($self, $uri, %args) = @_; | ||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 168 | |||||||||||||||
| 169 | 0 | $uri->query_form($args{GET_DATA}); | |||||||||||||
| 170 | |||||||||||||||
| 171 | 0 | my $req = HTTP::Request->new(DELETE => $uri); | |||||||||||||
| 172 | |||||||||||||||
| 173 | 0 | my $send_method = $args{__client_send_method}; | |||||||||||||
| 174 | |||||||||||||||
| 175 | 0 | $self->$send_method($req, %args); | |||||||||||||
| 176 | 3 | ▶ | } | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 177 | |||||||||||||||
| 178 | sub patch: Log { | ||||||||||||||
| 179 | 0 | ▶ | my ($self, $uri, %args) = @_; | ||||||||||||
|
Subroutine
patch: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 180 | |||||||||||||||
| 181 | 0 | $uri->query_form($args{GET_DATA}); | |||||||||||||
| 182 | |||||||||||||||
| 183 | 0 | my $req = HTTP::Request->new(PATCH => $uri); | |||||||||||||
| 184 | |||||||||||||||
| 185 | 0 | my $send_method = $args{__client_send_method}; | |||||||||||||
| 186 | |||||||||||||||
| 187 | 0 | $self->$send_method($req, %args); | |||||||||||||
| 188 | |||||||||||||||
| 189 | 3 | ▶ | } | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 190 | |||||||||||||||
| 191 | sub put: Log { | ||||||||||||||
| 192 | 0 | ▶ | my ($self, $uri, %args) = @_; | ||||||||||||
|
Subroutine
put: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||
| 193 | |||||||||||||||
| 194 | 0 | $uri->query_form($args{GET_DATA}); | |||||||||||||
| 195 | |||||||||||||||
| 196 | 0 | my $req = HTTP::Request->new(PUT=> $uri); | |||||||||||||
| 197 | |||||||||||||||
| 198 | 0 | my $send_method = $args{__client_send_method}; | |||||||||||||
| 199 | |||||||||||||||
| 200 | 0 | ▶ | my $headers = $args{HEADER_DATA} || {'Content-Type' => 'application/json'}; | ||||||||||||
|
Condition0%0/2
$args{'HEADER_DATA'} || {'Content-Type', 'application/json'}
MC/DC0%0/1
$args{'HEADER_DATA'} || {'Content-Type', 'application/json'}
$args{'HEADER_DATA'}
Truth table0%0/2
A
A= $args{'HEADER_DATA'}
| |||||||||||||||
| 201 | |||||||||||||||
| 202 | 0 | $req->header(%$headers); | |||||||||||||
| 203 | |||||||||||||||
| 204 | 0 | my $content; | |||||||||||||
| 205 | |||||||||||||||
| 206 | 0 | ▶ | if($req->header("Content-Type") eq "application/json"){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 207 | |||||||||||||||
| 208 | $content = JSON->new->allow_blessed(1) | ||||||||||||||
| 209 | ->convert_blessed(1)->utf8 | ||||||||||||||
| 210 | 0 | ▶ | ->encode($args{POST_DATA} || {}); | ||||||||||||
|
Condition0%0/2
$args{'POST_DATA'} || {}
MC/DC0%0/1
$args{'POST_DATA'} || {}
$args{'POST_DATA'}
Truth table0%0/2
A
A= $args{'POST_DATA'}
| |||||||||||||||
| 211 | } | ||||||||||||||
| 212 | else{ | ||||||||||||||
| 213 | |||||||||||||||
| 214 | # application/x-www-form-urlencoded | ||||||||||||||
| 215 | # raw stream | ||||||||||||||
| 216 | 0 | ▶ | while(my ($key, $value) = each (%{$args{POST_DATA} || {}})){ | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 217 | 0 | $content .= "$key=$value&"; | |||||||||||||
| 218 | } | ||||||||||||||
| 219 | |||||||||||||||
| 220 | |||||||||||||||
| 221 | } | ||||||||||||||
| 222 | |||||||||||||||
| 223 | 0 | $req->content($content); | |||||||||||||
| 224 | |||||||||||||||
| 225 | 0 | $self->$send_method($req, %args); | |||||||||||||
| 226 | 3 | ▶ | } | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 227 | |||||||||||||||
| 228 | |||||||||||||||
| 229 | sub build_uri { | ||||||||||||||
| 230 | 5 | ▶ | my ($self, $entity, $id,$action, $implicit_format) = @_; | ||||||||||||
|
Subroutine
build_uri: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||
| 231 | |||||||||||||||
| 232 | 5 | my $uri = $self->{endpoint}.'/'.$entity; | |||||||||||||
| 233 | |||||||||||||||
| 234 | 5 | ▶ | $uri .= '/'.$id if(defined($id)); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 235 | 5 | ▶ | $uri .= '/'.$action if(defined($action)); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 236 | |||||||||||||||
| 237 | 5 | ▶ | return URI->new($uri) if($implicit_format); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 238 | |||||||||||||||
| 239 | ($self->current_method =~ /^get/)? | ||||||||||||||
| 240 | |||||||||||||||
| 241 | 3 | ▶ | URI->new($uri.'/'.$self->{format}) : | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 242 | |||||||||||||||
| 243 | URI->new($uri); | ||||||||||||||
| 244 | } | ||||||||||||||
| 245 | |||||||||||||||
| 246 | |||||||||||||||
| 247 | sub generate_query_str { | ||||||||||||||
| 248 | 0 | ▶ | my ($self, %args) = @_; | ||||||||||||
|
Subroutine
generate_query_str: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 249 | |||||||||||||||
| 250 | 0 | join '&', map {"$_=$args{$_}"} keys(%args); | |||||||||||||
| 251 | } | ||||||||||||||
| 252 | |||||||||||||||
| 253 | |||||||||||||||
| 254 | sub __send{ | ||||||||||||||
| 255 | 5 | ▶ | my ($self, $req, %args) = @_; | ||||||||||||
|
Subroutine
__send: called CC=1 SCAR=0.0
| |||||||||||||||
| 256 | |||||||||||||||
| 257 | Eixo::Rest::RequestSync->new( | ||||||||||||||
| 258 | |||||||||||||||
| 259 | callback=>$args{__callback}, | ||||||||||||||
| 260 | |||||||||||||||
| 261 | 5 | %{$args{PROCESS_DATA}}, | |||||||||||||
| 262 | |||||||||||||||
| 263 | __format=>$args{__format} | ||||||||||||||
| 264 | |||||||||||||||
| 265 | 5 | )->send( | |||||||||||||
| 266 | |||||||||||||||
| 267 | $self->ua(), | ||||||||||||||
| 268 | |||||||||||||||
| 269 | $req | ||||||||||||||
| 270 | |||||||||||||||
| 271 | ); | ||||||||||||||
| 272 | |||||||||||||||
| 273 | } | ||||||||||||||
| 274 | |||||||||||||||
| 275 | sub __sendAsync{ | ||||||||||||||
| 276 | 0 | ▶ | my ($self, $req, %args) = @_; | ||||||||||||
|
Subroutine
__sendAsync: not called CC=2 SCAR=17.9
| |||||||||||||||
| 277 | |||||||||||||||
| 278 | 0 | ▶ | die("unsupported: ERROR: This Perl not built to support threads\n") if (! $Config{'useithreads'}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 279 | |||||||||||||||
| 280 | 0 | eval 'use Eixo::Rest::RequestAsync'; | |||||||||||||
| 281 | |||||||||||||||
| 282 | Eixo::Rest::RequestAsync->new( | ||||||||||||||
| 283 | |||||||||||||||
| 284 | job_id=>$args{__job_id}, | ||||||||||||||
| 285 | |||||||||||||||
| 286 | api=>$args{api}, | ||||||||||||||
| 287 | |||||||||||||||
| 288 | callback=>$args{__callback}, | ||||||||||||||
| 289 | |||||||||||||||
| 290 | 0 | %{$args{PROCESS_DATA}}, | |||||||||||||
| 291 | |||||||||||||||
| 292 | __format=>$args{__format} | ||||||||||||||
| 293 | |||||||||||||||
| 294 | 0 | )->send( | |||||||||||||
| 295 | |||||||||||||||
| 296 | $self->ua(), | ||||||||||||||
| 297 | |||||||||||||||
| 298 | $req | ||||||||||||||
| 299 | |||||||||||||||
| 300 | ); | ||||||||||||||
| 301 | |||||||||||||||
| 302 | } | ||||||||||||||
| 303 | |||||||||||||||
| 304 | sub remote_error { | ||||||||||||||
| 305 | 1 | ▶ | my ($self,$response) = @_; | ||||||||||||
|
Subroutine
remote_error: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||
| 306 | |||||||||||||||
| 307 | 1 | my $status = $response->code; | |||||||||||||
| 308 | 1 | my $extra = $response->content; | |||||||||||||
| 309 | |||||||||||||||
| 310 | 1 | ▶ | if(defined($self->error_callback)){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 311 | |||||||||||||||
| 312 | 0 | &{$self->error_callback}( | |||||||||||||
| 313 | |||||||||||||||
| 314 | $self->current_method, | ||||||||||||||
| 315 | |||||||||||||||
| 316 | 'ERROR_CODE', | ||||||||||||||
| 317 | |||||||||||||||
| 318 | $status, | ||||||||||||||
| 319 | |||||||||||||||
| 320 | $extra, | ||||||||||||||
| 321 | #@extra_args | ||||||||||||||
| 322 | |||||||||||||||
| 323 | ); | ||||||||||||||
| 324 | } | ||||||||||||||
| 325 | else{ | ||||||||||||||
| 326 | 1 | die "Remote Api error: ($status). Details: $extra\n"; | |||||||||||||
| 327 | } | ||||||||||||||
| 328 | } | ||||||||||||||
| 329 | |||||||||||||||
| 330 | |||||||||||||||
| 331 | 3 | 1; | |||||||||||||