Skip to content

Commit

Permalink
Merge pull request #27 from ResearchObject/fix-conda-build
Browse files Browse the repository at this point in the history
Move credentials calls inside functions to fix conda-forge build
  • Loading branch information
elichad authored Jul 9, 2024
2 parents 0bd9d7f + 25d5e64 commit a5ca85b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/rocrate_inveniordm/upload/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@
import requests
import rocrate_inveniordm.upload.credentials as credentials

api_url = credentials.get_repository_base_url()

headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": f"Bearer {credentials.get_api_key()}",
}

headers_stream = {
"Accept": "application/json",
"Content-Type": "application/octet-stream",
"Authorization": f"Bearer {credentials.get_api_key()}",
}
def get_headers(content_type: str):
headers = {
"Accept": "application/json",
"Content-Type": content_type,
"Authorization": f"Bearer {credentials.get_api_key()}",
}
return headers


def deposit(metadata, files, publish=False):
Expand All @@ -52,10 +47,11 @@ def create_draft_record(metadata):
:param files: The record's files.
:returns: The record's id.
"""
api_url = credentials.get_repository_base_url()
resp = requests.post(
f"{api_url}/api/records",
data=json.dumps(metadata),
headers=headers,
headers=get_headers("application/json"),
)

if resp.status_code != 201:
Expand All @@ -78,10 +74,11 @@ def start_draft_files_upload(record_id, files):
_, filename = os.path.split(file)
payload.append({"key": filename})

api_url = credentials.get_repository_base_url()
resp = requests.post(
f"{api_url}/api/records/{record_id}/draft/files",
data=json.dumps(payload),
headers=headers,
headers=get_headers("application/json"),
)
if resp.status_code != 201:
print(f"Could not initiate file upload: {resp.status_code} {resp.text}")
Expand All @@ -101,22 +98,23 @@ def upload_file(record_id, file_path):
print(file_name)

# Upload file content
api_url = credentials.get_repository_base_url()
upload_url = f"{api_url}/api/records/{record_id}/draft/files/{file_name}/content"
try:
# regular file
with open(file_path, "r") as f:
resp = requests.put(
upload_url,
data=f,
headers=headers_stream,
headers=get_headers("application/octet-stream"),
)
except UnicodeDecodeError:
# binary file
with open(file_path, "rb") as f:
resp = requests.put(
upload_url,
data=f,
headers=headers_stream,
headers=get_headers("application/octet-stream"),
)

if resp.status_code != 200:
Expand All @@ -126,7 +124,7 @@ def upload_file(record_id, file_path):
# Complete draft file upload
resp = requests.post(
f"{api_url}/api/records/{record_id}/draft/files/{file_name}/commit",
headers=headers,
headers=get_headers("application/json"),
)
if resp.status_code != 200:
print(f"Could not commit file upload: {resp.status_code} {resp.text}")
Expand Down Expand Up @@ -162,9 +160,10 @@ def publish_record(record_id):
:param record_id: The record's id.
"""
api_url = credentials.get_repository_base_url()
resp = requests.post(
f"{api_url}/api/records/{record_id}/draft/actions/publish",
headers=headers,
headers=get_headers("application/json"),
)
if resp.status_code != 202:
print(f"Could not publish record: {resp.status_code} {resp.text}")
Expand Down
18 changes: 18 additions & 0 deletions test/unit/test_uploader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
from unittest import mock

import rocrate_inveniordm.upload.uploader as uploader


@mock.patch.dict(os.environ, {"INVENIORDM_API_KEY": "test-key"})
def test_get_headers():
input = "application/json"
expected = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": f"Bearer test-key",
}

result = uploader.get_headers(input)

assert result == expected

0 comments on commit a5ca85b

Please sign in to comment.