RDF/Trine/Store/Dydra.pm untested

filter: Statementstmt 0.0% 0 / 237 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 382 CC 28 SCAR 31.3
CC
28
Cov
0%
add_statement
56.0
remove_statement
56.0
_create_repo
42.0
CRAP 23.0
1 =head1 NAME
2  
3 RDF::Trine::Store::Dydra - RDF Store proxy for a Dydra endpoint
4  
5 =head1 VERSION
6  
7 This document describes RDF::Trine::Store::Dydra version 1.019
8  
9 =head1 SYNOPSIS
10  
11 use RDF::Trine::Store::Dydra;
12  
13 =head1 DESCRIPTION
14  
15 RDF::Trine::Store::Dydra provides a RDF::Trine::Store API to interact with a
16 remote Dydra endpoint.
17  
18 =cut
19  
20 package RDF::Trine::Store::Dydra;
21  
22 use strict;
23 use warnings;
24 no warnings 'redefine';
25 use base qw(RDF::Trine::Store::SPARQL);
26  
27 use URI::Escape;
28 use Data::Dumper;
29 use List::Util qw(first);
30 use Scalar::Util qw(refaddr reftype blessed);
31 use HTTP::Request::Common ();
32 use JSON;
33  
34 use RDF::Trine::Error qw(:try);
35  
36 ######################################################################
37  
38 my @pos_names;
39 our $VERSION;
40 BEGIN {
41 $VERSION = "1.019";
42 my $class = __PACKAGE__;
43 $RDF::Trine::Store::STORE_CLASSES{ $class } = $VERSION;
44 @pos_names = qw(subject predicate object context);
45 }
46  
47 ######################################################################
48  
49 =head1 METHODS
50  
51 Beyond the methods documented below, this class inherits methods from the
52 L<RDF::Trine::Store> class.
53  
54 =over 4
55  
56 =item C<< new ( $user, $repo, $token ) >>
57  
58 Returns a new storage object that will act as a proxy for the Dydra endpoint
59 for the $repo repository of $user, using the given API $token.
60  
61 =item C<new_with_config ( $hashref )>
62  
63 Returns a new storage object configured with a hashref with certain
64 keys as arguments.
65  
66 The C<storetype> key must be C<Dydra> for this backend.
67  
68 The following key must also be used:
69  
70 =over
71  
72 =item C<user>
73  
74 The Dydra username.
75  
76 =item C<token>
77  
78 The Dydra API token.
79  
80 =item C<repo>
81  
82 The Dydra repository name.
83  
84 =back
85  
86 =cut
87  
88 sub new {
89 my $class = shift;
90 my $user = shift;
91 my $repo = shift;
92 my $token = shift;
93 my $ua = RDF::Trine->default_useragent->clone;
94 my $accept = join(',',
95 "application/sparql-results+json",
96 "application/rdf+xml;q=0.5",
97 "application/x-turtle;q=0.7",
98 "application/json;q=0.4",
99 "text/turtle;q=0.7",
100 "text/xml;q=0.1"
101 );
102 $ua->default_headers->push_header( 'Accept' => $accept );
103 $ua->credentials("dydra.com:80", "Application", $token, "X");
104 $ua->credentials("dydra.com", "Application", $token, "X");
105
106 my $base = "http://dydra.com:80";
107 my $url = "${base}/${user}/${repo}/sparql";
108 my $self = bless({
109 ua => $ua,
110 base => $base,
111 url => $url,
112 user => $user,
113 repo => $repo,
114 token => $token,
115 }, $class);
116
117 # check if this repo already exists, otherwise create it
118 $self->_create_repo( $user, $repo );
119
120 return $self;
121 }
122  
123 =item C<< base >>
124  
125 Returns the service base URI ("http://dydra.com:80" by default).
126  
127 =cut
128  
129 sub base {
130 my $self = shift;
131 return $self->{base};
132 }
133  
134 sub _create_repo {
135 my $self = shift;
136 my $user = shift;
137 my $repo = shift;
138 my $ua = $self->{ua};
139 my $base = $self->base;
140 my $repourl = "${base}/${user}/repositories";
141 my $req = HTTP::Request->new(GET => $repourl);
142 $req->header(Accept => "application/json");
143 $req->header(Host => "dydra.com");
144 $req->authorization_basic($self->{token}) if (defined $self->{token});
145 my $resp = $ua->request( $req );
146 if ($resp->is_success) {
147 my $data = from_json($resp->content);
148 my %repos;
149 foreach my $r (@$data) {
150 $repos{ $r->{name} } = $r;
151 }
152 unless ($repos{ $repo }) {
153 my $req = HTTP::Request::Common::POST( $repourl, [
154 'repository[name]' => $repo,
155 ] );
156 $req->authorization_basic($self->{token}) if (defined $self->{token});
157 $req->header(Accept => "application/json");
158 my $resp = $ua->request( $req );
159 if ($resp->is_success) {
160 # OK
161 } else {
162 my $status = $resp->status_line;
163 warn Dumper($resp);
164 throw RDF::Trine::Error::DatabaseError -text => "Error making remote REST call attempting to create repository '$repo' ($status)";
165 }
166 }
167 } else {
168 my $status = $resp->status_line;
169 warn Dumper($resp);
170 throw RDF::Trine::Error::DatabaseError -text => "Error making remote REST call in new ($status)";
171 }
172 }
173  
174 sub _new_with_string {
175 my $class = shift;
176 my $config = shift;
177 return $class->new( $config );
178 }
179  
180 =item C<< new_with_config ( \%config ) >>
181  
182 Returns a new RDF::Trine::Store object based on the supplied configuration hashref.
183  
184 =cut
185  
186 sub new_with_config {
187 my $proto = shift;
188 my $config = shift;
189 $config->{storetype} = 'Dydra';
190 return $proto->SUPER::new_with_config( $config );
191 }
192  
193 sub _new_with_config {
194 my $class = shift;
195 my $config = shift;
196 return $class->new( @{ $config }{qw(user repo token)} );
197 }
198  
199 sub _config_meta {
200 return {
201 required_keys => [qw(user repo token)],
202 fields => {
203 user => { description => 'Dydra username', type => 'string' },
204 repo => { description => 'Dydra repository name', type => 'string' },
205 token => { description => 'Dydra API token', type => 'string' },
206 }
207 }
208 }
209  
210  
211  
212 =item C<< add_statement ( $statement [, $context] ) >>
213  
214 Adds the specified C<$statement> to the underlying model.
215  
216 =cut
217  
218 sub add_statement {
219 my $self = shift;
220 my $st = shift;
221 my $context = shift;
222 unless (blessed($st) and $st->isa('RDF::Trine::Statement')) {
223 throw RDF::Trine::Error::MethodInvocationError -text => "Not a valid statement object passed to add_statement";
224 }
225
226 if ($st->isa('RDF::Trine::Statement::Quad') and blessed($context)) {
227 throw RDF::Trine::Error::MethodInvocationError -text => "add_statement cannot be called with both a quad and a context";
228 }
229
230 if ($self->_bulk_ops) {
231 push(@{ $self->{ ops } }, ['_add_statements', $st, $context]);
232 } else {
233 my $s = RDF::Trine::Serializer::NTriples->new();
234 my $ua = $self->{ua};
235 my $user = $self->{user};
236 my $repo = $self->{repo};
237 my $base = $self->base;
238 my $url = "${base}/${user}/${repo}/statements";
239 if ($st->isa('RDF::Trine::Statement::Quad') or $context) {
240 my $g = $context || $st->context;
241 $url .= '?context=' . uri_escape($g->uri_value);
242 }
243 my $req = HTTP::Request->new(POST => $url);
244 $req->authorization_basic($self->{token}) if (defined $self->{token});
245 $req->content_type('text/plain');
246 $req->content($s->statement_as_string($st));
247
248 warn "add_statement request: " . Dumper($req);
249
250 my $resp = $ua->request( $req );
251 if ($resp->is_success) {
252 return;
253 } else {
254 my $status = $resp->status_line;
255 warn Dumper($resp);
256 throw RDF::Trine::Error::DatabaseError -text => "Error making remote REST call in add_statement ($status)";
257 }
258 }
259 return;
260 }
261  
262 =item C<< remove_statement ( $statement [, $context]) >>
263  
264 Removes the specified C<$statement> from the underlying model.
265  
266 =cut
267  
268 sub remove_statement {
269 my $self = shift;
270 my $st = shift;
271 my $context = shift;
272
273 unless (blessed($st) and $st->isa('RDF::Trine::Statement')) {
274 throw RDF::Trine::Error::MethodInvocationError -text => "Not a valid statement object passed to remove_statement";
275 }
276
277 if ($st->isa('RDF::Trine::Statement::Quad') and blessed($context)) {
278 throw RDF::Trine::Error::MethodInvocationError -text => "remove_statement cannot be called with both a quad and a context";
279 }
280
281 if ($self->_bulk_ops) {
282 push(@{ $self->{ ops } }, ['_remove_statements', $st, $context]);
283 } else {
284 my $s = RDF::Trine::Serializer::NTriples->new();
285 my $ua = $self->{ua};
286 my $user = $self->{user};
287 my $repo = $self->{repo};
288 my $base = $self->base;
289 my $data = $s->statement_as_string($st);
290 if ($st->isa('RDF::Trine::Statement::Quad') or $context) {
291 my $g = $context || $st->context;
292 my $uri = $g->uri_value;
293 $data = "GRAPH <$uri> { $data }";
294 }
295 my $sparql = "DELETE DATA { $data }";
296 my $url = "${base}/${user}/${repo}/sparql";
297 my $req = HTTP::Request::Common::POST( $url, [ query => $sparql ] );
298 $req->authorization_basic($self->{token}) if (defined $self->{token});
299 my $resp = $ua->request( $req );
300 warn 'remove_statement: ' . Dumper($req, $resp);
301
302 if ($resp->is_success) {
303 return;
304 } else {
305 my $status = $resp->status_line;
306 warn Dumper($resp);
307 throw RDF::Trine::Error::DatabaseError -text => "Error making remote REST call in remove_statement ($status)" . Dumper($st);
308 }
309 }
310 return;
311 }
312  
313 =item C<< remove_statements ( $subject, $predicate, $object [, $context]) >>
314  
315 Removes the specified C<$statement> from the underlying model.
316  
317 =cut
318  
319 sub remove_statements {
320 my $self = shift;
321 my $st = shift;
322 my $context = shift;
323
324 unless (blessed($st) and $st->isa('RDF::Trine::Statement')) {
325 throw RDF::Trine::Error::MethodInvocationError -text => "Not a valid statement object passed to remove_statements";
326 }
327
328 if ($self->_bulk_ops) {
329 push(@{ $self->{ ops } }, ['_remove_statement_patterns', $st, $context]);
330 } else {
331 warn "do DELETE for remove_statements";
332 throw RDF::Trine::Error::UnimplementedError -text => "remove_statements not implemented for Dydra stores yet";
333 }
334 return;
335 }
336  
337 =item C<< count_statements ( $subject, $predicate, $object, $context ) >>
338  
339 Returns a count of all the statements matching the specified subject,
340 predicate, object, and context. Any of the arguments may be undef to match any
341 value.
342  
343 =cut
344  
345 =item C<< supports ( [ $feature ] ) >>
346  
347 If C<< $feature >> is specified, returns true if the feature is supported by the
348 store, false otherwise. If C<< $feature >> is not specified, returns a list of
349 supported features.
350  
351 =cut
352  
353 sub supports {
354 my $self = shift;
355 my %features = map { $_ => 1 } (
356 'http://www.w3.org/ns/sparql-service-description#SPARQL10Query',
357 'http://www.w3.org/ns/sparql-service-description#SPARQL11Query',
358 );
359 if (@_) {
360 my $f = shift;
361 return $features{ $f };
362 } else {
363 return keys %features;
364 }
365 }
366  
367 =item C<< get_sparql ( $sparql ) >>
368  
369 Returns an iterator object of all bindings matching the specified SPARQL query.
370  
371 =cut
372  
373 sub get_sparql {
374 my $self = shift;
375 my $sparql = shift;
376 my $ua = $self->{ua};
377
378 my $urlchar = ($self->{url} =~ /\?/ ? '&' : '?');
379 my $url = $self->{url} . $urlchar . 'query=' . uri_escape($sparql);
380 my $response = $ua->get( $url );
381 if ($response->is_success) {
382 my $content = $response->content;
383 my $iter = RDF::Trine::Iterator->from_json( $content );
384 return $iter;
385 } else {
386 my $status = $response->status_line;
387 my $endpoint = $self->{url};
388 warn Dumper($response);
389 throw RDF::Trine::Error::DatabaseError -text => "Error making remote SPARQL call to endpoint $endpoint ($status)";
390 }
391 }
392  
393 =item C<< size >>
394  
395 Returns the number of statements in the store.
396  
397 =cut
398  
399 sub size {
400 my $self = shift;
401 my $ua = $self->{ua};
402 my $user = $self->{user};
403 my $repo = $self->{repo};
404 my $base = $self->base;
405 my $url = "${base}/${user}/${repo}/size";
406 my $req = HTTP::Request->new(GET => $url);
407 $req->authorization_basic($self->{token}) if (defined $self->{token});
408 $req->header(Accept => 'text/plain');
409 my $resp = $ua->request( $req );
410 if ($resp->is_success) {
411 warn 'size(): ' . Dumper($req, $resp);
412 return 0+$resp->content;
413 } else {
414 my $status = $resp->status_line;
415 warn 'size() failed: ' . Dumper($resp);
416 throw RDF::Trine::Error::DatabaseError -text => "Error making remote REST call in size ($status)";
417 }
418 }
419  
420 sub _end_bulk_ops {
421 my $self = shift;
422 if (scalar(@{ $self->{ ops } || []})) {
423 my @ops = splice(@{ $self->{ ops } });
424 my @aggops = $self->_group_bulk_ops( @ops );
425 my @sparql;
426 warn '_end_bulk_ops: ' . Dumper(\@aggops);
427 throw RDF::Trine::Error::UnimplementedError -text => "bulk operations not implemented for Dydra stores yet";
428 }
429 $self->{BulkOps} = 0;
430 }
431  
432 =item C<< nuke >>
433  
434 Permanently removes the store and its data.
435  
436 =cut
437  
438 sub nuke {
439 my $self = shift;
440 my $ua = $self->{ua};
441 my $user = $self->{user};
442 my $repo = $self->{repo};
443 my $base = $self->base;
444 my $url = "${base}/${user}/${repo}";
445 my $req = HTTP::Request->new(DELETE => $url);
446 $req->authorization_basic($self->{token}) if (defined $self->{token});
447 my $resp = $ua->request( $req );
448 if ($resp->is_success) {
449 return;
450 } else {
451 my $status = $resp->status_line;
452 warn 'nuke failed: ' . Dumper($resp);
453 throw RDF::Trine::Error::DatabaseError -text => "Error making remote REST call in remove_statement ($status)";
454 }
455 }
456  
457  
458 1;
459  
460 __END__