Attean
view release on metacpan or search on metacpan
lib/Attean/SPARQLClient.pm view on Meta::CPAN
=item C<< query_request( $sparql ) >>
Returns an HTTP::Request object for the given SPARQL query string.
=cut
sub query_request {
my $self = shift;
my $sparql = shift;
my $endpoint = $self->endpoint->value;
my $uri = URI->new($endpoint);
my %params = $uri->query_form;
$params{'query'} = $sparql;
$uri->query_form(%params);
my $url = $uri->as_string;
my $req = HTTP::Request->new('GET', $url);
if (my $signer = $self->request_signer) {
$signer->sign($req);
}
return $req;
}
=item C<< query( $sparql ) >>
Executes the given SPARQL query string at the remote endpoint. If execution is
successful, returns an Attean::API::Iterator object with the results. If
execution fails but the client C<< silent >> flag is true, returns an empty
iterator. Otherwise raises an error via C<< die >>.
=cut
sub query {
my $self = shift;
my $sparql = shift;
my $req = $self->query_request($sparql);
my $silent = $self->silent;
my $ua = $self->user_agent;
my $response = $ua->request($req);
if (blessed($response) and $response->is_success) {
my $type = $response->header('Content-Type');
my $pclass = eval { Attean->get_parser(media_type => $type) or die "No parser for media type: $type" };
if ($@) {
if ($silent) {
my $b = Attean::Result->new( bindings => {} );
return Attean::ListIterator->new(variables => [], values => [$b], item_type => 'Attean::API::Result');
} else {
die $@;
}
}
if (not($pclass)) {
if ($silent) {
my $b = Attean::Result->new( bindings => {} );
return Attean::ListIterator->new(variables => [], values => [$b], item_type => 'Attean::API::Result');
} else {
die "No parser found for type [$type]";
}
}
my $parser = $pclass->new();
my $xml = $response->decoded_content;
my $bytes = encode('UTF-8', $xml, Encode::FB_CROAK);
return $parser->parse_iter_from_bytes($bytes);
} elsif ($silent) {
my $b = Attean::Result->new( bindings => {} );
return Attean::ListIterator->new(variables => [], values => [$b], item_type => 'Attean::API::Result');
} else {
die "SPARQL Protocol error: " . $response->status_line;
}
}
}
1;
__END__
=back
=head1 BUGS
Please report any bugs or feature requests to through the GitHub web interface
at L<https://github.com/kasei/attean/issues>.
=head1 SEE ALSO
L<SPARQL 1.1 Protocol|https://www.w3.org/TR/sparql11-protocol/>
=head1 AUTHOR
Gregory Todd Williams C<< <gwilliams@cpan.org> >>
=head1 COPYRIGHT
Copyright (c) 2014--2022 Gregory Todd Williams.
This program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
( run in 1.531 second using v1.01-cache-2.11-cpan-39bf76dae61 )