Hypothesis-API

 view release on metacpan or  search on metacpan

lib/Hypothesis/API.pm  view on Meta::CPAN

        'content-type' => 'application/json;charset=UTF-8', 
        'x-csrf-token' => $self->csrf_token,
    );
    if (not defined $query) {
        $query = {};
    }
    if ( defined $query->{ 'uri' } ) {
        $query->{ 'uri' } = $self->uri_encoder->encode(
           $query->{ 'uri' }
        );
    }
    if (not defined $page_size) {
        #Default at the time, but need to make explicit here:
        $page_size = 20;
    }
    if ( not defined $query->{ 'limit' } ) {
        #Default at the time, but need to make explicit here:
        $query->{ 'limit' } = $page_size;
    }

    my $done = 0;
    my $last_id = undef;
    my $num_returned = 0;
    my $limit_orig = $query->{ 'limit' };
    $query->{ 'limit' } = $page_size + 1;
    #
    # End of code duplication:
    #

    my $url = URI->new( "${\$self->api_url}/search" );
    $url->query_form($query);
    warn $url, "\n" if $VERB > 1;
    my $response = $self->ua->get( $url );
    my $json_content = 0;
    if ($response->code != 500) {
        $json_content = try_json_decode($response);
        if (not $json_content) {
            die "Was unable to decode JSON content in search_total.";
        }
    } else {
        die "Received status code ${\$response->code} from Hypothes.is in search_total.";
    }
    return $json_content->{ 'total' };
}


=head2 update_id($id, \%payload)

Interface to PUT /api/annotations/<id>

Updates the annotation for a given annotation id if id is defined and
the user is authenticated and has update permissions. Takes a payload
as described for 'search'. Only fields specified in the new payload
are altered; other existing fields should remain unchanged.

Returns a boolean value indicating whether or not the annotation for
that id has been successfully updated (1 = yes, 0 = no).

=cut

sub update_id {
    my ($self, $id, $payload) = @_;
    if (not defined $id) {
        die "Can only call update if given an id.";
    }
    my $data = $json->encode($payload);
    my $h = HTTP::Headers->new;
    $h->header(
        'content-type' => 'application/json;charset=UTF-8', 
        'x-csrf-token' => $self->csrf_token,
        'X-Annotator-Auth-Token' => $self->token, 
    );
    $self->ua->default_headers( $h );
    my $url = URI->new( "${\$self->api_url}/annotations/$id" );
    my $response = $self->ua->put( $url, Content => $data );
    my $json_content = 0;
    if ($response->code != 500) {
        $json_content = try_json_decode($response);
        if (not $json_content) {
            die "Was unable to decode JSON content for update_id, id: $id";
        }
    } else {
        die "Received status code ${\$response->code} from Hypothes.is in update_id.";
    }
    my $content_type = ref($json_content);
    if ($content_type eq "HASH") {
        if (defined $json_content->{'updated'}) {
            if ($json_content->{'updated'}) {
                return 1;
            } elsif (not $json_content->{'updated'}) {
                return 0;
            } else { # Never reached in current implementation
                warn "unexpected update status: ${\$json_content->{'updated'}}";
                return 0;
            }
        } else {
            die "Received unexpected object: no 'updated' entry present.";
        }
    } else {
        die "Got $content_type; expected an ARRAY or HASH.";
    }
}


=head1 EXTERNAL ACCESSORS

=head2 get_ua_timeout($timeout)

Gets the timeout (in seconds) of the internal LWP::UserAgent object used to
make requests to the Hypothes.is service.

=cut

sub get_ua_timeout {
    my ($self) = @_;
    return $self->ua->timeout;
}

=head2 set_ua_timeout($timeout)

Under certain circumstances, particularly for testing, it is helpful to set



( run in 1.325 second using v1.01-cache-2.11-cpan-140bd7fdf52 )