Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion permanent_upload/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ def main(environment, path):
for f in files:
logging.info("Processing %s", f)
results.append(
api.file_upload(f, parent_folder_id, parent_folder_link_id, timeout)
api.file_upload(
f,
parent_folder_id,
parent_folder_link_id,
archive["archiveId"],
login_result.response["SimpleVO"]["value"],
timeout,
)
)
print(tabulate(results, headers, tablefmt="github"))
validate_supported_types(results)
Expand Down
74 changes: 60 additions & 14 deletions permanent_upload/permanent.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,40 @@ def make_request(self):
PermanentRequest.csrf = json_resp["csrf"]


class V2PermanentRequest:
csrf = ""
session = requests.Session()

def __init__(self, base_url, path, auth_token, data=[]):
"""
Craft the fields required to make a request.
"""
self.body = data
self.url = f"{base_url}{path}"
self.auth_token = auth_token
self.make_request()

def make_request(self):
"""
Make a JSON post request to the API backend.
"""
logging.debug("Request url: %s", self.url)
logging.debug("Request json: %s", self.body)
response = PermanentRequest.session.post(
self.url,
json=self.body,
headers={
"Request-Version": "2",
"Authorization": "Bearer " + self.auth_token,
},
)
if not response.ok:
utils.raise_for_status(response)
json_resp = response.json()
logging.debug("Response body: %s", json_resp)
self.response = json_resp


class PermanentAPI:
def __init__(self, base_url=None):
if not base_url:
Expand All @@ -57,19 +91,12 @@ def _measure_post_upload_processing(self, record_vo, timeout):
i += 1
return i, record

def _register_record(self, record_vo, s3_url):
logging.info("Register record for file %s", record_vo["displayName"])
body = {
"RecordVO": record_vo,
"SimpleVO": {
"key": "s3url",
"value": s3_url,
},
}
request = PermanentRequest(
self.base_url, "/api/record/registerRecord", data=body
def _register_record(self, auth_token, body):
logging.info("Register record for file %s", body["displayName"])
request = V2PermanentRequest(
self.base_url, "/api/record/registerRecord", auth_token, data=body
)
return request.response["RecordVO"]
return request.response

def _get_presigned_url(self, record_vo):
logging.info("Get pre-signed URL for %s", record_vo["displayName"])
Expand Down Expand Up @@ -100,7 +127,15 @@ def _get_record(self, record_id, archive_nbr):
request = PermanentRequest(self.base_url, "/api/record/get", data=body)
return request.response["RecordVO"]

def file_upload(self, file_path, parent_folder_id, parent_folder_link_id, timeout):
def file_upload(
self,
file_path,
parent_folder_id,
parent_folder_link_id,
archive_id,
auth_token,
timeout,
):
"""
Perform the file upload requests, and then poll for status until the timeout.
"""
Expand All @@ -117,7 +152,18 @@ def file_upload(self, file_path, parent_folder_id, parent_folder_link_id, timeou

s3_info = self._get_presigned_url(record_vo)
utils.upload_to_s3(file_path, s3_info["presignedPost"])
created_record_vo = self._register_record(record_vo, s3_info["destinationUrl"])

request = {
"archiveId": archive_id,
"derivedCreatedDT": int(time.time()),
"displayName": filename,
"parentFolderId": parent_folder_id,
"parentFolder_linkId": parent_folder_link_id,
"uploadFileName": filename,
"size": os.path.getsize(file_path),
"s3url": s3_info["destinationUrl"],
}
created_record_vo = self._register_record(auth_token, request)

attempts, processed_record = self._measure_post_upload_processing(
created_record_vo, timeout
Expand Down
Loading