| 1 | package Net::OpenStack::Client::REST; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 7 | $Net::OpenStack::Client::REST::VERSION = '0.1.4'; | |||||||||||||||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Net::OpenStack::Client::Request qw(@METHODS_REQUIRE_OPTIONS $HDR_X_AUTH_TOKEN); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Net::OpenStack::Client::Response; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use REST::Client; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # Map with HTTP return codes indicating success | ||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # if method is missing (only) 200 is conisdered success | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # if method is present, 200 is not considered success by default | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 7 | Readonly my %SUCCESS => { | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | POST => [201], | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | PUT => [200, 201, 204], | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | DELETE => [204, 201], # yes, 201 when deleting a token | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # JSON::XS instance | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # sort the keys, to create reproducable results | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 7 | my $json = JSON::XS->new()->canonical(1); | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | =head1 methods | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | =over | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | sub _new_client | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 6 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_new_client: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 6 | my $browser = LWP::UserAgent->new(); | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | # Temporary cookie_jar | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 6 | $browser->cookie_jar( {} ); | |||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 6 | my $rc = REST::Client->new( | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | useragent => $browser, | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 6 | $self->{rc} = $rc; | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # Actual REST::Client call | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # Returns tuple repsonse, repsonse headers and error message. | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # Processes the repsonse code, including possible JSON decoding | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # Reports error and returns err (with repsonse undef) | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | sub _call | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 54 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_call: called CC=13 SCAR=27.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 54 | my ($self, $method, $url, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 54 | my $err; | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | 54 | my $rc = $self->{rc}; | |||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # make the call | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 54 | $rc->$method($url, @args); | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 54 | my $code = $rc->responseCode(); | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | 54 | my $content = $rc->responseContent(); | |||||||||||||||||||||||||||||||||||||||||||||
| 65 | 54 | my $rheaders = {map {$_ => $rc->responseHeader($_)} $rc->responseHeaders}; | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | 54 | ▶ | my $success = grep {$code == $_} @{$SUCCESS{$method} || [200]}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$SUCCESS{$method} || [200]
MC/DC100%1/1
$SUCCESS{$method} || [200]
$SUCCESS{$method}
Truth table100%2/2
A
A= $SUCCESS{$method}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 54 | my $response; | |||||||||||||||||||||||||||||||||||||||||||||
| 69 | 54 | ▶ | my $type = $rheaders->{'Content-Type'} || 'RESPONSE_WO_CONTENT_TYPE_HEADER'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$rheaders->{'Content-Type'} || 'RESPONSE_WO_CONTENT_TYPE_HEADER'
MC/DC0%0/1
$rheaders->{'Content-Type'} || 'RESPONSE_WO_CONTENT_TYPE_HEADER'
$rheaders->{'Content-Type'}
Truth table50%1/2
A
A= $rheaders->{'Content-Type'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 54 | ▶ | if ($type =~ qr{^application/json}i) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 54 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | 54 | eval { | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | 54 | $response = $json->decode($content); | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 54 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my $report = $success ? 'error' : 'verbose'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | $self->$report("REST $method with ".($success ? 'success' : 'error'). | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | " failure to decode JSON content $content: $@"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $response = $content; | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 54 | ▶ | if ($success) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 53 | $self->verbose("Successful REST $method url $url type $type"); | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | 53 | ▶ | if ($self->{debugapi}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | # might contain sensitive data, eg security token | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 53 | my $headers_txt = join(',', map {"$_=$rheaders->{$_}"} sort keys %$rheaders); | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | 53 | $self->debug("REST $method full response headers $headers_txt"); | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | 53 | $self->debug("REST $method full response content $content"); | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | my $errmsg = "$method failed (url $url code $code)"; | |||||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | ▶ | if (ref($response) eq 'HASH' && | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $response eq "HASH" and $response->{"error"}
MC/DC0%0/2
ref $response eq "HASH" and $response->{"error"}
ref $response eq "HASH"
$response->{"error"}
Truth table33%1/3
A and B
A= ref $response eq "HASH"B= $response->{"error"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | $response->{error}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | $err = $response->{error}; | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | $err->{url} = $url; | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | $err->{method} = $method; | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | ▶ | $err->{code} = $code if !exists($err->{code}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | $err = $errmsg; | |||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | ▶ | $content = '<undef>' if ! defined($content); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | $errmsg = "$errmsg: $content"; | |||||||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | $self->error("REST $errmsg"); | |||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 54 | return $response, $rheaders, $err; | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | # Handle pagination: https://developer.openstack.org/api-guide/compute/paginated_collections.html | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | # For any decoded reply, walk the tree | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | # look for <key>+<key>_links combos | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # <key> should be a list (a collection) | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | # the <key>_links part should have a rel=next, href=newurl | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | # follow it, and merge the results with the original key list | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | # lets assume they are in the same relative path | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | # Aaaargh who came up with this crap | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | # Return array of paths that have to processed for paging | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | # Each element is a tuple of path (as arrayref of subpaths) | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | # and url to follow | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | # Assumes that all responses can be retrieved from | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | # and be joined using the same path in JSON | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | # Only pass hashref as data. | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | # TODO: support lookup of links in arrays? | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | sub _page_paths | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 73 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_page_paths: called CC=11 SCAR=24.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 73 | my ($self, $data) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 73 | my @paths; | |||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 73 | foreach my $key (sort keys %$data) { | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | 88 | my $lkey = $key."_links"; | |||||||||||||||||||||||||||||||||||||||||||||
| 136 | 88 | my $ref = ref($data->{$key}); | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | 88 | ▶ | if (exists($data->{$lkey}) && | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%4/6
exists $data->{$lkey} and $ref eq "ARRAY" and ref $data->{$lkey} eq "ARRAY"
exists $data->{$lkey} and $ref eq "ARRAY"
MC/DC33%1/3
exists $data->{$lkey} and $ref eq "ARRAY" and ref $data->{$lkey} eq "ARRAY"
exists $data->{$lkey}
$ref eq "ARRAY"
ref $data->{$lkey} eq "ARRAY"
Truth table50%2/4
A and B and C
A= exists $data->{$lkey}B= $ref eq "ARRAY"C= ref $data->{$lkey} eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 138 | $ref eq 'ARRAY' && | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | ref($data->{$lkey}) eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||||||
| 140 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 6 | foreach my $link (@{$data->{$lkey}}) { | |||||||||||||||||||||||||||||||||||||||||||||
| 142 | 6 | ▶ | if (exists($link->{rel}) && | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
exists $link->{"rel"} and $link->{"rel"} eq "next" and exists $link->{"href"}
exists $link->{"rel"} and $link->{"rel"} eq "next"
MC/DC33%1/3
exists $link->{"rel"} and $link->{"rel"} eq "next" and exists $link->{"href"}
exists $link->{"rel"}
$link->{"rel"} eq "next"
exists $link->{"href"}
Truth table50%2/4
A and B and C
A= exists $link->{"rel"}B= $link->{"rel"} eq "next"C= exists $link->{"href"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 143 | $link->{rel} eq 'next' && | ||||||||||||||||||||||||||||||||||||||||||||||
| 144 | exists($link->{href})) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | # only first one | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 4 | push(@paths, [[$key], $link->{href}]); | |||||||||||||||||||||||||||||||||||||||||||||
| 147 | 4 | last; | |||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 150 | } elsif ($ref eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 18 | foreach my $rpath_tuple ($self->_page_paths($data->{$key})) { | |||||||||||||||||||||||||||||||||||||||||||||
| 152 | # add current key to path element (i.e. the path is relative to $key) | ||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 6 | unshift(@{$rpath_tuple->[0]}, $key); | |||||||||||||||||||||||||||||||||||||||||||||
| 154 | 6 | push(@paths, $rpath_tuple); | |||||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 73 | return @paths; | |||||||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | # only hashrefs | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | sub _page | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 54 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_page: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 54 | my ($self, $method, $response, $headers) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 54 | my $err; | |||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 54 | foreach my $path_tuple ($self->_page_paths($response)) { | |||||||||||||||||||||||||||||||||||||||||||||
| 170 | # No body, this is GET only | ||||||||||||||||||||||||||||||||||||||||||||||
| 171 | # We only care about the response headers of the first batch | ||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 2 | $self->debug("_page method $method url $path_tuple->[1]"); | |||||||||||||||||||||||||||||||||||||||||||||
| 173 | 2 | my ($tresponse, $trheaders, $terr) = $self->_call($method, $path_tuple->[1], $headers); | |||||||||||||||||||||||||||||||||||||||||||||
| 174 | 2 | ▶ | if ($terr) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 175 | # no temp err here, a failure in the paged GET repsonse is a failure nonetheless | ||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | $err = $terr; | |||||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||
| 178 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 2 | my @path = @{$path_tuple->[0]}; | |||||||||||||||||||||||||||||||||||||||||||||
| 180 | # extend path of tresponse in path of response | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 2 | my $rarray = $response; | |||||||||||||||||||||||||||||||||||||||||||||
| 182 | 2 | foreach my $p (@path) { | |||||||||||||||||||||||||||||||||||||||||||||
| 183 | 5 | $rarray = $rarray->{$p}; | |||||||||||||||||||||||||||||||||||||||||||||
| 184 | 5 | $tresponse = $tresponse->{$p}; | |||||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 2 | push(@$rarray, @$tresponse); | |||||||||||||||||||||||||||||||||||||||||||||
| 187 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 54 | return $response, $err; | |||||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||
| 193 | =item rest | ||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||
| 195 | Given a Request instance C<req>, perform this request. | ||||||||||||||||||||||||||||||||||||||||||||||
| 196 | All options are passed to the headers method. | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | The token option is added if the token attribute exists and | ||||||||||||||||||||||||||||||||||||||||||||||
| 198 | if not token option was already in the options. | ||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||
| 200 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | sub rest | ||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 54 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rest: called CC=15 SCAR=27.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 54 | my ($self, $req, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||
| 206 | # methods that require options, must pass said option as body | ||||||||||||||||||||||||||||||||||||||||||||||
| 207 | # general call is $rc->$method($url, [body if options], $headers) | ||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 54 | my $method = $req->{method}; | |||||||||||||||||||||||||||||||||||||||||||||
| 210 | 54 | my $rservice = $req->{service}; | |||||||||||||||||||||||||||||||||||||||||||||
| 211 | 54 | my $service; | |||||||||||||||||||||||||||||||||||||||||||||
| 212 | 54 | ▶ | if ($rservice) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 54 | $service = $self->{services}->{$rservice}; | |||||||||||||||||||||||||||||||||||||||||||||
| 214 | 54 | ▶ | if (!$service) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 3 | $self->debug("REST $method request endpoint $req->{endpoint} service $rservice has no known service"); | |||||||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 217 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | $self->debug("REST $method request endpoint $req->{endpoint} has no service"); | |||||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||
| 221 | # url | ||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 54 | my $url = $req->endpoint($service); | |||||||||||||||||||||||||||||||||||||||||||||
| 223 | 54 | ▶ | if (!$url) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | my $msg = "REST $method request endpoint $req->{endpoint} ". | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 225 | ($service ? "service $service" : "no service")." has no endpoint url"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | $self->error($msg); | |||||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | return mkresponse(error => $msg); | |||||||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 54 | my @args = ($url); | |||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||
| 232 | # body if needed | ||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 54 | my $body; | |||||||||||||||||||||||||||||||||||||||||||||
| 234 | 54 | ▶ | if (grep {$method eq $_} @METHODS_REQUIRE_OPTIONS) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 27 | my $data = $req->opts_data; | |||||||||||||||||||||||||||||||||||||||||||||
| 236 | 27 | $body = $json->encode($data); | |||||||||||||||||||||||||||||||||||||||||||||
| 237 | 27 | push(@args, $body); | |||||||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||
| 240 | # headers | ||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 54 | ▶ | $opts{token} = $self->{token} if (exists($self->{token}) && !exists $opts{token}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $self->{"token"} and not exists $opts{"token"}
MC/DC50%1/2
exists $self->{"token"} and not exists $opts{"token"}
exists $self->{"token"}
not exists $opts{"token"}
Truth table66%2/3
A and not(B)
A= exists $self->{"token"}B= not exists $opts{"token"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 54 | my $headers = $req->headers(%opts); | |||||||||||||||||||||||||||||||||||||||||||||
| 243 | 54 | push(@args, $headers); | |||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 54 | ▶ | $self->debug("REST $method url $url, ".(defined $body ? '' : 'no ')."body, headers ".join(',', sort keys %$headers)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 54 | ▶ | if ($self->{debugapi}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | # might contain sensitive data, eg security token | ||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 54 | my $headers_txt = join(',', map {"$_=$headers->{$_}"} sort keys %$headers); | |||||||||||||||||||||||||||||||||||||||||||||
| 249 | 54 | $self->debug("REST $method full headers $headers_txt"); | |||||||||||||||||||||||||||||||||||||||||||||
| 250 | 54 | ▶ | $self->debug("REST $method full body $body") if $body; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 54 | my ($response, $rheaders, $err) = $self->_call($method, @args); | |||||||||||||||||||||||||||||||||||||||||||||
| 254 | # The err here could be a failure in the paged GET repsonse | ||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 54 | ▶ | ($response, $err) = $self->_page($method, $response, $headers) if (!$err && $response && ref($response) eq 'HASH'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
not $err and $response and ref $response eq "HASH"
not $err and $response
MC/DC33%1/3
not $err and $response and ref $response eq "HASH"
not $err
$response
ref $response eq "HASH"
Truth table50%2/4
not(A) and B and C
A= not $errB= $responseC= ref $response eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 54 | my %ropts = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 258 | data => $response, | ||||||||||||||||||||||||||||||||||||||||||||||
| 259 | headers => $rheaders, | ||||||||||||||||||||||||||||||||||||||||||||||
| 260 | error => $err, | ||||||||||||||||||||||||||||||||||||||||||||||
| 261 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 54 | ▶ | $ropts{result_path} = $req->{result} if defined $req->{result}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 54 | return mkresponse(%ropts); | |||||||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||
| 266 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||
| 268 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||
| 270 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||||||