Zum Inhalt

metadata_toolbox_upload

Overview

The metadata_toolbox_upload module uploads each article's CrossRef metadata to an external system (toolbox). For each article, it reads a previously-assigned toolbox ID from a text file, derives a project ID from it, and sends the article's metadata JSON as an HTTP POST request to a per-project toolbox endpoint, authenticated via Prefect Secret blocks.

Module ordering: metadata_toolbox_upload should run after toolbox_ids module.

The module contains two primary Python files:

  1. flow.py
  2. tasks.py

flow.py

This file defines the main flow for toolbox metadata upload. Unlike other modules, flow.py manually inserts its parent directory into sys.path at import time.

metadata_toolbox_upload()

  • Required Parameters:

  • name_of_config_file (str): Name of the configuration file. Default is "config-test_metadata_upload.json"

  • test_copy (bool): Flag to indicate if it is a test run. Default is False

  • Context for Use:

  • This function is the main entry point. It resolves the module context, then reads the metadata format string (header_metadata_format, used as part of the upload's Content-Type header) from the first journal entry in the module context, and passes it (along with the full module context) to tasks.upload_metadata, which performs all per-journal and per-article work.

  • Example Usage:

  • python metadata_toolbox_upload('config-test_metadata_upload.json', test_copy=False)

  • Example Output:

  • INFO: Module name: metadata_toolbox_upload INFO: Set Base Path to: /mnt/.../wdm-test INFO: Module Context: [{'ISSN': '2948-2143', 'position': 5, 'previous_module': 'toolbox_ids', 'metadata-format': 'crossref', ...}] INFO: Have to work on 1 journal(s) INFO: Start metadata upload for ISSN: 2948-2143. INFO: Toolbox ID: xxxxx-0001 INFO: Successfully Uploaded Metadata for Toolbox ID: xxxxx-0001

tasks.py

This file contains the task definitions used in the toolbox upload flow. It handles reading the toolbox ID, deriving the project ID and endpoint URL, and performing the authenticated HTTP POST request that uploads the metadata.

upload_metadata()

  • Required Parameters:

  • module_context (list): The resolved module context (journals + previous-module info)

  • module_name (str): Name of the current module
  • header_metadata_format (str): The metadata format string used in the upload's Content-Type header (e.g. crossref)
  • current_datetime (dict): Dictionary containing the current date and timestamp
  • test_copy (bool): Flag to indicate if it is a test run

  • Context for Use:

  • This is the main Prefect @task and the sole per-article processing function in this module. It follows the standard previous_module / temp-<module> folder convention: for each eligible article, it copies the previous module's content into a new temp directory, reads the toolbox ID file, and calls begin_upload_to_toolbox to perform the upload. On success, it writes a history.csv entry and finalizes the module folder (rename, clear previous module contents, mark as processed). On failure, it logs an error and moves on to the next article.

  • Example Usage:

  • This function is called directly from flow.py; it is not typically invoked standalone.

begin_upload_to_toolbox()

  • Required Parameters:

  • toolbox_id (str): The toolbox ID, e.g. 'xxxxx-0001'

  • metadata_file_path (pathlib.Path): Path to the article's metadata JSON file
  • header_metadata_format: The metadata format string used in the Content-Type header

  • Context for Use:

  • Builds the upload endpoint URL from the toolbox base URL (get_toolbox_base_url()) and the project ID (derived from toolbox_id via get_project_id), loads the toolbox credentials from Prefect (toolbox-username, toolbox-password), and sends the article's metadata JSON as an HTTP POST request with Basic Auth. Returns a (upload_success: bool, endpoint_url: str, text_retrieval_url: str) and treats HTTP 200 and 201 as success.

  • Example Usage:

  • python success, endpoint_url, response_text = begin_upload_to_toolbox( toolbox_id, metadata_file_path, header_metadata_format )


The lower-level helper functions in tasks.py are listed below.

get_toolbox_id()

  • Required Parameters:

  • toolbox_id_path (pathlib.Path): Path to the toolbox ID text file

  • Context for Use:

  • Reads and returns the full text content of the toolbox ID file. Logs an error and implicitly returns None if the file does not exist.

get_project_id()

  • Required Parameters:

  • toolbox_id (str): The toolbox ID string, e.g. 'xxxxx-0001'

  • Context for Use:

  • Extracts the project ID from a toolbox ID by splitting on - and taking the first segment (e.g. 'xxxxx-0001' → 'xxxxx'). Used to build the per-project upload endpoint URL.

get_toolbox_base_url()

  • Context for Use:

  • Reads the toolbox API's base URL from the shared harvesting config JSON.

get_config_json()

  • Context for Use:

  • Reads and returns the shared harvesting configuration JSON from the data root.