blib/lib/Hypothesis/API.pm

filter: Statementstmt 18.9% 57 / 301 Branchbran 0.0% 0 / 122 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 46.6% 14 / 30 Podpod 90.0% 9 / 10 totaltotal 16.0% 80 / 498 CC 69 SCAR 51.3
CC
69
Cov
13%
__ANON__
552.0
create
132.0
delete_id
72.0
CRAP 169.5
↑ Summary
1 package Hypothesis::API;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use CGI::Cookie;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use URI::Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # For better performance, also install:
21 # JSON::XS
22  
23 # DEBUG
24 # use Data::Dumper;
25 #
26 # 0 = None, 5 = Max:
27 1 my $VERB = 0;
28  
29 =pod
30  
31 =head1 NAME
32  
33 Hypothesis::API - Wrapper for the hypothes.is web (HTTP) API.
34  
35 =head1 VERSION
36  
37 Version 0.13
38  
39 =cut
40  
41 1 our $VERSION = '0.13';
42  
43 =head1 SYNOPSIS
44  
45 A Perl wrapper and utility functions for the hypothes.is web (HTTP) API.
46  
47 Create a hypothes.is object.
48  
49 use Hypothesis::API;
50  
51 my $H = Hypothesis::API->new();
52  
53 # or if user-specific actions without login are needed (no known uses yet):
54 my $H = Hypothesis::API->new($username);
55  
56 # or if login is needed (usually for annotator-store alterations)
57 my $H = Hypothesis::API->new($username, $password);
58  
59  
60 Login-required functionality:
61  
62 $H->login;
63  
64 my $payload = {
65 "uri" => 'http://my.favorite.edu/doc.html',
66 "text" => "testing create in hypothes.is API"
67 };
68 my $id = $H->create($payload);
69 $H->delete_id($id);
70  
71 Search functionality (no login needed):
72  
73 my $annotation = $H->read_id($id);
74 die if ($annotation->{'id'} ne $id);
75  
76 my $page_size = 20;
77 my $iter = $H->search({limit => 100}, $page_size);
78 my @annotations;
79 while ( my $item = $iter->() ) {
80 push @annotations, $item;
81 }
82  
83 my $total = $H->search_total({limit => 100}, $page_size);
84 print "Reported $total total items.\n";
85  
86 =head1 EXPORT
87  
88 Currently nothing.
89  
90 =cut
91  
92 1 my $json = JSON->new->allow_nonref;
93 1 $json->pretty(1);
94 1 $json->canonical(1);
95  
96  
97 #
98 # TODO: add getter/setter?
99 #
100 1 my $page_size_default = 20;
101  
102 1 has 'api_url' => (
103 is => 'ro',
104 default => 'https://hypothes.is/api',
105 predicate => 'has_api_url',
106 );
107  
108 1 has 'app_url' => (
109 is => 'ro',
110 default => 'https://hypothes.is/app',
111 predicate => 'has_app_url',
112 );
113  
114 1 has 'username' => (
115 is => 'ro',
116 predicate => 'has_username',
117 );
118  
119 1 has 'password' => (
120 is => 'ro',
121 predicate => 'has_password',
122 );
123  
124 1 has 'token' => (
125 is => 'ro',
126 predicate => 'has_token',
127 writer => '_set_token',
128 init_arg => undef,
129 );
130  
131 1 has 'csrf_token' => (
132 is => 'ro',
133 predicate => 'has_csrf_token',
134 writer => '_set_csrf_token',
135 init_arg => undef,
136 );
137  
138 has 'ua' => (
139 is => 'ro',
140 0 ▶ default => sub { LWP::UserAgent->new; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 1 predicate => 'has_ua',
142 );
143  
144 has 'uri_encoder' => (
145 is => 'ro',
146 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 0 URI::Encode->new( {
148 encode_reserved => 0,
149 double_encode => 0,
150 } );
151 },
152 1 predicate => 'has_uri_encoder',
153 );
154  
155 0 ▶around BUILDARGS => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
156 0 my $orig = shift;
157 0 my $class = shift;
158  
159 0 ▶ if ( @_ >= 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ >= 2) { } 0 0 0
elsif (@_ == 1 and not ref $_[0]) { } 0 0 0
160 0 ▶ if ( @_ > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) 0 0 0
161 0 warn "At most two arguments expected in constructor.\n";
162 }
163 0 return $class->$orig( username => $_[0], password => $_[1] );
164 ▶ } elsif ( @_ == 1 && !ref $_[0] ) {
Condition0%0/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table0%0/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
165 0 return $class->$orig( username => $_[0], password => undef );
166 } else {
167 0 return $class->$orig( username => undef, password => undef );
168 }
169 1 };
170  
171 =head1 SUBROUTINES/METHODS
172  
173 =head2 create(\%payload)
174  
175 Generalized interface to POST /api/annotations
176  
177 In the simplest form, creates an annotation
178 $payload->{'text'} at $payload->{'uri'}.
179 For more sophisticated usage please see the
180 hypothes.is API documentation.
181  
182 Returns annotation id if created or HTTP status
183 code otherwise.
184  
185 =cut
186  
187 0 ▶sub create {
Subroutine create: not called CC=11 SCAR=48.8
Pod documented
188 0 my ($self, $payload) = @_;
189  
190 0 ▶ if (ref($payload) ne "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $payload ne "HASH") 0 0 0
191 0 warn 'Payload is not a hashref.\n';
192 0 return -1;
193 }
194 0 ▶ if (not exists $payload->{'uri'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $payload->{"uri"}) 0 0 0
195 0 warn "Payload does not contain a 'uri' key to be annotated.\n";
196 0 return -1;
197 }
198 0 my $payload_out = dclone $payload;
199 0 my $user = $self->username;
200 0 my $user_acct = "acct:$user\@hypothes.is";
201 0 $payload_out->{'user'} = $user_acct;
202 0 ▶ if (not exists $payload->{'permissions'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $payload->{"permissions"}) 0 0 0
203 0 $payload_out->{'permissions'} = {
204 "read" => ["group:__world__"],
205 "update" => [$user_acct],
206 "delete" => [$user_acct],
207 "admin" => [$user_acct]
208 };
209 }
210 0 ▶ if (not exists $payload->{'document'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $payload->{"document"}) 0 0 0
211 0 $payload_out->{'document'} = {};
212 }
213 0 ▶ if (not exists $payload->{'text'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $payload->{"text"}) 0 0 0
214 0 $payload_out->{'text'} = undef;
215 }
216 0 ▶ if (not exists $payload->{'tags'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $payload->{"tags"}) 0 0 0
217 0 $payload_out->{'tags'} = undef;
218 }
219 0 ▶ if (not exists $payload->{'target'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $payload->{"target"}) 0 0 0
220 0 $payload_out->{'target'} = undef;
221 }
222
223 0 my $data = $json->encode($payload_out);
224 0 my $h = HTTP::Headers->new;
225 0 $h->header(
226 'content-type' => 'application/json;charset=UTF-8',
227 'x-csrf-token' => $self->csrf_token,
228 'X-Annotator-Auth-Token' => $self->token,
229 );
230 0 $self->ua->default_headers( $h );
231 0 my $url = URI->new( "${\$self->api_url}/annotations" );
232 0 my $response = $self->ua->post( $url, Content => $data );
233 0 ▶ if ($response->code == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code == 200) { } 0 0 0
234 0 my $json_content = try_json_decode($response);
235 0 ▶ if (not $json_content) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json_content) 0 0 0
236 0 die "Was unable to decode JSON content for id from 'create' call.";
237 }
238 0 ▶ if (exists $json_content->{'id'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $json_content->{'id'}) { } 0 0 0
239 0 return $json_content->{'id'};
240 } else {
241 0 return -1;
242 }
243 } else {
244 0 return $response->code;
245 }
246 }
247  
248  
249 =head2 delete_id($id)
250  
251 Interface to DELETE /api/annotations/<id>
252  
253 Given an annotation id, returns a boolean value indicating whether or
254 not the annotation for that id has been successfully delete (1 = yes,
255 0 = no).
256  
257 =cut
258  
259 0 ▶sub delete_id {
Subroutine delete_id: not called CC=8 SCAR=42.8
Pod documented
260 0 my ($self, $id) = @_;
261 0 ▶ if (not defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 0 0
262 0 warn "No id given to delete.\n";
263 0 return 0;
264 }
265 0 my $h = HTTP::Headers->new;
266 0 $h->header(
267 'content-type' => 'application/json;charset=UTF-8',
268 'x-csrf-token' => $self->csrf_token,
269 'X-Annotator-Auth-Token' => $self->token,
270 );
271 0 $self->ua->default_headers( $h );
272 0 my $url = URI->new( "${\$self->api_url}/annotations/$id" );
273 0 my $response = $self->ua->delete( $url );
274 0 my $json_content = 0;
275 0 ▶ if ($response->code != 500) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code != 500) { } 0 0 0
276 0 $json_content = try_json_decode($response);
277 0 ▶ if (not $json_content) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json_content) 0 0 0
278 0 die "Was unable to decode JSON content for delete_id, id: $id";
279 }
280 } else {
281 0 die "Received status code ${\$response->code} from Hypothes.is in delete_id.";
282 }
283 0 my $content_type = ref($json_content);
284 0 ▶ if ($content_type eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type eq 'HASH') { } 0 0 0
285 0 ▶ if (defined $json_content->{'deleted'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json_content->{'deleted'}) { } 0 0 0
286 0 ▶ if ($json_content->{'deleted'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($json_content->{'deleted'}) { } 0 0 0
elsif (not $json_content->{'deleted'}) { } 0 0 0
287 0 return 1;
288 } elsif (not $json_content->{'deleted'}) {
289 0 return 0;
290 } else { # Never reached in current implementation
291 0 warn "unexpected deletion status: ${\$json_content->{'deleted'}}";
292 0 return 0;
293 }
294 } else {
295 0 warn "Received unexpected object: no 'deleted' entry present.";
296 0 return 0;
297 }
298 } else {
299 0 die "Got $content_type; expected an ARRAY or HASH.";
300 }
301 }
302  
303  
304 =head2 login
305  
306 Proceeds to login; on success retrieves and stores
307 CSRF and bearer tokens.
308  
309 =cut
310  
311 0 ▶sub login {
Subroutine login: not called CC=2 SCAR=17.9
Pod documented
312 0 my ($self) = @_;
313  
314 # Grab cookie_jar for csrf_token, etc.
315 0 my $request = HTTP::Request->new(GET => $self->app_url);
316 0 my $cookie_jar = HTTP::Cookies->new();
317 0 $self->ua->cookie_jar($cookie_jar);
318 0 my $response = $self->ua->request($request);
319 0 $cookie_jar->extract_cookies( $response );
320 0 my %cookies = CGI::Cookie->parse($cookie_jar->as_string);
321 0 ▶ if (exists $cookies{'Set-Cookie3: XSRF-TOKEN'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cookies{'Set-Cookie3: XSRF-TOKEN'}) { } 0 0 0
322 0 $self->_set_csrf_token($cookies{'Set-Cookie3: XSRF-TOKEN'}->value);
323 } else {
324 0 warn "Login failed: couldn't obtain CSRF token.";
325 0 return -1;
326 }
327  
328 0 my $h = HTTP::Headers->new;
329 0 $h->header(
330 'content-type' => 'application/json;charset=UTF-8',
331 'x-csrf-token' => $self->csrf_token,
332 );
333 0 $self->ua->default_headers( $h );
334 0 my $payload = {
335 username => $self->username,
336 password => $self->password
337 };
338 0 my $data = $json->encode($payload);
339 0 $response = $self->ua->post(
340 $self->app_url . '?__formid__=login',
341 Content => $data
342 );
343 0 my $url = URI->new( "${\$self->api_url}/token" );
344 0 $url->query_form(assertion => $self->csrf_token);
345 0 $response = $self->ua->get( $url );
346 0 $self->_set_token($response->content);
347  
348 0 return 0;
349 }
350  
351  
352 =head2 read_id($id)
353  
354 Interface to GET /api/annotations/<id>
355  
356 Returns the annotation for a given annotation id if id is defined or
357 nonempty. Otherwise (in an effort to remain well-typed) returns the
358 first annotation on the list returned from hypothes.is. At the time of
359 this writing, this functionality of empty 'search' and 'read' requests
360 are identical in the HTTP API, but in this Perl API, 'read'
361 returns a scalar value and 'search' returns an array.
362  
363 =cut
364  
365 0 ▶sub read_id {
Subroutine read_id: not called CC=7 SCAR=40.3
Pod documented
366 0 my ($self, $id) = @_;
367 0 ▶ if (not defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 0 0
368 0 $id = q();
369 }
370 0 my $url = URI->new( "${\$self->api_url}/annotations/$id" );
371 0 my $response = $self->ua->get( $url );
372 0 my $json_content = 0;
373 0 ▶ if ($response->code != 500) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code != 500) { } 0 0 0
374 0 $json_content = try_json_decode($response);
375 0 ▶ if (not $json_content) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json_content) 0 0 0
376 0 die "Was unable to decode JSON content for read_id, id: $id"
377 }
378 } else {
379 0 die "Received status code ${\$response->code} from Hypothes.is in read_id.";
380 }
381 0 my $content_type = ref($json_content);
382 0 ▶ if ($content_type eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type eq 'HASH') { } 0 0 0
383 0 ▶ if (defined $json_content->{'id'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $json_content->{'id'}) { } 0 0 0
elsif (defined $json_content->{'rows'}) { } 0 0 0
384 0 return $json_content;
385 } elsif (defined $json_content->{'rows'}) {
386 0 return $json_content->{'rows'}->[0];
387 } else {
388 0 die "Don't know how to find the annotation.";
389 }
390 } else {
391 0 die "Got $content_type; expected a HASH.";
392 }
393 }
394  
395  
396  
397 =head2 search(\%query, $page_size)
398  
399 Generalized interface to GET /api/search
400  
401 Generalized query function.
402  
403 query is a hash ref with the following optional keys
404 as defined in the hypothes.is HTTP API:
405 * limit
406 * offset
407 * uri
408 * uri.parts
409 * text
410 * quote
411 * user
412  
413 page_size is an additional parameter related to $query->limit
414 and $query->offset, which specifies the number of annotations
415 to fetch at a time, but does not override the spirit of either
416 of the $query parameters.
417  
418 Tries not to return annotations created after initiation
419 of the search.
420  
421 Note that while this function has been made robust to addition of
422 new annotations being created during a query, it is not yet
423 robust to deletion of annotations.
424  
425 =cut
426  
427 # FIXME: improve handling of deletions
428  
429 0 ▶sub search {
Subroutine search: not called CC=5 SCAR=34.0
Pod documented
430 0 my ($self, $query, $page_size) = @_;
431  
432 0 my $h = HTTP::Headers->new;
433 0 $h->header(
434 'content-type' => 'application/json;charset=UTF-8',
435 'x-csrf-token' => $self->csrf_token,
436 );
437 0 ▶ if (not defined $query) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $query) 0 0 0
438 0 $query = {};
439 }
440 0 ▶ if ( defined $query->{ 'uri' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query->{"uri"}) 0 0 0
441 $query->{ 'uri' } = $self->uri_encoder->encode(
442 0 $query->{ 'uri' }
443 );
444 }
445 0 ▶ if (not defined $page_size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $page_size) 0 0 0
446 #Default at the time, but need to make explicit here:
447 0 $page_size = $page_size_default;
448 }
449 0 ▶ if ( not defined $query->{ 'limit' } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $query->{"limit"}) 0 0 0
450 #Default at the time, but need to make explicit here:
451 0 $query->{ 'limit' } = $page_size;
452 }
453  
454 0 my $done = 0;
455 0 my $last_id = undef;
456 0 my $num_returned = 0;
457 0 my $limit_orig = $query->{ 'limit' };
458 0 $query->{ 'limit' } = $page_size + 1;
459  
460 0 my @annotation_buff = ();
461 0 ▶ return sub {
Subroutine __ANON__: not called CC=23 SCAR=63.1
462 0 ▶ $done = 1 if (defined $limit_orig and $num_returned >= $limit_orig);
Branch: 1 branch
BranchTrueFalseTotal
if defined $limit_orig and $num_returned >= $limit_orig 0 0 0
Condition0%0/3
defined $limit_orig and $num_returned >= $limit_orig
!ll&&!rl&&r
000
MC/DC0%0/2
defined $limit_orig and $num_returned >= $limit_orig
defined $limit_orig $num_returned >= $limit_orig
Truth table0%0/3
A and B
A= defined $limit_origB= $num_returned >= $limit_orig
ABresult
0X0
100
111
463 0 ▶ QUERY: if (@annotation_buff == 0 && not $done) {
Branch: 1 branch
BranchTrueFalseTotal
if (@annotation_buff == 0 and not $done) 0 0 0
Condition0%0/3
@annotation_buff == 0 and not $done
!ll&&!rl&&r
000
MC/DC0%0/2
@annotation_buff == 0 and not $done
@annotation_buff == 0 not $done
Truth table0%0/3
A and not(B)
A= @annotation_buff == 0B= not $done
ABresult
0X0
100
111
464 0 ▶ warn "fetching annotations from server.\n" if $VERB > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $VERB > 0 0 0 0
465 #Need to refill response buffer
466 0 my $url = URI->new( "${\$self->api_url}/search" );
467 0 $url->query_form($query);
468 0 ▶ warn $url, "\n" if $VERB > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $VERB > 1 0 0 0
469 0 my $response = $self->ua->get( $url );
470 0 my $json_content = 0;
471 0 ▶ if ($response->code != 500) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code != 500) { } 0 0 0
472 0 $json_content = try_json_decode($response);
473 0 ▶ if (not $json_content) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json_content) 0 0 0
474 0 die "Was unable to decode JSON content in search.";
475 }
476 } else {
477 0 die "Received status code ${\$response->code} from Hypothes.is in search.";
478 }
479 0 @annotation_buff = @{$json_content->{ 'rows' }};
480 0 ▶ if (defined $limit_orig and $limit_orig eq 'Infinity') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $limit_orig and $limit_orig eq "Infinity") 0 0 0
Condition0%0/3
defined $limit_orig and $limit_orig eq "Infinity"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $limit_orig and $limit_orig eq "Infinity"
defined $limit_orig $limit_orig eq "Infinity"
Truth table0%0/3
A and B
A= defined $limit_origB= $limit_orig eq "Infinity"
ABresult
0X0
100
111
481 # OK, we get the point, but let's get finite.
482 0 $limit_orig = $json_content->{ 'total' };
483 0 $query->{ 'limit' } = $json_content->{ 'total' };
484 }
485 0 ▶ if (not defined $limit_orig or $json_content->{ 'total' } < $limit_orig) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $limit_orig or $json_content->{"total"} < $limit_orig) 0 0 0
Condition0%0/3
not defined $limit_orig or $json_content->{"total"} < $limit_orig
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $limit_orig or $json_content->{"total"} < $limit_orig
not defined $limit_orig $json_content->{"total"} < $limit_orig
Truth table0%0/3
not(A) or B
A= not defined $limit_origB= $json_content->{"total"} < $limit_orig
ABresult
1X1
011
000
486 # No limit set or more than total. Set it to the total
487 # so we don't have to try an extra request past the
488 # total number of results
489 0 $limit_orig = $json_content->{ 'total' };
490 0 ▶ warn "setting limit_orig=$limit_orig based on total\n" if $VERB > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $VERB > 1 0 0 0
491 }
492 0 ▶ if (defined $last_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last_id) 0 0 0
493 # This assumes that the feed is like a stack: LIFO.
494 # Annotations created after the search call
495 # shouldn't be returned.
496 #
497 # This is not the first query because $last_id is set and the
498 # offset arranges so that, without the addition of new
499 # annotations, the first result from the new query will be
500 # the same as the last result of the old query. If it isn't
501 # then we assume that new items have been added to the beginning
502 # and scan forward to find the id. The may be more than one
503 # page of scanning.
504 0 ▶ while (@annotation_buff and $last_id ne $annotation_buff[0]->{'id'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@annotation_buff and $last_id ne $annotation_buff[0]{"id"}) 0 0 0
Condition0%0/3
@annotation_buff and $last_id ne $annotation_buff[0]{"id"}
!ll&&!rl&&r
000
MC/DC0%0/2
@annotation_buff and $last_id ne $annotation_buff[0]{"id"}
@annotation_buff $last_id ne $annotation_buff[0]{"id"}
Truth table0%0/3
A and B
A= @annotation_buffB= $last_id ne $annotation_buff[0]{"id"}
ABresult
0X0
100
111
505 0 ▶ warn "mismatch: scanning for last seen id\n" if $VERB > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $VERB > 0 0 0 0
506 0 shift @annotation_buff;
507 0 ▶ if (@annotation_buff == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@annotation_buff == 0) 0 0 0
508 0 $query->{ 'offset' } += $page_size;
509 0 goto QUERY;
510 }
511 }
512 0 ▶ if (@annotation_buff) {
Branch: 1 branch
BranchTrueFalseTotal
if (@annotation_buff) 0 0 0
513 0 shift @annotation_buff;
514 }
515 }
516 0 $query->{ 'offset' } += $page_size;
517 0 ▶ warn $response->content if $VERB > 5;
Branch: 1 branch
BranchTrueFalseTotal
if $VERB > 5 0 0 0
518 }
519 0 ▶ return undef if ($done or @annotation_buff == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $done or @annotation_buff == 0 0 0 0
Condition0%0/3
$done or @annotation_buff == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$done or @annotation_buff == 0
$done @annotation_buff == 0
Truth table0%0/3
A or B
A= $doneB= @annotation_buff == 0
ABresult
1X1
011
000
520 0 my $anno = shift @annotation_buff;
521 0 $last_id = $anno->{'id'};
522 0 $num_returned++;
523 0 return $anno;
524 }
525  
526 0 }
527  
528 =head2 search_total(\%query, $page_size)
529  
530 Specific interface to GET /api/search that simply
531 returns the total number of query results. See
532 the search subroutine for more details on parameters.
533  
534 =cut
535  
536 0 ▶sub search_total {
Subroutine search_total: not called CC=8 SCAR=42.8
Pod documented
537  
538 # Note: try to keep the logic here the same as in the search
539 # function, or possibly remove code duplication.
540 #
541 # Start of code duplication:
542 #
543 0 my ($self, $query, $page_size) = @_;
544  
545 0 my $h = HTTP::Headers->new;
546 0 $h->header(
547 'content-type' => 'application/json;charset=UTF-8',
548 'x-csrf-token' => $self->csrf_token,
549 );
550 0 ▶ if (not defined $query) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $query) 0 0 0
551 0 $query = {};
552 }
553 0 ▶ if ( defined $query->{ 'uri' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query->{"uri"}) 0 0 0
554 $query->{ 'uri' } = $self->uri_encoder->encode(
555 0 $query->{ 'uri' }
556 );
557 }
558 0 ▶ if (not defined $page_size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $page_size) 0 0 0
559 #Default at the time, but need to make explicit here:
560 0 $page_size = 20;
561 }
562 0 ▶ if ( not defined $query->{ 'limit' } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $query->{"limit"}) 0 0 0
563 #Default at the time, but need to make explicit here:
564 0 $query->{ 'limit' } = $page_size;
565 }
566  
567 0 my $done = 0;
568 0 my $last_id = undef;
569 0 my $num_returned = 0;
570 0 my $limit_orig = $query->{ 'limit' };
571 0 $query->{ 'limit' } = $page_size + 1;
572 #
573 # End of code duplication:
574 #
575  
576 0 my $url = URI->new( "${\$self->api_url}/search" );
577 0 $url->query_form($query);
578 0 ▶ warn $url, "\n" if $VERB > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $VERB > 1 0 0 0
579 0 my $response = $self->ua->get( $url );
580 0 my $json_content = 0;
581 0 ▶ if ($response->code != 500) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code != 500) { } 0 0 0
582 0 $json_content = try_json_decode($response);
583 0 ▶ if (not $json_content) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json_content) 0 0 0
584 0 die "Was unable to decode JSON content in search_total.";
585 }
586 } else {
587 0 die "Received status code ${\$response->code} from Hypothes.is in search_total.";
588 }
589 0 return $json_content->{ 'total' };
590 }
591  
592  
593 =head2 update_id($id, \%payload)
594  
595 Interface to PUT /api/annotations/<id>
596  
597 Updates the annotation for a given annotation id if id is defined and
598 the user is authenticated and has update permissions. Takes a payload
599 as described for 'search'. Only fields specified in the new payload
600 are altered; other existing fields should remain unchanged.
601  
602 Returns a boolean value indicating whether or not the annotation for
603 that id has been successfully updated (1 = yes, 0 = no).
604  
605 =cut
606  
607 0 ▶sub update_id {
Subroutine update_id: not called CC=8 SCAR=42.8
Pod documented
608 0 my ($self, $id, $payload) = @_;
609 0 ▶ if (not defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 0 0
610 0 die "Can only call update if given an id.";
611 }
612 0 my $data = $json->encode($payload);
613 0 my $h = HTTP::Headers->new;
614 0 $h->header(
615 'content-type' => 'application/json;charset=UTF-8',
616 'x-csrf-token' => $self->csrf_token,
617 'X-Annotator-Auth-Token' => $self->token,
618 );
619 0 $self->ua->default_headers( $h );
620 0 my $url = URI->new( "${\$self->api_url}/annotations/$id" );
621 0 my $response = $self->ua->put( $url, Content => $data );
622 0 my $json_content = 0;
623 0 ▶ if ($response->code != 500) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code != 500) { } 0 0 0
624 0 $json_content = try_json_decode($response);
625 0 ▶ if (not $json_content) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json_content) 0 0 0
626 0 die "Was unable to decode JSON content for update_id, id: $id";
627 }
628 } else {
629 0 die "Received status code ${\$response->code} from Hypothes.is in update_id.";
630 }
631 0 my $content_type = ref($json_content);
632 0 ▶ if ($content_type eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type eq 'HASH') { } 0 0 0
633 0 ▶ if (defined $json_content->{'updated'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json_content->{'updated'}) { } 0 0 0
634 0 ▶ if ($json_content->{'updated'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($json_content->{'updated'}) { } 0 0 0
elsif (not $json_content->{'updated'}) { } 0 0 0
635 0 return 1;
636 } elsif (not $json_content->{'updated'}) {
637 0 return 0;
638 } else { # Never reached in current implementation
639 0 warn "unexpected update status: ${\$json_content->{'updated'}}";
640 0 return 0;
641 }
642 } else {
643 0 die "Received unexpected object: no 'updated' entry present.";
644 }
645 } else {
646 0 die "Got $content_type; expected an ARRAY or HASH.";
647 }
648 }
649  
650  
651 =head1 EXTERNAL ACCESSORS
652  
653 =head2 get_ua_timeout($timeout)
654  
655 Gets the timeout (in seconds) of the internal LWP::UserAgent object used to
656 make requests to the Hypothes.is service.
657  
658 =cut
659  
660 0 ▶sub get_ua_timeout {
Subroutine get_ua_timeout: not called CC=1 SCAR=6.9
Pod documented
661 0 my ($self) = @_;
662 0 return $self->ua->timeout;
663 }
664  
665 =head2 set_ua_timeout($timeout)
666  
667 Under certain circumstances, particularly for testing, it is helpful to set
668 the timeout (in seconds) used by LWP::UserAgent to make requests to the
669 Hypothes.is service.
670  
671 =cut
672  
673 0 ▶sub set_ua_timeout {
Subroutine set_ua_timeout: not called CC=1 SCAR=6.9
Pod documented
674 0 my ($self, $timeout) = @_;
675 0 $self->ua->timeout( $timeout );
676 0 return;
677 }
678  
679  
680 0 ▶sub try_json_decode {
Subroutine try_json_decode: not called CC=1 SCAR=6.9
Pod undocumented
681 0 my ($response) = @_;
682 0 my $json_content = 0;
683 0 ▶ try{
Subroutine __ANON__: not called CC=1 SCAR=6.9
684 0 $json_content = $json->decode($response->content);
685 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
686 0 warn "Trouble decoding JSON: $_\n";
687 0 warn $response->content;
688 0 };
689 0 return $json_content;
690 }
691  
692  
693  
694 =head1 AUTHOR
695  
696 Brandon E. Barker, C<< <brandon.barker at cornell.edu> >>
697  
698 Created 06/2015
699  
700 Licensed under the Apache License, Version 2.0 (the "Apache License");
701 also licensed under the Artistic License 2.0 (the "Artistic License").
702 you may not use this file except in compliance with one of
703 these two licenses. You may obtain a copy of the Apache License at
704  
705 http://www.apache.org/licenses/LICENSE-2.0
706  
707 Alternatively a copy of the Apache License should be available in the
708 LICENSE-2.0.txt file found in this source code repository.
709  
710 You may obtain a copy of the Artistic License at
711  
712 http://www.perlfoundation.org/artistic_license_2_0
713  
714 Alternatively a copy of the Artistic License should be available in the
715 artistic-2_0.txt file found in this source code repository.
716  
717 Unless required by applicable law or agreed to in writing, software
718 distributed under the License is distributed on an "AS IS" BASIS,
719 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
720 See the Apache License or Artistic License for the specific language
721 governing permissions and limitations under the licenses.
722  
723 =head1 BUGS
724  
725 Please report any bugs or feature requests at L<https://github.com/bbarker/Hypothesis-API/issues>.
726 Alternatively, you may send them to C<bug-hypothesis-api at rt.cpan.org>, or through
727 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Hypothesis-API>, but this
728 is not preferred. In either case, I will be notified, and then you'll
729 automatically be notified of progress on your bug as I make changes.
730  
731 =head1 REPOSITORY
732  
733 L<https://github.com/bbarker/Hypothesis-API>
734  
735  
736 =head1 SUPPORT
737  
738 You can find documentation for this module with the perldoc command.
739  
740 perldoc Hypothesis::API
741  
742 You can also look for information at:
743  
744 =over 4
745  
746 =item * RT: CPAN's request tracker (report bugs here)
747  
748 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Hypothesis-API>
749  
750 =item * AnnoCPAN: Annotated CPAN documentation
751  
752 L<http://annocpan.org/dist/Hypothesis-API>
753  
754 =item * CPAN Ratings
755  
756 L<http://cpanratings.perl.org/d/Hypothesis-API>
757  
758 =item * Search CPAN
759  
760 L<http://search.cpan.org/dist/Hypothesis-API/>
761  
762 =back
763  
764  
765 =head1 ACKNOWLEDGEMENTS
766  
767 We are thankful for support from the Alfred P. Sloan Foundation.
768  
769 =cut
770  
771 1 1; # End of Hypothesis::API
↑ Summary