|
4 | 4 | from caltechdata_api import send_s3 |
5 | 5 |
|
6 | 6 |
|
| 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 | + |
7 | 50 | def caltechdata_edit(token, ids, metadata={}, files={}, delete={}, production=False): |
8 | 51 | """Including files will only replaces files if they have the same name |
9 | 52 | The delete option will delete any existing files with a given file extension |
|
0 commit comments