Skip to content

Commit c2bd398

Browse files
committed
Add caltechdata_unembargo function
1 parent 41de853 commit c2bd398

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

caltechdata_api/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .caltechdata_write import send_s3
33
from .caltechdata_edit import caltechdata_add
44
from .caltechdata_edit import caltechdata_edit
5+
from .caltechdata_edit import caltechdata_unembargo
56
from .customize_schema import customize_schema
67
from .decustomize_schema import decustomize_schema
78
from .get_metadata import get_metadata

caltechdata_api/caltechdata_edit.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,49 @@
44
from caltechdata_api import send_s3
55

66

7+
def caltechdata_unembargo(token, ids, production=False):
8+
"""Unembargo files from a record. Needed bacause the delete API call
9+
doesn't remove the embargo data"""
10+
11+
# Ensure ids is an arrary
12+
if isinstance(ids, int):
13+
ids = [str(ids)]
14+
if isinstance(ids, str):
15+
ids = [ids]
16+
17+
headers = {"Authorization": "Bearer %s" % token, "Content-type": "application/json"}
18+
19+
if production == True:
20+
url = "https://data.caltech.edu/submit/api/edit/"
21+
api_url = "https://data.caltech.edu/api/record/"
22+
else:
23+
url = "https://cd-sandbox.tind.io/submit/api/edit/"
24+
api_url = "https://cd-sandbox.tind.io/api/record/"
25+
26+
for idv in ids:
27+
update = []
28+
c = session()
29+
existing = c.get(api_url + idv)
30+
file_info = existing.json()["metadata"]
31+
if "electronic_location_and_access" in file_info:
32+
for ex in file_info["electronic_location_and_access"]:
33+
name = ex["electronic_name"][0]
34+
fu = ex["uniform_resource_identifier"].split("/")[-2]
35+
update.append({"id": fu, "embargo_status": "open"})
36+
37+
metadata = {
38+
"id": idv,
39+
"embargo_date": "2000-01-01",
40+
"files": {"update": update},
41+
}
42+
43+
dat = json.dumps({"record": metadata})
44+
45+
c = session()
46+
response = c.post(url, headers=headers, data=dat)
47+
return response.text
48+
49+
750
def caltechdata_edit(token, ids, metadata={}, files={}, delete={}, production=False):
851
"""Including files will only replaces files if they have the same name
952
The delete option will delete any existing files with a given file extension

caltechdata_api/caltechdata_write.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ def send_s3(filepath, token, production=False):
5252

5353
c = session()
5454
response = c.post(url, files=form, headers=s3headers)
55-
print(response)
55+
# print(response)
5656
if response.text:
5757
raise Exception(response.text)
5858

5959
# print(chkurl + "/" + bucket + "/" + key + "/")
6060
# print(headers)
6161
response = c.get(chkurl + "/" + bucket + "/" + key + "/", headers=headers)
62-
print(response)
62+
# print(response)
6363
md5 = response.json()["md5"]
6464
filename = filepath.split("/")[-1]
6565

codemeta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"codeRepository": "https://github.com/caltechlibrary/caltechdata_api",
77
"issueTracker": "https://github.com/caltechlibrary/caltechdata_api/issues",
88
"license": "https://data.caltech.edu/license",
9-
"version": "0.1.5",
9+
"version": "0.1.6",
1010
"author": [
1111
{
1212
"@type": "Person",

0 commit comments

Comments
 (0)