-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
partners: add initial partner test payloads
- Loading branch information
Showing
6 changed files
with
622 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2023 CERN. | ||
# | ||
# ZenodoRDM is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Utilities for testing partner payloads on the legacy REST API. | ||
Usage: python -m zenodo_rdm.partners "dryad" | ||
""" | ||
import sys | ||
|
||
from . import dryad, lory, plazi | ||
from .client import client | ||
from .payloads import rand_bytes_file | ||
|
||
PARTNERS = { | ||
"dryad": dryad, | ||
"lory": lory, | ||
"plazi": plazi, | ||
} | ||
|
||
|
||
if __name__ == "__main__": | ||
partner = sys.argv[1] | ||
|
||
data = PARTNERS[partner].PAYLOAD | ||
res = client.create(data, files=[rand_bytes_file("data.txt")]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2023 CERN. | ||
# | ||
# ZenodoRDM is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Zenodo legacy REST API client.""" | ||
import os | ||
from typing import BinaryIO | ||
|
||
import requests | ||
|
||
|
||
class ZenodoClientError(Exception): | ||
"""Zenodo REST API client error.""" | ||
|
||
def __init__(self, message, response): | ||
super().__init__(message) | ||
self.response = response | ||
|
||
|
||
class ZenodoClient: | ||
"""Zenodo REST API client.""" | ||
|
||
DOMAINS = { | ||
"local": "localhost:5000", | ||
"zenodo-prod": "zenodo.org", | ||
"zenodo-qa": "sandbox.zenodo.org", | ||
"zenodo-rdm-prod": "zenodo-rdm.web.cern.ch", | ||
"zenodo-rdm-qa": "zenodo-rdm-qa.web.cern.ch", | ||
} | ||
|
||
def __init__(self, token=None, *, base_url=None): | ||
"""Initialize client.""" | ||
self._token = token | ||
self.base_url = self.DOMAINS.get(base_url, base_url) | ||
self.session = self._create_session(token) | ||
|
||
@property | ||
def deposit_url(self): | ||
"""Legacy deposit REST API endpoint URL.""" | ||
return f"https://{self.base_url}/api/deposit/depositions" | ||
|
||
@staticmethod | ||
def _create_session(token): | ||
"""Create requests session.""" | ||
session = requests.Session() | ||
session.verify = False | ||
session.headers.update({"Authorization": f"Bearer {token}"}) | ||
return session | ||
|
||
def _raise_resp(self, message, response): | ||
"""Raise exception on bad response codes.""" | ||
if not response.ok: | ||
raise ZenodoClientError(message, response) | ||
|
||
def _upload_file(self, deposit, file: BinaryIO, use_files_api=True): | ||
"""Upload a file to the legacy REST API.""" | ||
if use_files_api: | ||
bucket_url = deposit["links"]["bucket"] | ||
upload_resp = self.session.put(f"{bucket_url}/{file.name}", data=file) | ||
else: | ||
files_url = deposit["links"]["files"] | ||
upload_resp = self.session.post( | ||
files_url, | ||
data={"name": file.name}, | ||
files={"file": file}, | ||
) | ||
|
||
self._raise_resp("Failed to upload file", upload_resp) | ||
return upload_resp | ||
|
||
def _publish(self, resp): | ||
"""Publish a deposit.""" | ||
publish_url = resp.json()["links"]["publish"] | ||
publish_resp = self.session.post(publish_url) | ||
self._raise_resp("Failed to publish deposit", publish_resp) | ||
return publish_resp | ||
|
||
def create( | ||
self, | ||
data: dict, | ||
files: list[BinaryIO], | ||
publish: bool = True, | ||
file_upload_kwargs: dict = None, | ||
): | ||
"""Create a new record with files.""" | ||
created_resp = self.session.post(self.deposit_url, json=data) | ||
self._raise_resp("Failed to create deposit", created_resp) | ||
|
||
for f in files: | ||
self._upload_file(created_resp.json(), file=f, **(file_upload_kwargs or {})) | ||
if not publish: | ||
return created_resp | ||
|
||
return self._publish(created_resp) | ||
|
||
def update( | ||
self, | ||
id: str, | ||
data: dict, | ||
files: list[BinaryIO] = None, | ||
publish: bool = True, | ||
): | ||
"""Update an existing record.""" | ||
deposit_url = f"{self.base_url}/{id}" | ||
get_resp = self.session.get(deposit_url) | ||
self._raise_resp("Failed to fetch deposit", get_resp) | ||
|
||
edit_url = get_resp.json()["links"]["edit"] | ||
edit_resp = self.session.post(edit_url) | ||
self._raise_resp("Failed to edit deposit", edit_resp) | ||
|
||
update_resp = self.session.put(deposit_url, json=data) | ||
self._raise_resp("Failed to update deposit", update_resp) | ||
|
||
if not publish: | ||
return update_resp | ||
|
||
return self._publish(update_resp) | ||
|
||
|
||
client = ZenodoClient( | ||
token=os.environ.get("ZENODO_TOKEN"), | ||
base_url=os.environ.get("ZENODO_BASE_URL"), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2023 CERN. | ||
# | ||
# ZenodoRDM is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Dryad payloads and settings.""" | ||
|
||
PAYLOAD = { | ||
"metadata": { | ||
# Communities | ||
# "communities": [{"identifier": "dryad"}], | ||
# | ||
# Required/basic fields | ||
# | ||
# Resource type | ||
# "upload_type": "dataset", | ||
# "upload_type": "software", | ||
"upload_type": "other", | ||
"publication_date": "2013-09-12", | ||
"title": "Test title", | ||
"description": "Some description", | ||
"creators": [ | ||
{ | ||
"name": "Doe, John", | ||
"affiliation": "Atlantis", | ||
"orcid": "0000-0002-1825-0097", | ||
"gnd": "170118215", | ||
}, | ||
{ | ||
"name": "Smith, Jane", | ||
"affiliation": "Atlantis", | ||
}, | ||
], | ||
# External DOI | ||
# "doi": "10.1234/foo.bar", | ||
"keywords": [ | ||
"cell biology", | ||
"machine learning", | ||
"cancer", | ||
"Open-source", | ||
"software", | ||
"phenotyping", | ||
"automation", | ||
"image analysis", | ||
], | ||
"notes": "<p>ROI files can be opened with ImageJ. Image .tif files can be opened with any imaging software including ImageJ. Feature tables are provided as comma separated files and can be opened with Excel, for example.</p><p>Funding provided by: Biotechnology and Biological Sciences Research Council<br>Crossref Funder Registry ID: http://dx.doi.org/10.13039/501100000268<br>Award Number: BB/S507416/1</p>", | ||
# Access | ||
"access_right": "open", | ||
# "access_right": "embargoed", | ||
# "embargo_date": (datetime.utcnow().date() + timedelta(days=1)).isoformat(), | ||
"license": "CC0-1.0", | ||
# "license": "CC-BY-4.0", | ||
# "license": "MIT", | ||
# "license": "LGPL-3.0-or-later", | ||
# Related/alternate identifiers | ||
"related_identifiers": [ | ||
{ | ||
"scheme": "url", | ||
"identifier": "https://www.researchsquare.com/article/rs-971415/v1", | ||
"relation": "isCitedBy", | ||
}, | ||
{ | ||
"scheme": "doi", | ||
"identifier": "10.5281/zenodo.7620171", | ||
"relation": "isDerivedFrom", | ||
}, | ||
{ | ||
"scheme": "url", | ||
"identifier": "https://cellphegui.shinyapps.io/app_to_host/", | ||
"relation": "isDerivedFrom", | ||
}, | ||
{ | ||
"scheme": "doi", | ||
"identifier": "10.5061/dryad.4xgxd25f0", | ||
"relation": "isDerivedFrom", | ||
}, | ||
], | ||
# Locations | ||
"locations": [ | ||
{"lat": 32.898114, "place": "San Diego, CA 92121, USA", "lon": -117.202936}, | ||
{ | ||
"place": "London", | ||
"description": "A nice location", | ||
"lat": 10.2, | ||
"lon": 5, | ||
}, | ||
{ | ||
"place": "Lisbon", | ||
"lat": 5.1231221, | ||
"lon": 1.23232323, | ||
}, | ||
], | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2023 CERN. | ||
# | ||
# ZenodoRDM is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""LORY payloads and settings.""" | ||
|
||
|
||
PAYLOAD = { | ||
"metadata": { | ||
# Communities | ||
"communities": [ | ||
{"identifier": "lory"}, | ||
{"identifier": "lory_unilu"}, | ||
{"identifier": "lory_unilu_rf"}, | ||
], | ||
# | ||
# Required/basic fields | ||
# | ||
# Resource type | ||
"upload_type": "publication", | ||
"publication_type": "article", | ||
"publication_date": "2013-09-12", | ||
"title": "Test title", | ||
"description": "Some description", | ||
"creators": [ | ||
{ | ||
"name": "Doe, John", | ||
"affiliation": "Atlantis", | ||
"orcid": "0000-0002-1825-0097", | ||
"gnd": "170118215", | ||
}, | ||
{ | ||
"name": "Smith, Jane", | ||
"affiliation": "Atlantis", | ||
}, | ||
], | ||
"notes": "+ zhb_151741 + Reihe: Luzerner historische Ver\u00f6ffentlichungen, Bd. 7", | ||
# Access | ||
"access_right": "open", | ||
"license": "CC-BY-NC-ND-4.0", | ||
# Journal | ||
"journal_title": "Journal of Global Buddhism", | ||
"journal_volume": "1", | ||
"journal_issue": "12, Dec. 2011", | ||
"journal_pages": "31-55", | ||
# Conference | ||
"conference_title": "DHIK Forum 2022 - Angewandte Forschung und Transfer im internationalen Rahmen", | ||
"conference_acronym": "DHIK", | ||
"conference_dates": "9 June 2022", | ||
"conference_place": "Hochschule Luzern - Departement Technik und Architektur, Technikumstrasse 21, CH-6048 Horw", | ||
"conference_url": "https://www.hslu.ch/dhik-forum-22", | ||
"conference_session": "13", | ||
"conference_session_part": "1", | ||
# Imprint | ||
"imprint_publisher": "Zenodo", | ||
"imprint_place": "Horw", | ||
"imprint_isbn": "0393-2990", | ||
"partof_title": "Some 'part of' title", | ||
"partof_pages": "234", | ||
# Thesis | ||
"thesis_supervisors": [ | ||
{"name": "Doe, John", "affiliation": "Atlantis"}, | ||
{ | ||
"name": "Smith, Jane", | ||
"affiliation": "Atlantis", | ||
"orcid": "0000-0002-1825-0097", | ||
"gnd": "170118215", | ||
}, | ||
], | ||
"thesis_university": "Hochschule Luzern \u2013 Soziale Arbeit", | ||
} | ||
} |
Oops, something went wrong.