From bb6fc18d042e8a9e8da4f91e0d1af058ce9249f2 Mon Sep 17 00:00:00 2001 From: Alex Ioannidis Date: Wed, 5 Jul 2023 08:42:25 +0200 Subject: [PATCH] partners: add initial partner test payloads --- site/zenodo_rdm/partners/__init__.py | 30 +++++ site/zenodo_rdm/partners/client.py | 128 ++++++++++++++++++++ site/zenodo_rdm/partners/dryad.py | 96 +++++++++++++++ site/zenodo_rdm/partners/lory.py | 75 ++++++++++++ site/zenodo_rdm/partners/payloads.py | 175 +++++++++++++++++++++++++++ site/zenodo_rdm/partners/plazi.py | 119 ++++++++++++++++++ 6 files changed, 623 insertions(+) create mode 100644 site/zenodo_rdm/partners/__init__.py create mode 100644 site/zenodo_rdm/partners/client.py create mode 100644 site/zenodo_rdm/partners/dryad.py create mode 100644 site/zenodo_rdm/partners/lory.py create mode 100644 site/zenodo_rdm/partners/payloads.py create mode 100644 site/zenodo_rdm/partners/plazi.py diff --git a/site/zenodo_rdm/partners/__init__.py b/site/zenodo_rdm/partners/__init__.py new file mode 100644 index 00000000..94959b32 --- /dev/null +++ b/site/zenodo_rdm/partners/__init__.py @@ -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")]) diff --git a/site/zenodo_rdm/partners/client.py b/site/zenodo_rdm/partners/client.py new file mode 100644 index 00000000..8140e2b9 --- /dev/null +++ b/site/zenodo_rdm/partners/client.py @@ -0,0 +1,128 @@ +# -*- 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): + """Initialize error.""" + 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"), +) diff --git a/site/zenodo_rdm/partners/dryad.py b/site/zenodo_rdm/partners/dryad.py new file mode 100644 index 00000000..473a2a25 --- /dev/null +++ b/site/zenodo_rdm/partners/dryad.py @@ -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": "

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.

Funding provided by: Biotechnology and Biological Sciences Research Council
Crossref Funder Registry ID: http://dx.doi.org/10.13039/501100000268
Award Number: BB/S507416/1

", + # 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, + }, + ], + } +} diff --git a/site/zenodo_rdm/partners/lory.py b/site/zenodo_rdm/partners/lory.py new file mode 100644 index 00000000..8659a195 --- /dev/null +++ b/site/zenodo_rdm/partners/lory.py @@ -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", + } +} diff --git a/site/zenodo_rdm/partners/payloads.py b/site/zenodo_rdm/partners/payloads.py new file mode 100644 index 00000000..48951146 --- /dev/null +++ b/site/zenodo_rdm/partners/payloads.py @@ -0,0 +1,175 @@ +# -*- 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. + +"""Base payloads and utilities.""" + +import io +import secrets +from datetime import datetime, timedelta + + +def rand_bytes_file(name, size=10): + """Generate an in-memory file pointer with random hex string content.""" + fp = io.BytesIO(secrets.token_hex(size).encode("utf-8")) + fp.name = name + return fp + + +FULL_METADATA = { + "metadata": { + # Communities + # "communities": [{"identifier": "c1"}], + # + # Required/basic fields + # + # Resource type + "upload_type": "dataset", + # "upload_type": "publication", + # "publication_type": "book", + # "upload_type": "image", + # "image_type": "figure", + "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", + # "prereserve_doi": True, + "version": "v1.0.0", + "language": "fre", + "keywords": ["Keyword 1", "keyword 2"], + "notes": "Some notes", + # Access + "access_right": "open", + # "access_right": "closed", + # "access_right": "embargoed", + # "embargo_date": (datetime.utcnow().date() + timedelta(days=1)).isoformat(), + # "access_right": "restricted", + # "access_conditions": "You have to be affiliated with XYZ to request access.", + "license": "cc-zero", + # Funding + "grants": [ + {"id": "10.13039/501100001665::755021"}, + ], + # Related/alternate identifiers + "related_identifiers": [ + { + "identifier": "10.1234/foo.bar2", + "relation": "isCitedBy", + "scheme": "doi", + }, + { + "identifier": "10.1234/foo.bar3", + "relation": "cites", + "scheme": "doi", + "resource_type": "dataset", + }, + # TODO commented since scheme ads not supported in rdm + # { + # "identifier": "2011ApJS..192...18K", + # "relation": "isAlternateIdentifier", + # "scheme": "ads", + # "resource_type": "publication-article", + # }, + ], + # Contributors + "contributors": [ + { + "name": "Doe, Jochen", + "affiliation": "Atlantis", + "type": "Other", + }, + { + "name": "Smith, Marco", + "affiliation": "Atlantis", + "orcid": "0000-0002-1825-0097", + "gnd": "170118215", + "type": "DataCurator", + }, + ], + # References + "references": [ + "Reference 1", + "Reference 2", + ], + # Journal + "journal_title": "Some journal name", + "journal_volume": "Some volume", + "journal_issue": "Some issue", + "journal_pages": "Some pages", + # Conference + "conference_title": "Some title", + "conference_acronym": "Some acronym", + "conference_dates": "Some dates", + "conference_place": "Some place", + "conference_url": "http://someurl.com", + "conference_session": "VI", + "conference_session_part": "1", + # Imprint + "imprint_publisher": "Zenodo", + "imprint_place": "Some place", + "imprint_isbn": "978-3-16-148410-0", + "partof_title": "Some part of title", + "partof_pages": "SOme part of", + # Thesis + "thesis_supervisors": [ + {"name": "Doe, John", "affiliation": "Atlantis"}, + { + "name": "Smith, Jane", + "affiliation": "Atlantis", + "orcid": "0000-0002-1825-0097", + "gnd": "170118215", + }, + ], + "thesis_university": "Some thesis_university", + # Subjects + "subjects": [ + {"scheme": "gnd", "identifier": "gnd:1234567899", "term": "Astronaut"}, + {"scheme": "gnd", "identifier": "gnd:1234567898", "term": "Amish"}, + ], + # Locations + "locations": [ + { + "place": "London", + "description": "A nice location", + "lat": 10.2, + "lon": 5, + }, + { + "place": "Lisbon", + "lat": 5.1231221, + "lon": 1.23232323, + }, + ], + # Dates + "dates": [ + { + "start": "2018-03-21", + "end": "2018-03-25", + "type": "Collected", + "description": "Specimen A5 collection period.", + }, + ], + # Custom + # "custom": { + # "dwc:genus": "Felis", + # }, + # Misc + "method": "This is the method used to collect this data.", + } +} diff --git a/site/zenodo_rdm/partners/plazi.py b/site/zenodo_rdm/partners/plazi.py new file mode 100644 index 00000000..cea3282f --- /dev/null +++ b/site/zenodo_rdm/partners/plazi.py @@ -0,0 +1,119 @@ +# -*- 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. + +"""Plazi payloads and settings.""" + +from datetime import datetime, timedelta + +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": "Agrisius japonicus dubatolovi Orhant, 2012 Orhant, 2012", + "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", + # "prereserve_doi": True, + "keywords": ["Keyword 1", "keyword 2"], + "notes": "Some notes", + # 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": [ + { + "relation": "isPartOf", + "identifier": "https://doi.org/10.11646/zootaxa.4459.1.5", + }, + { + "relation": "cites", + "identifier": "https://zenodo.org/record/1458418/files/figure.png", + }, + { + "relation": "cites", + "identifier": "https://zenodo.org/record/1458424/files/figure.png", + }, + ], + # Contributors + "contributors": [ + {"name": "K. Ueda & Dubatolov", "type": "DataCollector"}, + {"name": "Wang Min & Dubatolov & Coll.", "type": "DataCollector"}, + {"name": "Coll.", "type": "DataCollector"}, + ], + # Journal + "journal_title": "On the taxonomy of the genus Agrisius Walker, 1855, with descriptions of two new species from Vietnam and Laos (Lepidoptera, Erebidae, Arctiinae) Zootaxa", + "journal_year": "2018", + "journal_volume": "4459", + "journal_issue": "1", + "references": [ + "Orhant, G. (2012) Deux nOuVeaux Agrisius Orientaux (LepidOptera, Arctiidae, LithOsiinae). Bulletin de la Societe entomologique de Mulhouse, 68 (3), 37 - 38.", + "Leech, J. H. (1889) 3. On the LepidOptera OF Japan and COrea. - Part II. HeterOcera, sect. I. Proceedings of the general meetings for scientific business of the Zoological Society of London, 1888, 580 - 654.", + "DubatOlOV, V. V. & Kishida, Y. (2013) ReMarks On the species cOMpOsitiOn OF the genus Agrisius, With a descriptiOn OF a neW species FrOM LaOs (LepidOptera, Arctiidae: LithOsiinae). Tinea, 22 (3), 156 - 160.", + "Daniel, F. (1952) Beitrage zur Kenntnis der Arctiidae Ostasiens unter besOnderer Berucksichtigung der Ausbeuten VOn Dr. h. c. H. Hone aus dieseM Gebiet (Lep. - Het.). Bonner zoologische Beitrage, 3 (1 - 2 & 3 - 4), 75 - 90 & 305 - 324.", + ], + # Locations + "locations": [ + { + "place": "London", + "description": "A nice location", + "lat": 10.2, + "lon": 5, + }, + { + "place": "Lisbon", + "lat": 5.1231221, + "lon": 1.23232323, + }, + ], + # Dates + "dates": [ + {"start": "2002-03-01", "end": "2002-03-31", "type": "Collected"}, + {"start": "2003-06-01", "end": "2003-06-30", "type": "Collected"}, + {"start": "2004-05-24", "end": "2004-05-24", "type": "Collected"}, + ], + # Misc + "method": "A very precise scientific method was used...", + # Custom + "custom": { + "dwc:scientificNameAuthorship": ["Volynkin & Dubatolov & Kishida"], + "dwc:kingdom": ["Animalia"], + "dwc:phylum": ["Arthropoda"], + "dwc:order": ["Lepidoptera"], + "dwc:family": ["Erebidae"], + "dwc:genus": ["Agrisius"], + "dwc:specificEpithet": ["japonicus"], + "dwc:taxonomicStatus": ["stat. nov."], + "dwc:taxonRank": ["subSpecies"], + "dwc:collectionCode": ["SZMN"], + }, + }, +}