Skip to content
This repository has been archived by the owner on Mar 17, 2019. It is now read-only.

Commit

Permalink
python client: switch to requests library (closes #308)
Browse files Browse the repository at this point in the history
  • Loading branch information
koelnconcert committed Nov 12, 2015
1 parent 0745e85 commit b4c95be
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 99 deletions.
19 changes: 8 additions & 11 deletions client/python/delete_metadata.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import httplib2, sys, base64
import requests, sys

endpoint = 'https://mds.datacite.org/metadata'
#endpoint = 'https://mds.datacite.org/metadata'
endpoint = 'https://mds.test.datacite.org/metadata'

if (len(sys.argv) < 4):
raise Exception('Please provide username, password and doi')

h = httplib2.Http()
auth_string = base64.encodestring(sys.argv[1] + ':' + sys.argv[2])
response, content = h.request(endpoint + '/' + sys.argv[3],
'DELETE',
headers={'Authorization':'Basic ' + auth_string})
username, password, doi = sys.argv[1:]

if (response.status != 201):
print str(response.status)
print(content.decode('utf-8'))
response = requests.delete(endpoint + '/' + doi,
auth = (username, password))

print str(response.status_code) + " " + response.text
17 changes: 17 additions & 0 deletions client/python/get_doi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import requests, sys

#endpoint = 'https://mds.datacite.org/doi'
endpoint = 'https://mds.test.datacite.org/doi'

if (len(sys.argv) < 4):
raise Exception('Please provide username, password and doi')

username, password, doi = sys.argv[1:]

response = requests.get(endpoint + '/' + doi,
auth = (username, password))

if (response.status_code != 200):
print str(response.status_code) + " " + response.text
else:
print response.text
23 changes: 12 additions & 11 deletions client/python/get_metadata.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import httplib2, sys, base64
import requests, sys

endpoint = 'https://mds.datacite.org/metadata'
#endpoint = 'https://mds.datacite.org/metadata'
endpoint = 'https://mds.test.datacite.org/metadata'

if (len(sys.argv) < 4):
raise Exception('Please provide username, password and doi')

h = httplib2.Http()
auth_string = base64.encodestring(sys.argv[1] + ':' + sys.argv[2])
response, content = h.request(endpoint + '/' + sys.argv[3],
headers={'Accept':'application/xml',
'Authorization':'Basic ' + auth_string})
username, password, doi = sys.argv[1:]

if (response.status != 200):
print str(response.status)

print(content.decode('utf-8'))
response = requests.get(endpoint + '/' + doi,
auth = (username, password),
headers = {'Accept':'application/xml'})

if (response.status_code != 200):
print str(response.status_code) + " " + response.text
else:
print response.text
27 changes: 11 additions & 16 deletions client/python/post_doi.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import httplib2, sys, base64, codecs
import requests, sys, codecs

#endpoint = 'https://mds.datacite.org/doi'
endpoint = 'https://mds.test.datacite.org/doi'

if (len(sys.argv) < 4):
raise Exception('Please provide username, password, location of doi-url file')

endpoint = 'https://mds.datacite.org/doi'

body_unicode = codecs.open(sys.argv[3], 'r', encoding='utf-8').read().strip()
username, password, filename = sys.argv[1:]

print(body_unicode);
file = codecs.open(sys.argv[3], 'r', encoding='utf-8').read().strip()

h = httplib2.Http()
auth_string = base64.encodestring(sys.argv[1] + ':' + sys.argv[2])
response = requests.post(endpoint,
auth = (username, password),
data = file.encode('utf-8'),
headers = {'Content-Type':'text/plain;charset=UTF-8'})

response, content = h.request(endpoint,
'POST',
body = body_unicode.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'))
print str(response.status_code) + " " + response.text
28 changes: 12 additions & 16 deletions client/python/post_metadata.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import httplib2, sys, base64, codecs
import requests, sys, codecs

#endpoint = 'https://mds.datacite.org/metadata'
endpoint = 'https://mds.test.datacite.org/metadata'

if (len(sys.argv) < 4):
raise Exception('Please provide username, password and location of metadata file')

endpoint = 'https://mds.datacite.org/metadata'

body_unicode = codecs.open(sys.argv[3], 'r', encoding='utf-8').read()
username, password, filename = sys.argv[1:]

print(body_unicode);
metadata = codecs.open(filename, 'r', encoding='utf-8').read()

h = httplib2.Http()
auth_string = base64.encodestring(sys.argv[1] + ':' + sys.argv[2])
response, content = h.request(endpoint,
'POST',
body = body_unicode.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'))
response = requests.post(endpoint,
auth = (username, password),
data = metadata.encode('utf-8'),
headers = {'Content-Type':'application/xml;charset=UTF-8'})

print str(response.status_code) + " " + response.text
23 changes: 0 additions & 23 deletions client/python/put_doi.py

This file was deleted.

22 changes: 0 additions & 22 deletions client/python/put_metadata.py

This file was deleted.

0 comments on commit b4c95be

Please sign in to comment.