Skip to content

Commit 56cc5ae

Browse files
committed
Use V2 requests to /registerRecord
All /registerRecord calls should use V2 requests, because this is necessary for the endpoint to fire events.
1 parent 90b25c2 commit 56cc5ae

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

permanent_upload/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def main(environment, path):
6565
for f in files:
6666
logging.info("Processing %s", f)
6767
results.append(
68-
api.file_upload(f, parent_folder_id, parent_folder_link_id, timeout)
68+
api.file_upload(f, parent_folder_id, parent_folder_link_id, archive["archiveId"], login_result.response["SimpleVO"]["value"], timeout)
6969
)
7070
print(tabulate(results, headers, tablefmt="github"))
7171
validate_supported_types(results)

permanent_upload/permanent.py

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,33 @@ def make_request(self):
3636
PermanentRequest.csrf = json_resp["csrf"]
3737

3838

39+
class V2PermanentRequest:
40+
csrf = ""
41+
session = requests.Session()
42+
43+
def __init__(self, base_url, path, auth_token, data=[]):
44+
"""
45+
Craft the fields required to make a request.
46+
"""
47+
self.body = data
48+
self.url = f"{base_url}{path}"
49+
self.auth_token = auth_token
50+
self.make_request()
51+
52+
def make_request(self):
53+
"""
54+
Make a JSON post request to the API backend.
55+
"""
56+
logging.debug("Request url: %s", self.url)
57+
logging.debug("Request json: %s", self.body)
58+
response = PermanentRequest.session.post(self.url, json=self.body, headers={"Request-Version": "2", "Authorization": "Bearer " + self.auth_token})
59+
if not response.ok:
60+
utils.raise_for_status(response)
61+
json_resp = response.json()
62+
logging.debug("Response body: %s", json_resp)
63+
self.response = json_resp
64+
65+
3966
class PermanentAPI:
4067
def __init__(self, base_url=None):
4168
if not base_url:
@@ -57,19 +84,12 @@ def _measure_post_upload_processing(self, record_vo, timeout):
5784
i += 1
5885
return i, record
5986

60-
def _register_record(self, record_vo, s3_url):
61-
logging.info("Register record for file %s", record_vo["displayName"])
62-
body = {
63-
"RecordVO": record_vo,
64-
"SimpleVO": {
65-
"key": "s3url",
66-
"value": s3_url,
67-
},
68-
}
69-
request = PermanentRequest(
70-
self.base_url, "/api/record/registerRecord", data=body
87+
def _register_record(self, auth_token, body):
88+
logging.info("Register record for file %s", body["displayName"])
89+
request = V2PermanentRequest(
90+
self.base_url, "/api/record/registerRecord", auth_token, data=body
7191
)
72-
return request.response["RecordVO"]
92+
return request.response
7393

7494
def _get_presigned_url(self, record_vo):
7595
logging.info("Get pre-signed URL for %s", record_vo["displayName"])
@@ -100,7 +120,7 @@ def _get_record(self, record_id, archive_nbr):
100120
request = PermanentRequest(self.base_url, "/api/record/get", data=body)
101121
return request.response["RecordVO"]
102122

103-
def file_upload(self, file_path, parent_folder_id, parent_folder_link_id, timeout):
123+
def file_upload(self, file_path, parent_folder_id, parent_folder_link_id, archive_id, auth_token, timeout):
104124
"""
105125
Perform the file upload requests, and then poll for status until the timeout.
106126
"""
@@ -117,7 +137,19 @@ def file_upload(self, file_path, parent_folder_id, parent_folder_link_id, timeou
117137

118138
s3_info = self._get_presigned_url(record_vo)
119139
utils.upload_to_s3(file_path, s3_info["presignedPost"])
120-
created_record_vo = self._register_record(record_vo, s3_info["destinationUrl"])
140+
141+
request = {
142+
"archiveId": archive_id,
143+
"derivedCreatedDT": int(time.time()),
144+
"displayName": filename,
145+
"parentFolderId": parent_folder_id,
146+
"parentFolder_linkId": parent_folder_link_id,
147+
"uploadFileName": filename,
148+
"size": os.path.getsize(file_path),
149+
"s3url": s3_info["destinationUrl"],
150+
}
151+
created_record_vo = self._register_record(auth_token, request)
152+
print(created_record_vo)
121153

122154
attempts, processed_record = self._measure_post_upload_processing(
123155
created_record_vo, timeout

0 commit comments

Comments
 (0)