Skip to content

Instantly share code, notes, and snippets.

@DZielke
Forked from blokhin/datacite_api_test.py
Last active August 29, 2015 14:13
Show Gist options
  • Save DZielke/1998198b537054ed8dff to your computer and use it in GitHub Desktop.
Save DZielke/1998198b537054ed8dff to your computer and use it in GitHub Desktop.
import os, sys
import httplib2
import base64
endpoint_meta = 'https://test.datacite.org/mds/metadata?testMode=0'
endpoint_mint = 'https://test.datacite.org/mds/doi?testMode=0'
login = '<login here>'
password = '<pass here>'
doi = '10.5072/NOMAD/W4UV5CN78NC347934J9483CD3453'
meta_req = '''<?xml version="1.0" encoding="UTF-8"?>
<resource xsi:schemaLocation="http://datacite.org/schema/kernel-3 http://schema.datacite.org/meta/kernel-3.1/metadata.xsd" xmlns="http://datacite.org/schema/kernel-3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<identifier identifierType="DOI">''' + doi + '''</identifier>
<creators>
<creator>
<creatorName>Lastname, Name</creatorName>
</creator>
</creators>
<titles>
<title>DOI Simple Test</title>
</titles>
<publisher>Yet Another Repository</publisher>
<publicationYear>2015</publicationYear>
</resource>
'''
url = 'http://nomad.physik.hu-berlin.de/gui#W4UV5CN78NC347934J9483CD3453' # NOT EXISTS, ONLY FOR TEST
doi_req = "doi="+doi+"\nurl="+url
h = httplib2.Http()
auth_string = base64.encodestring(login + ':' + password)
# First, save meta
response, content = h.request(endpoint_meta,
'POST',
body = meta_req.encode('utf-8'),
headers={'Content-Type':'application/xml;charset=UTF-8',
'Authorization':'Basic ' + auth_string})
if response.status != 201: print str(response.status)
print content.decode('utf-8')
# Then, save its new ID
response, content = h.request(endpoint_mint,
'POST',
body = doi_req.encode('utf-8'),
headers={'Content-Type':'text/plain;charset=UTF-8',
'Authorization':'Basic ' + auth_string})
if response.status != 201: print str(response.status)
print content.decode('utf-8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment