Last active
October 27, 2017 15:23
-
-
Save LincolnUniLTL/78ea0ff25ebc729afb5d to your computer and use it in GitHub Desktop.
Get or set Symplectic Elements publication item metadata using the Elements API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/perl | |
# ********************************************************* | |
# Invoke the Symplectic Elements API with a get or set call | |
# to a publication item | |
# (/{cat}/records/{source}/{proprietary-id} API method) | |
# using either HTTP GET or PUT (set) depending on arguments. | |
# Usage: | |
# - get/dump : pubitem.pl $id | |
# - set/amend: pubitem.pl $id $xmlfile | |
# There's also some embedded sample XML in the file you can | |
# mess around with if that's easier for experimenting. | |
# Replace the $xmlfile argument with '-' for that option. | |
# | |
# Caveat: The record source is hardcoded 'Manual', should | |
# possibly be overrideable. #FIXME | |
use strict; | |
require HTTP::Request; | |
require HTTP::Headers; | |
use LWP::UserAgent; | |
use HTTP::Status qw(:constants :is status_message); | |
my $item = shift or die 'Usage: pubitem.pl id [xmlfile]'; | |
my $source = 'Manual'; | |
my $hostname = 'elements.example.edu'; | |
my $endpoint = "https://$hostname/elements-api/"; | |
my $resource = "publication/records/$source/$item"; | |
my $username = '*USERNAME*'; | |
my $password = '********'; | |
my $test_payload = <<EOP | |
<?xml version="1.0" encoding="utf-8"?> | |
<import-record type-id="18" xmlns="http://www.symplectic.co.uk/publications/api"> | |
<native> | |
<field name="keywords"> | |
<keywords> | |
<keyword>foo</keyword> | |
<keyword>bar</keyword> | |
</keywords> | |
</field> | |
</native> | |
</import-record> | |
EOP | |
; | |
my $payload; | |
if (my $xmlsource = shift) { | |
if ($xmlsource eq '-') { | |
$payload = $test_payload; | |
} | |
else { | |
open( my $fh, '<', $xmlsource) | |
or die "Unable to open $xmlsource for reading: $!"; | |
$payload = join('', @_ = <$fh>); | |
} | |
}; | |
my $url = $endpoint . lc($resource); | |
my $ua = LWP::UserAgent->new(); | |
my $request; | |
if ($payload) { | |
$request = HTTP::Request->new(PUT => $url); | |
$request->header('Content-Type' => 'text/xml'); | |
$request->content($payload); | |
} | |
else { | |
$request = HTTP::Request->new(GET => $url); | |
} | |
$request->authorization_basic($username, $password); | |
my $response = $ua->request($request); | |
print $response->status_line, "\n"; | |
print $response->content; | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment