-
Notifications
You must be signed in to change notification settings - Fork 13
/
datacite.py
65 lines (47 loc) · 2.15 KB
/
datacite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Functions for communicating with DataCite and some utilities."""
__copyright__ = 'Copyright (c) 2019-2024, Utrecht University'
__license__ = 'GPLv3, see LICENSE'
import random
import string
from typing import Dict
import requests
from util import *
def metadata_post(payload: Dict) -> int:
"""Register DOI metadata with DataCite."""
url = "{}/dois".format(config.datacite_rest_api_url)
auth = (config.datacite_username, config.datacite_password)
headers = {'Content-Type': 'application/json', 'charset': 'UTF-8'}
response = requests.post(url,
auth=auth,
data=payload.encode(),
headers=headers,
timeout=30,
verify=config.datacite_tls_verify)
return response.status_code
def metadata_put(doi: str, payload: str) -> int:
"""Update metadata with DataCite."""
url = "{}/dois/{}".format(config.datacite_rest_api_url, doi)
auth = (config.datacite_username, config.datacite_password)
headers = {'Content-Type': 'application/json', 'charset': 'UTF-8'}
response = requests.put(url,
auth=auth,
data=payload.encode(),
headers=headers,
timeout=30,
verify=config.datacite_tls_verify)
return response.status_code
def metadata_get(doi: str) -> int:
"""Check with DataCite if DOI is available."""
url = "{}/dois/{}".format(config.datacite_rest_api_url, doi)
auth = (config.datacite_username, config.datacite_password)
headers = {'Content-Type': 'application/json', 'charset': 'UTF-8'}
response = requests.get(url,
auth=auth,
headers=headers,
timeout=30,
verify=config.datacite_tls_verify)
return response.status_code
def generate_random_id(length: int) -> str:
"""Generate random ID for DOI."""
characters = string.ascii_uppercase + string.digits
return ''.join(random.choice(characters) for x in range(int(length)))