initial REST API for EPrints
authorDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 29 Oct 2013 17:19:37 +0000 (18:19 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 29 Oct 2013 17:19:37 +0000 (18:19 +0100)
eprints-rest.pl [new file with mode: 0755]

diff --git a/eprints-rest.pl b/eprints-rest.pl
new file mode 100755 (executable)
index 0000000..c4a0ba0
--- /dev/null
@@ -0,0 +1,79 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use LWP::Debug qw(+);
+use LWP::UserAgent;
+use Data::Dump qw(dump);
+use utf8;
+
+=begin eprints configuration
+
+add to /usr/share/eprints3/archives/grf/cfg/cfg.d/user_roles.pl
+
+$c->{user_roles}->{admin} = [qw{
+       # ...
+       rest
+       upsert
+       # ...
+}];
+
+=cut
+
+our $repo = "http://eprints.ffzg.hr/grf";
+our $user = 'admin';
+our $passwd = 'XXXX';
+require "config.pl";
+
+warn "# repo: $repo [$user]\n";
+
+my $nr;
+
+my $ua = LWP::UserAgent->new;
+$ua->credentials("eprints.ffzg.hr:80", "EPrintsREST", "admin", "grfadmin");
+
+my @ids;
+my $response = $ua->get( "$repo/rest/eprint/" );
+if ($response->is_success) {
+       my $html = $response->decoded_content;
+#      print $html;
+       @ids = grep { defined $_ } map { m{href=['"](\d+)/} && $1 || undef } split(/[\n\r]/, $html);
+} else {
+       die $response->status_line;
+}
+
+print "ids = ",dump(@ids),$/;
+
+$nr = $ids[-1];
+
+print "nr = $nr\n";
+
+$response = $ua->get( "$repo/rest/eprint/$nr.xml" );
+
+if ($response->is_success) {
+       print "ORIG\n", $response->decoded_content;
+} else {
+       die "ERROR: ", $response->status_line;
+}
+
+
+my $xml = $response->decoded_content;  # or whatever
+my $title = $1 if $xml =~ m{<title>(.+)</title>};
+$title =~ s{\s*\[editirano]}{} or $title .= ' [editirano]';
+my $update = $ua->request( HTTP::Request->new( 'PUT', "$repo/rest/eprint/$nr/title.txt", undef, "$title" ) );
+if ($update->is_success) {
+       print "UPDATE: ", $update->decoded_content;
+} else {
+       die "ERROR: ", $update->status_line;
+}
+
+my $old = $nr;
+$nr++;
+$xml =~ s{(eprint.*)$old}{$1$nr}g;
+
+my $create = $ua->request( HTTP::Request->new( 'PUT', "$repo/rest/eprint/$nr/", [ 'Content-type' => 'text/xml' ], utf8::decode($xml) ) );
+if ($create->is_success) {
+       print "CREATE: ", $create->decoded_content;
+} else {
+       die "ERROR: ", $create->status_line, $create->decoded_content;
+}
+