Skip to content

Commit 91533bd

Browse files
committed
Add more retry options
1 parent c95e691 commit 91533bd

2 files changed

Lines changed: 38 additions & 25 deletions

File tree

caltechdata_api/caltechdata_edit.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import copy, os, json
1+
import copy, os, json, time
22

33
import requests
44
from requests import session
@@ -193,7 +193,14 @@ def caltechdata_edit(
193193
headers=headers,
194194
)
195195
if existing.status_code != 200:
196-
raise Exception(f"Record {idv} does not exist, cannot edit")
196+
# Try back again to the record
197+
existing = requests.get(
198+
url + "/api/records/" + idv,
199+
headers=headers,
200+
)
201+
202+
if existing.status_code != 200:
203+
raise Exception(f"Record {idv} does not exist, cannot edit")
197204

198205
existing = existing.json()
199206
status = existing["status"]
@@ -329,7 +336,13 @@ def caltechdata_edit(
329336
headers=headers,
330337
)
331338
if result.status_code != 201:
332-
raise Exception(result.text)
339+
time.sleep(3)
340+
result = requests.post(
341+
url + "/api/records/" + idv + "/draft",
342+
headers=headers,
343+
)
344+
if result.status_code != 201:
345+
raise Exception(result.text)
333346
# We want files to stay the same as the existing record
334347
data["files"] = existing["files"]
335348
if default_preview:
@@ -341,13 +354,23 @@ def caltechdata_edit(
341354
json=data,
342355
)
343356
if result.status_code != 200:
344-
raise Exception(result.text)
357+
time.sleep(3)
358+
result = requests.put(
359+
url + "/api/records/" + idv + "/draft",
360+
headers=headers,
361+
json=data,
362+
)
363+
if result.status_code != 200:
364+
raise Exception(result.text)
345365

346366
if publish:
347367
publish_link = f"{url}/api/records/{idv}/draft/actions/publish"
348368
result = requests.post(publish_link, headers=headers)
349369
if result.status_code != 202:
350-
raise Exception(result.text)
370+
time.sleep(3)
371+
result = requests.post(publish_link, headers=headers)
372+
if result.status_code != 202:
373+
raise Exception(result.text)
351374
if return_id:
352375
return result.json()["id"]
353376
else:

caltechdata_api/cli.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -173,23 +173,19 @@ def get_funding_details():
173173
award_number = get_user_input("Enter the award number for funding: ")
174174
award_exists = check_award_number(award_number)
175175
if not award_exists:
176-
print(
177-
f"""Error: No award with number '{award_number}' found in
176+
print(f"""Error: No award with number '{award_number}' found in
178177
CaltechDATA. You will need to provide more details about the
179-
funding."""
180-
)
178+
funding.""")
181179
award_title = get_user_input("Enter the award title for funding: ")
182180
while True:
183181
funder_identifier = get_user_input("Enter the funder ROR (https://ror.org): ")
184182
name = validate_funder_identifier(funder_identifier)
185183
if name:
186184
break
187185
else:
188-
print(
189-
"""This funder identifier is not a ROR. Please enter a valid
186+
print("""This funder identifier is not a ROR. Please enter a valid
190187
ROR identifier (without the url). For example the ROR for the
191-
NSF is 021nxhr62."""
192-
)
188+
NSF is 021nxhr62.""")
193189
print("-" * 10)
194190
return {
195191
"awardNumber": award_number,
@@ -349,14 +345,12 @@ def write_s3cmd_config(endpoint):
349345
access_key = get_user_input("Enter the access key: ")
350346
secret_key = get_user_input("Enter the secret key: ")
351347
with open(configf, "w") as file:
352-
file.write(
353-
f"""[default]
348+
file.write(f"""[default]
354349
access_key = {access_key}
355350
host_base = {endpoint}
356351
host_bucket = %(bucket).{endpoint}
357352
secret_key = {secret_key}
358-
"""
359-
)
353+
""")
360354

361355

362356
def upload_supporting_file(record_id=None):
@@ -431,11 +425,9 @@ def upload_supporting_file(record_id=None):
431425
elif filename in files:
432426
file_size = os.path.getsize(filename)
433427
if file_size > 1024 * 1024 * 1024:
434-
print(
435-
"""The file is greater than 1 GB. Please upload the
428+
print("""The file is greater than 1 GB. Please upload the
436429
metadata to CaltechDATA, and you'll be provided
437-
instructions to upload the files to S3 directly."""
438-
)
430+
instructions to upload the files to S3 directly.""")
439431
else:
440432
filepath = os.path.abspath(filename)
441433
filepaths.append(filepath)
@@ -643,12 +635,10 @@ def print_upload_message(rec_id, production):
643635
if production
644636
else "https://data.caltechlibrary.dev/uploads/"
645637
)
646-
print(
647-
f"""You can view and publish this record at
638+
print(f"""You can view and publish this record at
648639
{base_url}{rec_id}
649640
If you need to upload large files to S3, you can type
650-
`s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/`"""
651-
)
641+
`s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/`""")
652642

653643

654644
def edit_record(production):

0 commit comments

Comments
 (0)