Skip to content

Instantly share code, notes, and snippets.

@LincolnUniLTL
Last active October 27, 2017 15:23
Show Gist options
  • Save LincolnUniLTL/78ea0ff25ebc729afb5d to your computer and use it in GitHub Desktop.
Save LincolnUniLTL/78ea0ff25ebc729afb5d to your computer and use it in GitHub Desktop.
Get or set Symplectic Elements publication item metadata using the Elements API
#! /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