blib/lib/Confluence/REST.pm

filter: Statementstmt 25.6% 40 / 156 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 54.5% 12 / 22 Podpod 100% 6 / 6 totaltotal 19.0% 58 / 305 CC 57 SCAR 48.1
CC
57
Cov
15%
_error
272.0
new
240.0
next_result
90.0
CRAP 122.5
↑ Summary
1 package Confluence::REST;
2  
3 # ABSTRACT: Thin wrapper around Confluence's REST API
4 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Util qw/:check/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use REST::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 $CONFLUENCE::REST::VERSION = '0.011';
18  
19 1 our $DEBUG_REQUESTS_P = 0;
20 1 our $DEBUG_JSON_P = 0;
21 1 our $DEBUG_ITERATORS = 0;
22  
23 =pod
24  
25 =encoding UTF-8
26  
27 =head1 NAME
28  
29 Confluence::REST - Thin wrapper around Confluence's REST API
30  
31 =head1 VERSION
32  
33 version 0.01
34  
35 =head1 SYNOPSIS
36  
37 use Confluence::REST;
38 use Data::Dumper;
39  
40 my $confluence = Confluence::REST->new('https://confluence.example.net');
41  
42 # Set up an iterator
43 $confluence->set_search_iterator(
44 {
45 cql => 'type = "page" and space = "home"',
46 expand => 'metadata.labels',
47 }
48 );
49  
50 # Keep bumping the iterator for the next page of results
51 while ( my $result = $confluence->next_result ) {
52  
53 # Print the hashref representing the JSON response
54 print Dumper $result;
55 }
56  
57  
58 =head1 DESCRIPTION
59  
60 Confluence::REST - Thin wrapper around Confluence's REST API
61  
62 L<Confluence|http://www.atlassian.com/software/confluence> is a proprietary
63 wiki from L<Atlassian|http://www.atlassian.com/>.
64  
65 This module is a thin wrapper around L<Confluence's REST
66 API|https://developer.atlassian.com/confcloud/confluence-rest-api-39985291.html>,
67 which is superseding its old SOAP API. (If you want to interact with
68 the SOAP API, there's another Perl module called
69 L<Confluence::Client::XMLRPC|https://github.com/heikojansen/confluence-client-xmlrpc>.
70  
71 This code is basically L<JIRA::REST|metacpan.org/pod/JIRA::REST> with
72 some tweaks to get it to work with the Confluence REST API.
73  
74 Copyright (c) 2013 by CPqD (http://www.cpqd.com.br/).
75  
76 Copyright (c) 2016 (Changes to adapt to Confluence REST APIs) by Rich Loveland.
77  
78 This is free software; you can redistribute it and/or modify it under
79 the same terms as the Perl 5 programming language system itself.
80  
81 =head1 METHODS
82  
83 =over
84  
85 =item new(URL, USERNAME, PASSWORD [, REST_CLIENT_CONFIG])
86  
87 The constructor needs up to four arguments:
88  
89 The URL is a string or a URI object denoting the base URL of the Confluence
90 server. This is a required argument.
91  
92 You may choose a specific API version by appending the
93 C</rest/api/VERSION> string to the URL's path. It's more common to
94 leave it unspecified, in which case the C</rest/api/latest> string is
95 appended automatically to the URL.
96  
97 The USERNAME of a Confluence user can be undefined if PASSWORD is also
98 undefined. In such a case the user credentials are looked up in the
99 C<.netrc> file.
100  
101 The HTTP PASSWORD of the user can be undefined, in which case the user
102 credentials are looked up in the C<.netrc> file. (This is the password
103 the user uses to log in to Confluence's web interface.)
104  
105 The REST_CLIENT_CONFIG is a REST::Client object used to make the REST
106 invocations. This optional argument must be a hashref that can be fed
107 to the REST::Client constructor. Note that the C<URL> argument
108 overwrites any value associated with the C<host> key in this hash.
109  
110 To use a network proxy please set the 'proxy' argument to the string
111 or URI object describing the fully qualified (including port) URL to
112 your network proxy. This is an extension to the REST::Client
113 configuration and will be removed from the hash before passing it on
114 to the REST::Client constructor.
115  
116 =cut
117  
118 0 ▶sub new {
Subroutine new: not called CC=15 SCAR=54.8
Pod documented
119 0 my ($class, $URL, $username, $password, $rest_client_config) = @_;
120  
121 0 ▶ $URL = URI->new($URL) if is_string($URL);
Branch: 1 branch
BranchTrueFalseTotal
if is_string($URL) 0 0 0
122 0 ▶ is_instance($URL, 'URI')
Branch: 1 branch
BranchTrueFalseTotal
is_instance($URL, "URI") or croak("Confluence::REST::new: URL argument must be a string or a URI object.\n") 0 0 0
123 or croak __PACKAGE__
124 . "::new: URL argument must be a string or a URI object.\n";
125  
126 # Choose the latest REST API unless already specified
127 0 ▶ unless ($URL->path =~ m@/rest/api/(?:\d+|content)/?$@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($URL->path =~ m[/rest/api/(?:\d+|content)/?$]) 0 0 0
128 0 $URL->path($URL->path . '/rest/api/content');
129 }
130  
131 # If no password is set we try to lookup the credentials in the .netrc file
132 0 ▶ if (!defined $password) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $password) 0 0 0
133 0 ▶ eval { require Net::Netrc }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Net::Netrc } } or croak("Can't require Net::Netrc module. Please, specify the USERNAME and PASSWORD.\n") 0 0 0
134 or croak
135 "Can't require Net::Netrc module. Please, specify the USERNAME and PASSWORD.\n";
136 0 ▶ if (my $machine = Net::Netrc->lookup($URL->host, $username))
Branch: 1 branch
BranchTrueFalseTotal
if (my $machine = 'Net::Netrc'->lookup($URL->host, $username)) { } 0 0 0
137 { # $username may be undef
138 0 $username = $machine->login;
139 0 $password = $machine->password;
140 }
141 else {
142 0 croak "No credentials found in the .netrc file.\n";
143 }
144 }
145  
146 0 ▶ is_string($username)
Branch: 1 branch
BranchTrueFalseTotal
is_string($username) or croak("Confluence::REST::new: USERNAME argument must be a string.\n") 0 0 0
147 or croak __PACKAGE__ . "::new: USERNAME argument must be a string.\n";
148  
149 0 ▶ is_string($password)
Branch: 1 branch
BranchTrueFalseTotal
is_string($password) or croak("Confluence::REST::new: PASSWORD argument must be a string.\n") 0 0 0
150 or croak __PACKAGE__ . "::new: PASSWORD argument must be a string.\n";
151  
152 0 ▶ $rest_client_config = {} unless defined $rest_client_config;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest_client_config 0 0 0
153 0 ▶ is_hash_ref($rest_client_config)
Branch: 1 branch
BranchTrueFalseTotal
is_hash_ref($rest_client_config) or croak("Confluence::REST::new: REST_CLIENT_CONFIG argument must be a hash-ref.\n") 0 0 0
154 or croak __PACKAGE__
155 . "::new: REST_CLIENT_CONFIG argument must be a hash-ref.\n";
156  
157 # remove the REST::Client faux config value 'proxy' if set and use it
158 # ourselves.
159 0 my $proxy = delete $rest_client_config->{proxy};
160  
161 0 ▶ if ($proxy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proxy) 0 0 0
162 0 ▶ is_string($proxy) || is_instance($proxy, 'URI')
Branch: 1 branch
BranchTrueFalseTotal
is_string($proxy) or is_instance($proxy, "URI") or croak("Confluence::REST::new: 'proxy' rest client attribute must be a string or a URI object.\n") 0 0 0
Condition0%0/3
is_string($proxy) or is_instance($proxy, "URI")
l!l&&r!l&&!r
000
MC/DC0%0/2
is_string($proxy) or is_instance($proxy, "URI")
is_string($proxy) is_instance($proxy, "URI")
Truth table0%0/3
A or B
A= is_string($proxy)B= is_instance($proxy, "URI")
ABresult
1X1
011
000
163 or croak __PACKAGE__
164 . "::new: 'proxy' rest client attribute must be a string or a URI object.\n";
165 }
166  
167 0 my $rest = REST::Client->new($rest_client_config);
168  
169 # Set proxy to be used
170 0 ▶ if ($proxy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proxy) 0 0 0
171 0 $rest->getUseragent->proxy(['http', 'https'] => $proxy);
172 }
173  
174 # Set default base URL
175 0 $rest->setHost($URL);
176  
177 # Follow redirects/authentication by default
178 0 $rest->setFollow(1);
179  
180 # Since Confluence doesn't send an authentication challenge, we may
181 # simply force the sending of the authentication header.
182 0 $rest->addHeader(
183 Authorization => 'Basic ' . encode_base64("$username:$password"));
184  
185 # Configure UserAgent name
186 0 $rest->getUseragent->agent(__PACKAGE__);
187  
188 return
189 0 bless {rest => $rest, json => JSON->new->utf8->allow_nonref,} => $class;
190 }
191  
192 0 ▶sub _error {
Subroutine _error: not called CC=16 SCAR=56.1
193 0 my ($self, $content, $type, $code) = @_;
194  
195 0 ▶ $type = 'text/plain' unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 0 0
196 0 ▶ $code = 500 unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 0 0
197  
198 0 my $msg = __PACKAGE__ . " Error[$code";
199  
200 0 ▶ if (eval { require HTTP::Status }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require HTTP::Status } }) 0 0 0
201 0 ▶ if (my $status = HTTP::Status::status_message($code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $status = &HTTP::Status::status_message($code)) 0 0 0
202 0 $msg .= " - $status";
203 }
204 }
205  
206 0 $msg .= "]:\n";
207  
208 0 ▶ if ($type =~ m:text/plain:i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type =~ m[text/plain]i) { } 0 0 0
elsif ($type =~ m[application/json]) { } 0 0 0
elsif ($type =~ m[text/html]i and eval { do { require HTML::TreeBuilder } }) { } 0 0 0
elsif ($type =~ m[^(text/|application|xml)]i) { } 0 0 0
209 0 $msg .= $content;
210 }
211 elsif ($type =~ m:application/json:) {
212 0 my $error = $self->{json}->decode($content);
213 0 ▶ if (ref $error eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $error eq 'HASH') { } 0 0 0
214  
215 # Confluence errors may be laid out in all sorts of ways. You have to
216 # look them up from the scant documentation at
217 # https://docs.atlassian.com/confluence/REST/latest/.
218  
219 # /issue/bulk tucks the errors one level down, inside the
220 # 'elementErrors' hash.
221 $error = $error->{elementErrors}
222 0 ▶ if exists $error->{elementErrors};
Branch: 1 branch
BranchTrueFalseTotal
if exists $error->{"elementErrors"} 0 0 0
223  
224 # Some methods tuck the errors in the 'errorMessages' array.
225 0 ▶ if (my $errorMessages = $error->{errorMessages}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $errorMessages = $error->{"errorMessages"}) 0 0 0
226 0 $msg .= "- $_\n" foreach @$errorMessages;
227 }
228  
229 # And some tuck them in the 'errors' hash.
230 0 ▶ if (my $errors = $error->{errors}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $errors = $error->{"errors"}) 0 0 0
231 0 $msg .= "- [$_] $errors->{$_}\n" foreach sort keys %$errors;
232 }
233 }
234 else {
235 0 $msg .= $content;
236 }
237 }
238 0 ▶ elsif ($type =~ m:text/html:i && eval { require HTML::TreeBuilder }) {
Condition0%0/3
$type =~ m[text/html]i and eval { do { require HTML::TreeBuilder } }
!ll&&!rl&&r
000
MC/DC0%0/2
$type =~ m[text/html]i and eval { do { require HTML::TreeBuilder } }
$type =~ m[text/html]i eval { do { require HTML::TreeBuilder } }
Truth table0%0/3
A and B
A= $type =~ m[text/html]iB= eval { do { require HTML::TreeBuilder } }
ABresult
0X0
100
111
239 0 $msg .= HTML::TreeBuilder->new_from_content($content)->as_text;
240 }
241 elsif ($type =~ m:^(text/|application|xml):i) {
242 0 $msg .= "<Content-Type: $type>$content</Content-Type>";
243 }
244 else {
245 0 $msg
246 .= "<Content-Type: $type>(binary content not shown)</Content-Type>";
247 }
248 0 $msg =~ s/\n*$/\n/s; # end message with a single newline
249 0 return $msg;
250 }
251  
252 0 ▶sub _content {
Subroutine _content: not called CC=7 SCAR=40.3
253 0 my ($self) = @_;
254  
255 0 my $rest = $self->{rest};
256 0 my $code = $rest->responseCode();
257 0 my $type = $rest->responseHeader('Content-Type');
258 0 my $content = $rest->responseContent();
259  
260 0 ▶ $code =~ /^2/ or croak $self->_error($content, $type, $code);
Branch: 1 branch
BranchTrueFalseTotal
$code =~ /^2/ or croak($self->_error($content, $type, $code)) 0 0 0
261  
262 0 ▶ return unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 0 0
263  
264 0 ▶ if (!defined $type) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $type) { } 0 0 0
elsif ($type =~ m[^application/json]i) { } 0 0 0
elsif ($type =~ m[^text/plain]i) { } 0 0 0
265 0 croak $self->_error(
266 "Cannot convert response content with no Content-Type specified."
267 );
268 }
269 elsif ($type =~ m:^application/json:i) {
270 0 my $decoded = $self->{json}->decode($content);
271 0 ▶ print Dumper $decoded if $DEBUG_JSON_P;
Branch: 1 branch
BranchTrueFalseTotal
if $Confluence::REST::DEBUG_JSON_P 0 0 0
272 0 return $decoded;
273 }
274 elsif ($type =~ m:^text/plain:i) {
275 0 return $content;
276 }
277 else {
278 0 croak $self->_error(
279 "I don't understand content with Content-Type '$type'.");
280 }
281 }
282  
283 0 ▶sub _build_query {
Subroutine _build_query: not called CC=2 SCAR=17.9
284 0 my ($self, $query) = @_;
285  
286 0 ▶ is_hash_ref($query)
Branch: 1 branch
BranchTrueFalseTotal
is_hash_ref($query) or croak($self->_error("The QUERY argument must be a hash-ref.")) 0 0 0
287 or croak $self->_error("The QUERY argument must be a hash-ref.");
288  
289 return '?'
290 0 . join('&', map { $_ . '=' . uri_escape($query->{$_}) } keys %$query);
291 }
292  
293 =item GET(PATH [, QUERY])
294  
295 Thin wrapper around the underlying REST::Client method.
296  
297 Takes a required PATH and an optional QUERY string as arguments.
298  
299 =cut
300  
301 0 ▶sub GET {
Subroutine GET: not called CC=3 SCAR=24.8
Pod documented
302 0 my ($self, $path, $query) = @_;
303  
304 0 ▶ $path .= $self->_build_query($query) if $query;
Branch: 1 branch
BranchTrueFalseTotal
if $query 0 0 0
305  
306 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
if ($Confluence::REST::DEBUG_REQUESTS_P) 0 0 0
307 0 print "GET: $path\n";
308 } if $DEBUG_REQUESTS_P;
309  
310 0 $self->{rest}->GET($path);
311  
312 0 return $self->_content();
313 }
314  
315 =item DELETE(PATH [, QUERY])
316  
317 Thin wrapper around the underlying REST::Client method.
318  
319 Takes a required PATH and an optional QUERY string as arguments.
320  
321 =cut
322  
323 0 ▶sub DELETE {
Subroutine DELETE: not called CC=3 SCAR=24.8
324 0 my ($self, $path, $query) = @_;
325  
326 0 ▶ $path .= $self->_build_query($query) if $query;
Branch: 1 branch
BranchTrueFalseTotal
if $query 0 0 0
327  
328 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
if ($Confluence::REST::DEBUG_REQUESTS_P) 0 0 0
329 0 print "DELETE: $path\n";
330 } if $DEBUG_REQUESTS_P;
331  
332 0 $self->{rest}->DELETE($path);
333  
334 0 return $self->_content();
335 }
336  
337 =item PUT(PATH, [QUERY], VALUE, [HEADERS])
338  
339 Thin wrapper around the underlying REST::Client method.
340  
341 Takes as arguments: a required PATH, an optional QUERY string, an
342 required hashref VALUE which is encoded as JSON, and an optional
343 hashref of HEADERS.
344  
345 =cut
346  
347 0 ▶sub PUT {
Subroutine PUT: not called CC=5 SCAR=34.0
Pod documented
348 0 my ($self, $path, $query, $value, $headers) = @_;
349  
350 0 ▶ defined $value
Branch: 1 branch
BranchTrueFalseTotal
defined $value or croak($self->_error("PUT method's 'value' argument is undefined.")) 0 0 0
351 or croak $self->_error("PUT method's 'value' argument is undefined.");
352  
353 0 ▶ $path .= $self->_build_query($query) if $query;
Branch: 1 branch
BranchTrueFalseTotal
if $query 0 0 0
354  
355 0 ▶ $headers //= {};
Condition0%0/2
$headers //= {}
l!l
00
MC/DC0%0/1
$headers //= {}
$headers
Truth table0%0/2
A
A= $headers
Aresult
11
00
356 0 ▶ $headers->{'Content-Type'} //= 'application/json;charset=UTF-8';
Condition0%0/2
$headers->{"Content-Type"} //= "application/json;charset=UTF-8"
l!l
00
MC/DC0%0/1
$headers->{"Content-Type"} //= "application/json;charset=UTF-8"
$headers->{"Content-Type"}
Truth table0%0/2
A
A= $headers->{"Content-Type"}
Aresult
11
00
357  
358 0 $self->{rest}->PUT($path, $self->{json}->encode($value), $headers);
359  
360 0 return $self->_content();
361 }
362  
363 =item POST(PATH, [QUERY], VALUE, [HEADERS])
364  
365 Thin wrapper around the underlying REST::Client method.
366  
367 Takes as arguments: a required PATH, an optional QUERY string, a
368 required hashref VALUE which is encoded as JSON, and an optional
369 hashref of HEADERS.
370  
371 =cut
372  
373 0 ▶sub POST {
Subroutine POST: not called CC=5 SCAR=34.0
Pod documented
374 0 my ($self, $path, $query, $value, $headers) = @_;
375  
376 0 ▶ defined $value
Branch: 1 branch
BranchTrueFalseTotal
defined $value or croak($self->_error("POST method's 'value' argument is undefined.")) 0 0 0
377 or croak $self->_error("POST method's 'value' argument is undefined.");
378  
379 0 ▶ $path .= $self->_build_query($query) if $query;
Branch: 1 branch
BranchTrueFalseTotal
if $query 0 0 0
380  
381 0 ▶ $headers //= {};
Condition0%0/2
$headers //= {}
l!l
00
MC/DC0%0/1
$headers //= {}
$headers
Truth table0%0/2
A
A= $headers
Aresult
11
00
382 0 ▶ $headers->{'Content-Type'} //= 'application/json;charset=UTF-8';
Condition0%0/2
$headers->{"Content-Type"} //= "application/json;charset=UTF-8"
l!l
00
MC/DC0%0/1
$headers->{"Content-Type"} //= "application/json;charset=UTF-8"
$headers->{"Content-Type"}
Truth table0%0/2
A
A= $headers->{"Content-Type"}
Aresult
11
00
383  
384 0 $self->{rest}->POST($path, $self->{json}->encode($value), $headers);
385  
386 0 return $self->_content();
387 }
388  
389 =item set_search_iterator(PARAMS)
390  
391 Used to create an "iterator" against which you will later "kick" for
392 results (in HOP parlance), using the C<next_result> method. PARAMS
393 must conform to the query parameters supported by the Confluence API.
394  
395 $confluence->set_search_iterator(
396 {
397 cql => 'label = test and type = page',
398 expand => 'metadata.labels',
399 }
400 );
401  
402 =cut
403  
404 0 ▶sub set_search_iterator {
Subroutine set_search_iterator: not called CC=1 SCAR=6.9
Pod documented
405 0 my ($self, $params) = @_;
406  
407 0 my %params = (%$params); # rebuild the hash to own it
408  
409 0 $params{start} = 0;
410 0 $params{limit} = 25;
411  
412 $self->{iter} = {
413 0 params => \%params, # params hash to be used in the next call
414 offset => 0, # offset of the next issue to be fetched
415 results => { # results of the last call
416 start => 0,
417 limit => 25,
418 json => {},
419 },
420 };
421  
422 0 return;
423 }
424  
425 =item next_result()
426  
427 Call this method to get the next page of results from your Confluence
428 API call. Requires that you have already called
429 C<set_search_iterator>.
430  
431 while ( my $item = $confluence->next_result ) {
432 # ... do things with the result
433 }
434  
435 =back
436  
437 =cut
438  
439 0 ▶sub next_result {
Subroutine next_result: not called CC=9 SCAR=45.0
Pod documented
440 0 my ($self) = @_;
441 0 state $calls = 0;
442  
443 my $iter = $self->{iter}
444 0 ▶ or croak $self->_error(
Branch: 1 branch
BranchTrueFalseTotal
my $iter = $self->{"iter"} or croak($self->_error("You must call set_search_iterator before calling next_result")) 0 0 0
445 "You must call set_search_iterator before calling next_result");
446  
447 0 my $has_next_page = $iter->{results}{json}{_links}{next};
448  
449 0 ▶ if (!$has_next_page && $calls >= 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $has_next_page and $calls >= 1) { } 0 0 0
elsif ($iter->{'offset'} % $iter->{'results'}{'limit'} == 0) { } 0 0 0
elsif ($calls == 0) { } 0 0 0
Condition0%0/3
not $has_next_page and $calls >= 1
!ll&&!rl&&r
000
MC/DC0%0/2
not $has_next_page and $calls >= 1
not $has_next_page $calls >= 1
Truth table0%0/3
not(A) and B
A= not $has_next_pageB= $calls >= 1
ABresult
0X0
100
111
450  
451 # If there is no next page, we've reached the end of the search results
452 0 $self->{iter} = undef;
453 0 return;
454 }
455 elsif ($iter->{offset} % $iter->{results}{limit} == 0) {
456  
457 # If the number of calls to the API so far is 0,
458 # OR,
459 # if the offset is divisible by the page limit (meaning that we've
460 # worked through the current page of responses), we need to:
461 #
462 # 1. bump the start pointer by LIMIT (unless no calls have been made)
463 #
464 # 2. fetch the next page of results
465 #
466  
467 0 ▶ $iter->{params}{start} += $iter->{results}{limit} if $calls > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $calls > 0 0 0 0
468 0 $iter->{results}{json} = $self->GET('/search', $iter->{params});
469 0 $calls++;
470  
471 0 ▶ print Dumper $iter if $DEBUG_ITERATORS;
Branch: 1 branch
BranchTrueFalseTotal
if $Confluence::REST::DEBUG_ITERATORS 0 0 0
472 }
473 elsif ($calls == 0) {
474 0 $iter->{params}{start} += $iter->{results}{limit};
475 }
476  
477 # If neither of the above conditions are true (meaning that we DO have a
478 # next page of results but we HAVE NOT yet reached the page offset limit,
479 # we need to:
480 #
481 # + return the next item in the search result ...
482 #
483 # + the index of which will be the sum of: the offset minus the start, e.g.,
484 # if the offset is 78, the start should be 75, meaning the index should be 3
485 #
486  
487 my $actual_start
488 = ($calls == 0)
489 ? $iter->{results}{start}
490 0 ▶ : $iter->{results}{json}{start};
Branch: 1 branch
BranchTrueFalseTotal
$calls == 0 ? : 0 0 0
491 0 return $iter->{results}{json}{results}[$iter->{offset}++ - $actual_start];
492 }
493  
494 1 1;
495  
496 __END__
↑ Summary